How to get current MVC name in .cshtml file in ASP.NET Core

I can get the name of the current controller and the name of the action in _Layout.cshtmlby doing the following

var controller = ViewContext.RouteData.Values["controller"].ToString();
var action = ViewContext.RouteData.Values["action"].ToString();

However, when I try to get the area

var area = ViewContext.RouteData.Values["area"].ToString();

it does not work, because in this object there are only 2 keys ("controller" and "action")

I looked at the object ViewContext.RouteDataand cannot get the value for the current region name.

previous answers for previous version ASP.netdo not work for me.

+4
source share
1 answer

! GetNormalizedRouteValue . , RazorViewEngine.GetNormalizedRouteValue , "area".

<h2> @RazorViewEngine.GetNormalizedRouteValue(ViewContext, "area") </h2>

RazorViewEngine Microsoft.AspNetCore.Mvc.Razor. , using , .

@using Microsoft.AspNetCore.Mvc.Razor

GetNormalizedRouteValue , .


(MVC6).

@using Microsoft.AspNet.Routing
@{        
    var myAreaName = string.Empty;
    object areaObj;
    if (ViewContext.RouteData.Values.TryGetValue("area", out areaObj))
    {
        myAreaName = areaObj.ToString();
    }
}

<h1>@myAreaName</h1>
+8

All Articles