How can I obtain the name of an Area after ASP. NET MVC uses Areas? mvcareas
This article is intended only for future reference when Area (region) is used in ASP. net mvc projects.
The method to obtain the name of the current Area is:
ViewContext. RouteData. DataTokens ["area"]
In this way, I can use the following three statements to obtain the Area, Controller, and Action currently accessed by the user.
String areaName = filterContext. RouteData. DataTokens ["area"] = null? "": FilterContext. routeData. dataTokens ["area"]. toString (); string controllerName = filterContext. routeData. values ["controller"]. toString (); string actionName = filterContext. routeData. values ["action"]. toString ();
When the user accesses the main site (root directory), filterContext. RouteData. DataTokens ["area"] is null. Therefore, the first sentence of the code above contains a statement to determine whether the value is null.