简体   繁体   中英

Identify MVC request in Global.asax.cs in Asp .NET MVC 2

Is is possible in EndRequest handler in Global.asax.cs in Asp .NET MVC 2 to idenfity whether current request handled by a mvc controller rather than a request to fetch some resource file (js, css, an image)? What further interests me, is it possible to do so even when response was once generated using a controller but is not served from OutputCache.

I think that you can run this code: this.Context.Handler is MvcHandler in the Application_EndRequest in the Global.asax.

If you want to iterate yourself over the Routes you can do something like this:

if(this.Context.Handler == null)
{
            foreach (var route in RouteTable.Routes)
            {
                var foundRoute = route.GetRouteData(new HttpContextWrapper(Context));
                if(foundRoute==null)
                    continue;    

                if(foundRoute.RouteHandler is MvcRouteHandler)
                {
                  // code
                  break;
                }
            }
}

if I remember correctly foundRoute will be NULL if it does not match the context, but you should run this code and test it.

FYI Ignored routes implement StopRouteHandler. MvcRoutees implement MvcRouteHandler. There are some other handlers such as PageRouteHandler and ResourceRouteHandler.

Update:

BTW, I would add a hash or some caching method, if you plan to use this search, so you don't iterate over the routes every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM