I need to check some stuff (Cookies) for each request coming to my application.
我需要为进入我的应用程序的每个请求检查一些内容(Cookies)。
In ASP.NET we've used HttpModule for this task , the question what should be used in MVC ? Some Global Filter , or I can Use HttpModuler as well, is there Any difference in Request PipeLine between MVC and regular ASP.NET ?
在ASP.NET中我们使用HttpModule来完成这个任务,这个问题应该在MVC中使用什么?一些全局过滤器,或者我也可以使用HttpModuler,在MVC和常规ASP.NET之间的Request PipeLine有什么区别吗?
3 个解决方案
#1
11
MVC is an abstraction over ASP.NET and therefore their "hooks" really depend at which level you want to inject your logic. An action filter will allow you to hook into MVC specific events:
MVC是对ASP.NET的抽象,因此它们的“钩子”实际上取决于您想要注入逻辑的级别。动作过滤器将允许您挂钩到MVC特定事件:
-
OnActionExecuting
– This method is called before a controller action is executed. - OnActionExecuting - 在执行控制器操作之前调用此方法。
-
OnActionExecuted
– This method is called after a controller action is executed. - OnActionExecuted - 执行控制器操作后调用此方法。
-
OnResultExecuting
– This method is called before a controller action result is executed. - OnResultExecuting - 在执行控制器操作结果之前调用此方法。
-
OnResultExecuted
– This method is called after a controller action result is executed. - OnResultExecuted - 在执行控制器操作结果后调用此方法。
Whereas an HttpModule only allows you to hook into ASP.NET (upon which MVC is built) specific events:
而HttpModule只允许您挂钩到ASP.NET(构建MVC)特定事件:
-
BeginRequest
- Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page), synchronize this event. - BeginRequest - 请求已经开始。如果您需要在请求开始时执行某些操作(例如,在每个页面顶部显示广告横幅),请同步此事件。
-
AuthenticateRequest
- If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password), build a module that synchronizes this event and authenticates the user in a way that you want to. - AuthenticateRequest - 如果要插入自己的自定义身份验证方案(例如,针对数据库查找用户以验证密码),请构建一个同步此事件的模块,并以您希望的方式对用户进行身份验证。
-
AuthorizeRequest
- This event is used internally to implement authorization mechanisms (for example, to store your access control lists (ACLs) in a database rather than in the file system). Although you can override this event, there are not many good reasons to do so. - AuthorizeRequest - 此事件在内部用于实现授权机制(例如,将访问控制列表(ACL)存储在数据库中而不是文件系统中)。虽然您可以覆盖此事件,但没有太多合理的理由。
-
PreRequestHandlerExecute
- This event occurs before the HTTP handler is executed. - PreRequestHandlerExecute - 此事件在HTTP处理程序执行之前发生。
-
PostRequestHandlerExecute
- This event occurs after the HTTP handler is executed. - PostRequestHandlerExecute - 执行HTTP处理程序后发生此事件。
-
EndRequest
- Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information to the page. - EndRequest - 请求已完成。您可能希望构建一个调试模块,该模块在整个请求中收集信息,然后将信息写入页面。
So it really depends on when you need to hook in your event and which events you need.
所以这取决于你何时需要挂钩你的活动以及你需要哪些活动。
#2
3
If the HttpModule worked well for you before then it will continue to with Mvc.
如果HttpModule在此之前效果很好,那么它将继续使用Mvc。
The other parts of your question are quite broad in scope and think you'd be as well reading a good article on asp.net-mvc pipeline and extensibility.
您的问题的其他部分范围非常广泛,并认为您还可以阅读有关asp.net-mvc管道和可扩展性的好文章。
#3
2
I've done similar things using a global action filter. It works quite well, and keeps your code integrated within your application.
我使用全局动作过滤器做了类似的事情。它运行良好,并将您的代码集成到您的应用程序中。
An HTTP module works as well, of course, but this will mean seperating the code from your main application and maintaining it seperately. Unless your code spans multiple sites or is used in multiple applications, or needs to work with web forms sites, then I would use a global filter.
当然,HTTP模块也可以工作,但这意味着从主应用程序中分离代码并单独维护它。除非您的代码跨越多个站点或用于多个应用程序,或者需要使用Web表单站点,否则我将使用全局过滤器。
#1
11
MVC is an abstraction over ASP.NET and therefore their "hooks" really depend at which level you want to inject your logic. An action filter will allow you to hook into MVC specific events:
MVC是对ASP.NET的抽象,因此它们的“钩子”实际上取决于您想要注入逻辑的级别。动作过滤器将允许您挂钩到MVC特定事件:
-
OnActionExecuting
– This method is called before a controller action is executed. - OnActionExecuting - 在执行控制器操作之前调用此方法。
-
OnActionExecuted
– This method is called after a controller action is executed. - OnActionExecuted - 执行控制器操作后调用此方法。
-
OnResultExecuting
– This method is called before a controller action result is executed. - OnResultExecuting - 在执行控制器操作结果之前调用此方法。
-
OnResultExecuted
– This method is called after a controller action result is executed. - OnResultExecuted - 在执行控制器操作结果后调用此方法。
Whereas an HttpModule only allows you to hook into ASP.NET (upon which MVC is built) specific events:
而HttpModule只允许您挂钩到ASP.NET(构建MVC)特定事件:
-
BeginRequest
- Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page), synchronize this event. - BeginRequest - 请求已经开始。如果您需要在请求开始时执行某些操作(例如,在每个页面顶部显示广告横幅),请同步此事件。
-
AuthenticateRequest
- If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password), build a module that synchronizes this event and authenticates the user in a way that you want to. - AuthenticateRequest - 如果要插入自己的自定义身份验证方案(例如,针对数据库查找用户以验证密码),请构建一个同步此事件的模块,并以您希望的方式对用户进行身份验证。
-
AuthorizeRequest
- This event is used internally to implement authorization mechanisms (for example, to store your access control lists (ACLs) in a database rather than in the file system). Although you can override this event, there are not many good reasons to do so. - AuthorizeRequest - 此事件在内部用于实现授权机制(例如,将访问控制列表(ACL)存储在数据库中而不是文件系统中)。虽然您可以覆盖此事件,但没有太多合理的理由。
-
PreRequestHandlerExecute
- This event occurs before the HTTP handler is executed. - PreRequestHandlerExecute - 此事件在HTTP处理程序执行之前发生。
-
PostRequestHandlerExecute
- This event occurs after the HTTP handler is executed. - PostRequestHandlerExecute - 执行HTTP处理程序后发生此事件。
-
EndRequest
- Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information to the page. - EndRequest - 请求已完成。您可能希望构建一个调试模块,该模块在整个请求中收集信息,然后将信息写入页面。
So it really depends on when you need to hook in your event and which events you need.
所以这取决于你何时需要挂钩你的活动以及你需要哪些活动。
#2
3
If the HttpModule worked well for you before then it will continue to with Mvc.
如果HttpModule在此之前效果很好,那么它将继续使用Mvc。
The other parts of your question are quite broad in scope and think you'd be as well reading a good article on asp.net-mvc pipeline and extensibility.
您的问题的其他部分范围非常广泛,并认为您还可以阅读有关asp.net-mvc管道和可扩展性的好文章。
#3
2
I've done similar things using a global action filter. It works quite well, and keeps your code integrated within your application.
我使用全局动作过滤器做了类似的事情。它运行良好,并将您的代码集成到您的应用程序中。
An HTTP module works as well, of course, but this will mean seperating the code from your main application and maintaining it seperately. Unless your code spans multiple sites or is used in multiple applications, or needs to work with web forms sites, then I would use a global filter.
当然,HTTP模块也可以工作,但这意味着从主应用程序中分离代码并单独维护它。除非您的代码跨越多个站点或用于多个应用程序,或者需要使用Web表单站点,否则我将使用全局过滤器。