ASP.NET Core:跟踪当前活动页面,或如何在视图中获取控制器和操作名称

时间:2022-10-24 07:32:16

I am new in ASP.NET Core. I have a navigation menu and I would like to track the active item. My idea is to use the action and controller names as navigation keys:

我是ASP.NET Core的新手。我有一个导航菜单,我想跟踪活动项目。我的想法是使用动作和控制器名称作为导航键:

ASP.NET Core:跟踪当前活动页面,或如何在视图中获取控制器和操作名称

the problem is I don't know how to obtain the action and controller name in the _Layout.cshtml view...

问题是我不知道如何在_Layout.cshtml视图中获取动作和控制器名称...

I have tried the ViewContext.ActionDescriptor.DisplayName but it renders something like this MyApp.Controllers.RecordsController.Index (MyApp)

我已经尝试过ViewContext.ActionDescriptor.DisplayName,但它呈现的内容类似于MyApp.Controllers.RecordsController.Index(MyApp)

I'd rather prefer to obtain something like this:

我更愿意获得这样的东西:

<script>$("li#@(Controller.Name)-@(Action.Name)")).addClass("active");</script>

3 个解决方案

#1


13  

Use

var controller = ViewContext.RouteData.Values["Controller"];
var action = ViewContext.RouteData.Values["Action"];

<script>
$("li#@(ViewContext.RouteData.Values["Controller"])-@(ViewContext.RouteData.Values["Action"])")).addClass("active");
</script>

PS: Use ToLower() if required

PS:如果需要,使用ToLower()

ViewContext.RouteData.Values["Action"].ToString().ToLower();

Also you can activate your menu by style block

您也可以按样式块激活菜单

<style>
li#@(ViewContext.RouteData.Values["Controller"])-@(ViewContext.RouteData.Values["Action"]) {
//add some style
}
</style>

#2


4  

With dot net core 2.0 for razor pages you could use like this

使用针对剃刀页面的dot net core 2.0,您可以像这样使用

var rv = ViewContext.RouteData.Values;
string page = $"{rv["page"]}".ToLowerInvariant();

On the tags

在标签上

<li class="@(page == "/index" ?  "active" : "")"><a asp-page="/Index" >Home</a></li>

#3


1  

You can achieve this server side if you combine this answer with the following.

如果将此答案与以下内容结合使用,则可以实现此服务器端。

Somewhere in your view.

在你看来的某个地方。

@{
    var rv = ViewContext.RouteData.Values;
    var id = $"{rv["Controller"]}-{rv["Action"]}".ToLowerInvariant();
}

On each <li>.

在每个

  • 上。

  • <li class-conditional-active="@(id == "records-index")">...</li>
    

    #1


    13  

    Use

    var controller = ViewContext.RouteData.Values["Controller"];
    var action = ViewContext.RouteData.Values["Action"];
    
    <script>
    $("li#@(ViewContext.RouteData.Values["Controller"])-@(ViewContext.RouteData.Values["Action"])")).addClass("active");
    </script>
    

    PS: Use ToLower() if required

    PS:如果需要,使用ToLower()

    ViewContext.RouteData.Values["Action"].ToString().ToLower();
    

    Also you can activate your menu by style block

    您也可以按样式块激活菜单

    <style>
    li#@(ViewContext.RouteData.Values["Controller"])-@(ViewContext.RouteData.Values["Action"]) {
    //add some style
    }
    </style>
    

    #2


    4  

    With dot net core 2.0 for razor pages you could use like this

    使用针对剃刀页面的dot net core 2.0,您可以像这样使用

    var rv = ViewContext.RouteData.Values;
    string page = $"{rv["page"]}".ToLowerInvariant();
    

    On the tags

    在标签上

    <li class="@(page == "/index" ?  "active" : "")"><a asp-page="/Index" >Home</a></li>
    

    #3


    1  

    You can achieve this server side if you combine this answer with the following.

    如果将此答案与以下内容结合使用,则可以实现此服务器端。

    Somewhere in your view.

    在你看来的某个地方。

    @{
        var rv = ViewContext.RouteData.Values;
        var id = $"{rv["Controller"]}-{rv["Action"]}".ToLowerInvariant();
    }
    

    On each <li>.

    在每个

  • 上。

  • <li class-conditional-active="@(id == "records-index")">...</li>