0

I just cant get this to work... I have the following routes:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("*.html|js|css|gif|jpg|jpeg|png|swf");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

routes.MapRoute(
    "Default", // Route name
    "{lama}/{controller}/{action}", // URL with parameters
    new { controller = "Home", action = "Index", lama = "en-gb" } // Parameter defaults
);

And once I load the page.. I have a img element that tries to retrive the following url: css/img/backgrounds/slide1_2048x1280.jpg

But the image wont show up and if I check my console I get the following error: GET {my localhost}/cn/Home/css/img/backgrounds/slide1_2048x1280.jpg 404 (Not Found)

I have such a hard time understanding the route-system.. is there anywhere I can read ALOT more about this?.. And could somebody please help me with this single problem then that whould be very appreciated!

4
  • Out of the box the MVC project template's default routing will not interfere with URLs that map to files that physically exist. Do you have routes.RouteExistingFiles = true in there somewhere? Commented Aug 6, 2012 at 14:03
  • Hmm... for some wierd reason it seems to work now.. :/... Commented Aug 6, 2012 at 14:14
  • Must have been some other wierd problem.. cause if i visit {my localhost}/en-gb.. everything works great.. but if i would visit {my localhost}/en-gb/Home/Index, the image is gone.. Commented Aug 6, 2012 at 14:16
  • Ah - that's a problem with how you are rendering you img link - you should ideally be using Url.Content to generate the correct absolute path to the image Commented Aug 6, 2012 at 14:33

2 Answers 2

1

I think have fallen foul of relative urls in your html.

Since you haven't said whether this is Razor or Aspx; I'm just going to go with Aspx.

When you write the img tag it seems that you might be doing:

<img src="[relative_path_to_file]" />, using the path of the img relative to the page.

If that doesn't start with / then it's almost certainly the case that you will end up with issues, especially since MVC URLs don't map to the path of the actual page.

What you want to do is to use Url.Content("~/[full_path_to_file]") which will ensure that an absolute path will always be used.

On another note - you really do not need to write all these ignore routes for files that exist on disk. By default, the routing engine will not route existing files - you have to set routes.RouteExistingFiles = true in the RegisterRoutes method in your global in order to route files that already exist; so I think you should get rid of them.

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

1 Comment

How couldnt I see that on myown :), thanks for pointing it out :)
0

i usually hit up 1) stackoverflow (obviously!), and 2) the msdn docs are pretty good: http://msdn.microsoft.com/en-us/library/dd410120.aspx. But i usually end up googling for specifically what i need =)

However, looks like you're trying to setup a route to ignore certain filetypes? i found this article that gives some good ideas on how to handle this.

I've only blocked one or two filetypes before, and i made one line per filetype. Not sure if you can make one line that has extensions delimited by pipe (|) like you're doing (i could be wrong!)

routes.IgnoreRoute("{*allaspx}", new {allaspx=@".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*allswf}", new {allswf=@".*\.swf(/.*)?"});

Comments

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.