Ignore embedded resources routing ASP.NET 4 WebFor

2019-08-23 10:33发布

问题:

I am using routing in asp.net 4 webforms. I have a theme dll which contains all the images, css and js files required for look and feel. I have only 1 page which dynamically loads the control in the page. I use routing to distinguish the request. Following routes are defined:

routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Default-All-Pages", "Pages/{*OtherParams}", "~/Default.aspx", false);

Handler for managing the embedded resources is already defined. When the application is executed it by virtue of code, redirects the request to default.aspx. it then goes ahead to load the css file and again routes the request to default.aspx.

I want it to route the css/jpg request to virtual path handler and not the page. What route should I define so that the request for files will not be handled by default.aspx page?

回答1:

routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" }); 
routes.Ignore("{*allcss}", new { allcss = @".*\.css(/.*)?" }); 
routes.Ignore("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" }); 
routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" }); 

This solved my problem.



回答2:

The same way you're ignoring HttpHandlers, you can add ignore rules for css and jpg files:

routes.Ignore("{resource}.css/{*pathInfo}");
routes.Ignore("{resource}.jpg/{*pathInfo}");

These will be excluded from the route table and will be handled by any registered handlers/modules/ISAPI filters.