我在.NET 4.6中有一个像这样的HTML扩展名
public static string IsSelected( this HtmlHelper html, string controller = null, string action = null, string area = null ) {
string cssClass = "active";
string currentAction = (string)html.ViewContext.RouteData.Values[ "action" ];
string currentController = (string)html.ViewContext.RouteData.Values[ "controller" ];
string currentArea = html.ViewContext.RouteData.DataTokens[ "area" ] == null ? String.Empty : (string)html.ViewContext.RouteData.DataTokens[ "area" ];
if ( String.IsNullOrEmpty( controller ) )
controller = currentController;
if ( String.IsNullOrEmpty( action ) )
action = currentAction;
if ( String.IsNullOrEmpty( area ) )
area = currentArea;
if ( controller.ToLower() == currentController.ToLower() && action.ToLower() == currentAction.ToLower() && area == currentArea ) {
return cssClass;
}
else {
return string.Empty;
}
}
通过这个帮助程序,我能够在剃刀视图中编写这样的代码:
<li class="treeview @Html.IsSelected( controller: "mycontroller", action : string.Empty, area : string.Empty )">
<a href="#">
<i class="fa fa-list-alt"></i> <span>Menu 1</span>
<span class="pull-right-container">
<i class="fa fa-angle-right pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li class="@Html.IsSelected( controller: "mycontroller", action : "myaction", area : string.Empty )">
<a href="@Url.Action("myaction", "mycontroller" )">
<i class="fa fa-retweet"></i>
My Action
</a>
</li>
</ul>
</li>
如何将此帮助程序转换为与 .NET Core 一起使用?我已经尝试使用IHtmlContent
但我无法获得ViewContext
.唯一的方法是标签助手?
也许很晚,但你可以通过以下方式获得它:
this.ViewContext
当您在 CSHTML 文件中时