views:

23

answers:

2

I just edited my route for a user details page to look like this:

        routes.MapRoute(
            "UserDetails", // Route name
            "{controller}/{action}/{id}/{title}", // URL with parameters
            new { controller = "Users", action = "Details", id = UrlParameter.Optional, title = UrlParameter.Optional } // Parameter defaults
            );

Now when my url looks like this: localhost/Users/Details/1/ShawnMclean Images do not load both from the controller and the site.master. (no idea why the css and javascript had correct urls though). If the url is localhost/Users/Details/1 then everything loads fine.

My img in site.master and Details.aspx looks like this in the old url:

<img src="../../Content/Images/logo3.png" />

but when the url gets an additional parameter, the image is actually located at ../../../Content/Images/logo3.png

Is there a way to make images and other static content's url change?

A: 

Try linking your images like this:

<img src="/Content/Images/logo3.png" /> 

or if that doesn't work you could always use a helper for your links

<img src="/<%= Url.Content("~/Content/Images/logo3.png") %>" />
quakkels
I have that above all my routes.
Shawn Mclean
could you post a code sample of an image link or css link?
quakkels
Why did `/Content/Images/logo3.png` worked? does the first slash make it start from the site root?
Shawn Mclean
`/` is for absolute (root). `../` is relative. Always try and use absolute paths. The correct way for MVC is using `Url.Content(~/)`.
RPM1984
+1  A: 

You can try using a helper:

<img src='<%= Url.Content( "~/Content/Images/pic.jpg" ) %>' alt="My Image" />
Justin Soliz