ASP.NET MVC Images and Another Static Content URL

I just edited my route for the 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 that my url looks like this: localhost/Users/Details/1/ShawnMclean Images do not load both from the controller and from the site.master website. (don't know why css and javascript had the correct urls). If url 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 url gets an extra parameter, the image is actually in ../../../Content/Images/logo3.png

Is there a way to change images and other static content urls?

+7
asp.net-mvc asp.net-mvc-routing
source share
3 answers

Try combining your images as follows:

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

or if that doesn't work, you can always use the helper for your links

 <img src="<%= Url.Content("~/Content/Images/logo3.png") %>" /> 

Another way could be

 <img src="@Url.Content("~/Content/Images/logo3.png")" /> 
+18
source share

You can try using an assistant:

 <img src='<%= Url.Content( "~/Content/Images/pic.jpg" ) %>' alt="My Image" /> 
+1
source share

You can try this,

 <a href="/"><img src="<%=Url.Content("~/Content/Images/logo.png")%>" alt="logo" title="Logo" /></a> 
+1
source share

All Articles