HTTP module vs action filter in asp.net-mvc

空扰寡人 提交于 2019-11-27 14:38:27

问题


I am developing an application in asp.net MVC3 and I have the following questions: When should I write an HTTP module and when should I write an action filter?


回答1:


  1. Filter are more MVC approach of doing thing whereas Http Module are more of ASP.NET way of doing thing. Both serve similar purpose by providing hook in the processing pipline.

  2. HttpModule is more generic and when you want some thing to be processed on every request. Filters are useful for adding action specific behaviour.

  3. If you want some thing to be executed only once per Http Request, you should use an HttpModule. ActionFilter may get executed several times during a request until and unless you check IsChildActionOn.




回答2:


HttpModule are called before and after the request handler executes. They are intended to enable a developer to intercept, participate, or modify each request. There are 22 available events that can be subscribed to that enables the module to work on the request in various stages of the process. The events are useful for page developers who want to run code when key request pipeline events are raised. They are also useful if you are developing a custom module and you want the module to be invoked for all requests to the pipeline.

Filters are designed to inject logic in between MVC request life cycle. Specifically before and after de action is invoked, as well as, before and after the result is processed. Filters provide users with powerful ways to inspect, analyze, capture and instruments several things going around within MVC projects. As of MVC5, there are 5 types of filters :

  • Authentication
  • Authorization
  • Action
  • Result
  • Exception

So if you want to intercept, participate, or modify in a specific of the 22 events in the http request pipeline choose the modules. If your logic is is strictly related to the action method you better server overriding one of the following ActionFilterAttribute methods:

  • OnActionExecuting
  • OnActionExecutted
  • OnResultExecuting
  • OnResultExecuted



回答3:


HttpModule is how IIS allows an Web application to override the default behavior or add custom logic by letting you attach event handlers to HttpApplication events. Different IIS modes (Integrated or Classic) even use has different Web.config settings.
Reference:
http://msdn.microsoft.com/en-us/library/ms227673(v=vs.100).aspx

Example: redirect non-www to www URLs

public void Init(HttpApplication application)
{
    application.PreRequestHandlerExecute += this.Application_PreRequestHandlerExecute;
}

private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    Uri requestUrl = HttpContext.Current.Request.Url;
    string host = requestUrl.Authority.ToLower();
    if (!host.StartsWith("www"))
    {
        HttpContext.Current.Response.Redirect(requestUrl.Scheme + "://www." + host + requestUrl.PathAndQuery);
        HttpContext.Current.Response.End();
    }
}

An Action Filter is an attribute decorating controllers or action methods. It is an abstraction layer between MVC routing and action methods. With action filters, we can apply same logic to multiple controllers or action methods. for example, custom logging.



来源:https://stackoverflow.com/questions/11507496/http-module-vs-action-filter-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!