【发布时间】:2023-03-11 09:02:01
【问题描述】:
有 Helpers 负责将 Active 类添加到菜单中。
public static class HtmlHelpers
{
public static string IsSelected(this IHtmlHelper html, string controller = null, string action = null, string status = null, string cssClass = null)
{
if (String.IsNullOrEmpty(cssClass))
cssClass = "active";
string currentAction = (string)html.ViewContext.RouteData.Values["action"];
string currentController = (string)html.ViewContext.RouteData.Values["controller"];
string currentStatus = (string)html.ViewContext.RouteData.Values["status"];
if (String.IsNullOrEmpty(controller))
controller = currentController;
if (String.IsNullOrEmpty(action))
action = currentAction;
if (String.IsNullOrEmpty(status))
status = currentStatus;
return controller == currentController && action == currentAction && status == currentStatus ?
cssClass : String.Empty;
}
public static string PageClass(this IHtmlHelper htmlHelper)
{
string currentAction = (string)htmlHelper.ViewContext.RouteData.Values["action"];
return currentAction;
}
}
虽然涉及标准 URL 参数(“controller} / {action} / {id?}”,但一切正常。但是如何读取 URL 中的变量。
是https://localhost:/Contractors?Status=false
如何获取状态数据
附:对于那些以后想使用 Helper 的人。 CHTML
<li class="@Html.IsSelected(action: "Index", controller: "Contractors", status: "true")"><a asp-action="Index" asp-controller="Contractors" asp-route-status="true">Clients</a></li>
【问题讨论】:
标签: c# asp.net-core asp.net-core-2.0