asp.net-mvc 我可以从 HttpContext 获取控制器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5302444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I get the controller from the HttpContext?
提问by hackerhasid
Given an HttpContext (or HttpContextBase), is there a way to get an instance of the Controller?
给定一个 HttpContext(或 HttpContextBase),有没有办法获取控制器的实例?
采纳答案by George Stocker
The HttpContext
will hold a reference to the MvcHandler
, which will hold a reference to the RouteData
, which will hold a reference to what controller is being invoked by a particular route.
TheHttpContext
将持有对 的引用,该引用将持有对 的引用MvcHandler
,该引用RouteData
将持有对特定路由正在调用的控制器的引用。
NB:This doesn't give you the actual controller, only the controller that the specific route is going to catch.
注意:这不会为您提供实际的控制器,只会提供特定路由将要捕获的控制器。
GetController(HttpContextBase httpContext)
{
var routeData = ((MvcHandler)httpContext.Handler).RequestContext.RouteData;
var routeValues = routeData.Values;
var matchedRouteBase = routeData.Route;
var matchedRoute = matchedRouteBase as Route;
if (matchedRoute != null)
{
Route = matchedRoute.Url ?? string.Empty;
}
AssignRouteValues(httpContext, routeValues);
}
protected virtual VirtualPathData getVirtualPathData(HttpContextBase httpContext, RouteValueDictionary routeValues)
{
return RouteTable.Routes.GetVirtualPath(((MvcHandler)httpContext.Handler).RequestContext, routeValues);
}
private void AssignRouteValues(HttpContextBase httpContext, RouteValueDictionary routeValues)
{
var virtualPathData = getVirtualPathData(httpContext, routeValues);
if (virtualPathData != null)
{
var vpdRoute = virtualPathData.Route as Route;
if (vpdRoute != null)
{
RouteDefaults = vpdRoute.Defaults;
RouteConstraints = vpdRoute.Constraints;
RouteDataTokens = virtualPathData.DataTokens;
RouteValues = routeValues;
}
}
}
This code may look familiar, it's because I've adapted it from Phil Haack's route debuggersource code.
这段代码可能看起来很熟悉,这是因为我改编自 Phil Haack 的路由调试器源代码。
回答by Bill
For those looking just to get the controller name and not an actual instance, as is needed for custom authorization overrides of AuthorizeCore(httpContext), this is the clean code.
对于那些只想获取控制器名称而不是实际实例的人来说,这是 AuthorizeCore(httpContext) 的自定义授权覆盖所需要的,这是干净的代码。
var request = httpContext.Request;
var currentUser = httpContext.User.Identity.Name;
string controller = request.RequestContext.RouteData.Values["controller"].ToString();
string action = request.RequestContext.RouteData.Values["action"].ToString();
回答by NidhinSPradeep
var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
var currentController = currentRouteData.Values["controller"].ToString();
var currentAction = currentRouteData.Values["action"].ToString();
回答by Vadim
Not easily, you will basically have to first get the MvcHandler from the RouteData, then build the Controller. Even then, it won't give you the instance used to handle the action as it will be a new instance of the controller.
不容易,您基本上必须首先从 RouteData 获取 MvcHandler,然后构建控制器。即便如此,它也不会为您提供用于处理操作的实例,因为它将是控制器的新实例。