0

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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This also fixes the problem when you have web methods within .aspx files and a catchall route.
0

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.

5 Comments

Have you also tried: routes.Add(new System.Web.Routing.Route("{resource}.css/{*pathInfo}", new System.Web.Routing.StopRoutingHandler())); ?
yes I did, doesn't seem to work. Actually i intercepted the request from global.asax begin request and whenever there is request for webresources.axd it doesn't hit the aspx init. when there is a request for css file, it hits the init of the aspx page.
Sorry, don't mean to beat a dead horse, but I assume you're adding the ignore rules before the route to the content handler aspx? Can you include the web.config portion where you're registering the handler for css/images?
yes, I guess I found a solution, but its not actually solving my problem. Just answering my question got link from haacked.com/archive/2008/07/14/…
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.