This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
42 lines (35 loc) · 1.65 KB
/
ExtensionMethods.cs
File metadata and controls
42 lines (35 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Mvc.Rendering;
namespace NuGet.Insights.Website
{
public static class ExtensionMethods
{
/// <summary>
/// Source: https://stackoverflow.com/a/20411015
/// </summary>
public static string IsSelected(this IHtmlHelper htmlHelper, string controllers, string actions, string cssClass = "active")
{
var currentAction = htmlHelper.ViewContext.RouteData.Values["action"] as string;
var currentController = htmlHelper.ViewContext.RouteData.Values["controller"] as string;
IEnumerable<string> acceptedActions = (actions ?? currentAction).Split(',');
IEnumerable<string> acceptedControllers = (controllers ?? currentController).Split(',');
return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
cssClass : string.Empty;
}
public static string GetRuntime(this DateTimeOffset? completed, DateTimeOffset? started)
{
if (!started.HasValue)
{
return string.Empty;
}
var runtime = completed.GetValueOrDefault(DateTimeOffset.UtcNow) - started.Value;
var runtimeStr = runtime.ToString("d\\.hh\\:mm\\:ss", CultureInfo.InvariantCulture);
if (runtimeStr.StartsWith("0.", StringComparison.Ordinal))
{
runtimeStr = runtimeStr.Substring(2);
}
return runtimeStr;
}
}
}