Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Request filters

Tags:

asp.net-mvc

Doesn't ASP.NET MVC support some kind of RequestFilters - the filters which are executed once per request before controllers and actions instantiation?

like image 509
SiberianGuy Avatar asked Aug 04 '11 11:08

SiberianGuy


People also ask

Which filter will be executed at first using ASP.NET MVC?

as you can see from the below diagram, as soon as the controller starts execution through Action Invoker, Authentication and authorization filters are the very first filters to be triggered, followed by model binding which maps request and route data to action parameters.

What is filter and types of filter in MVC?

The ASP.NET MVC framework supports four different types of filters − Authorization Filters − Implements the IAuthorizationFilter attribute. Action Filters − Implements the IActionFilter attribute. Result Filters − Implements the IResultFilter attribute. Exception Filters − Implements the IExceptionFilter attribute.


2 Answers

You could create your own Routing Handler which might be early enough in the pipeline to achieve your goal.

public class MyRoutingHandler : IRouteHandler
{
    protected virtual IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new InterceptingMvcHandler(requestContext);
    }

    IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext)
    {
        return GetHttpHandler(requestContext);
    }

}

and the corresponding mvc handler:

public class InterceptingMvcHandler : MvcHandler
{
    public InterceptingMvcHandler(RequestContext requestContext) : base(requestContext)
    {
    }

    protected override IAsyncResult BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, object state)
    {
        httpContext.Response.Write("<h2>BeginProcessRequest</h2>");
        return base.BeginProcessRequest(httpContext, callback, state);
    }

    protected override void EndProcessRequest(IAsyncResult asyncResult)
    {
        var context = RequestContext.HttpContext;
        base.EndProcessRequest(asyncResult);
        if (context != null)
        {
            context.Response.Write("<h2>EndProcessRequest</h2>");
        }
    }
}

You can then register the mvc handler in your route registrations.

like image 141
Kim Major Avatar answered Nov 01 '22 09:11

Kim Major


here is an example for you;

public class CompressFilter : ActionFilterAttribute {

    public override void OnActionExecuting(ActionExecutingContext filterContext) {

        HttpRequestBase request = filterContext.HttpContext.Request;

        string acceptEncoding = request.Headers["Accept-Encoding"];

        if (string.IsNullOrEmpty(acceptEncoding)) return;

        acceptEncoding = acceptEncoding.ToUpperInvariant();

        HttpResponseBase response = filterContext.HttpContext.Response;

        if (acceptEncoding.Contains("GZIP")) {

            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);

        } else if (acceptEncoding.Contains("DEFLATE")) {

            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
        }

    }

}

ones you created it, you can use it per action, per controller or even for global project basis;

    public static void RegisterGlobalFilters(GlobalFilterCollection filters) {

        filters.Add(new CompressFilter());

    }
like image 34
tugberk Avatar answered Nov 01 '22 07:11

tugberk