Get Current Area Name in View or Controller

前端 未结 12 1498
闹比i
闹比i 2020-11-29 00:50

How do you get the current area name in the view or controller?

Is there anything like ViewContext.RouteData.Values[\"controller\"] for areas?

相关标签:
12条回答
  • 2020-11-29 01:24

    I dont know why but accepted answer is not working. It returns null with e.g ( maybe about mvc, i use .net core )

    http://localhost:5000/Admin/CustomerGroup

    I always debug the variable and fetch data from in it.

    Try this. It works for me

    var area = ViewContext.RouteData.Values["area"]
    

    Detailed logical example

    Layout = ViewContext.RouteData.Values["area"] == null ? "_LayoutUser" : "_LayoutAdmin";
    
    0 讨论(0)
  • 2020-11-29 01:26

    I know this is a very very old post but we can use the Values Property exactly the same way as the DataTokens

    Url.RequestContext.RouteData.Values["action"] worked for me.

    0 讨论(0)
  • 2020-11-29 01:27

    Get area name in View (.NET Core 2.2):

    ViewContext?.ActionDescriptor?.RouteValues["area"]
    
    0 讨论(0)
  • 2020-11-29 01:30

    To get area name in the view, in ASP.NET Core MVC 2.1:

    @Context.GetRouteData().Values["area"]
    
    0 讨论(0)
  • 2020-11-29 01:41

    In ASP.NET Core 1.0 the value is found in

    ViewContext.RouteData.Values["area"];

    0 讨论(0)
  • 2020-11-29 01:42

    MVC Futures has an AreaHelpers.GetAreaName() method. However, use caution if you're using this method. Using the current area to make runtime decisions about your application could lead to difficult-to-debug or insecure code.

    0 讨论(0)
提交回复
热议问题