.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();
posted @ 2020-12-04 19:27  户的博客  阅读(587)  评论(0编辑  收藏  举报