1、视图中
string controller =ViewContext.RouteData.Route.GetRouteData(this.Context).Values["controller"].ToString(); string controller = ViewContext.RouteData.Values["controller"].ToString();
2、控制器的action中
string controller = RouteData.Route.GetRouteData(this.HttpContext).Values["controller"].ToString(); string controller = RouteData.Values["controller"].ToString();
3、过滤器中
/// <summary> /// 过滤器中 /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); string controller = filterContext.RouteData.Values["controller"].ToString(); controller = controller + "Controller"; }
4、公共方法中
/// <summary> /// 获取当前页面的Controller全名称 /// </summary> /// <returns></returns> public string GetCurrentController() { string controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString(); if (!string.IsNullOrWhiteSpace(controller)) { controller = controller + "Controller"; } else { controller = ""; } return controller; }