.net mvc获取当前请求的区域、控制器、方法名
作者:互联网
转载:https://blog.csdn.net/chengmodelong/article/details/46228109
1)过滤器中的:
public class CMSAttribute : FilterAttribute, IAuthorizationFilter { public void OnAuthorization(AuthorizationContext filterContext) { var controllerName = (filterContext.RouteData.Values["controller"]).ToString().ToLower(); var actionName = (filterContext.RouteData.Values["action"]).ToString().ToLower(); var areaName = (filterContext.RouteData.DataTokens["area"] == null ? "" : filterContext.RouteData.DataTokens["area"]).ToString().ToLower(); } }
2)视图中的:
@{ //获取当前路由的区域名称 var areaName = (ViewContext.RouteData.DataTokens["area"] == null ? "" : ViewContext.RouteData.DataTokens["area"]).ToString().ToLower(); var controllerName = ViewContext.RouteData.Values["controller"].ToString().ToLower(); var actionName = ViewContext.RouteData.Values["action"].ToString().ToLower(); }3)action中:
string areaName=RouteData.DataTokens["area"].ToString().ToLower(); string controllerName = RouteData.Values["controller"].ToString().ToLower(); string actionName = RouteData.Values["action"].ToString().ToLower();
标签:控制器,ToLower,RouteData,mvc,ToString,var,Values,net,DataTokens 来源: https://www.cnblogs.com/fangxinliu/p/14087402.html