ASP.Net MVC路由和PreRequestHandler

时间:2022-08-26 20:49:13

I'm trying to instantiate a service and authenticate the current user within the Application_PreRequestHandlerExecute() method and then dispose of this service in the* Application_PostRequestHandlerExecute() method of the global.asax.cs class. One of the items I need for this process is the orgname which is appended at the beginning of my url route. I have mapped a route that looks like this "{orgName}/{controller}/{action}/{id}"

我正在尝试实例化一个服务并在Application_PreRequestHandlerExecute()方法中验证当前用户,然后在global.asax.cs类的* Application_PostRequestHandlerExecute()方法中处理此服务。我需要这个过程的一个项目是orgname,它附加在我的url路由的开头。我已经映射了一个看起来像这样的路线“{orgName} / {controller} / {action} / {id}”

So my question is, within an ASP.Net MVC application is it possible to access any of the routing information (or somehow access the "orgname" in my instance) within the Application_PreRequestHandlerExecute() event? If this is not possible is there some other way to hook into an MvcHandler and do something similar (maybe I should build a custom filter?)

所以我的问题是,在ASP.Net MVC应用程序中,是否可以在Application_PreRequestHandlerExecute()事件中访问任何路由信息(或以某种方式访问​​我实例中的“orgname”)?如果这是不可能的,还有其他方法可以挂钩到MvcHandler并做类似的事情(也许我应该构建一个自定义过滤器?)

1 个解决方案

#1


1  

You need the "RequestContext" to find all the route values. I don't know any other way to get them than inside the controller.

您需要“RequestContext”来查找所有路径值。我不知道任何其他方式来获取它们而不是控制器内部。

You sould implement a "ActionFilterAttribute", then decorate your controllers with it.

你应该实现一个“ActionFilterAttribute”,然后用它装饰你的控制器。

the ActionFilter has the methods

ActionFilter有方法

//     Called after the action method executes.
public virtual void OnActionExecuted(ActionExecutedContext filterContext);
//     Called before the action method executes.
public virtual void OnActionExecuting(ActionExecutingContext filterContext);

that you can do all sorts of fun stuff in.

你可以做各种有趣的事情。

#1


1  

You need the "RequestContext" to find all the route values. I don't know any other way to get them than inside the controller.

您需要“RequestContext”来查找所有路径值。我不知道任何其他方式来获取它们而不是控制器内部。

You sould implement a "ActionFilterAttribute", then decorate your controllers with it.

你应该实现一个“ActionFilterAttribute”,然后用它装饰你的控制器。

the ActionFilter has the methods

ActionFilter有方法

//     Called after the action method executes.
public virtual void OnActionExecuted(ActionExecutedContext filterContext);
//     Called before the action method executes.
public virtual void OnActionExecuting(ActionExecutingContext filterContext);

that you can do all sorts of fun stuff in.

你可以做各种有趣的事情。