views:

142

answers:

2

We have URLs of the form

http://site/controller.mvc/action

If we accidentally write a relative URL for an image into the html (say src="imgs/img1.gif") this results in the browser making a request to:

http://site/controller.mvc/action/imgs/img1.gif

Which gets routed through to the controller but then cannot resolve to an action method.

This is fine except we would rather use routing to prevent this ever happening (so TempData doesn't get emptied and whatever).

However if we put a custom constraint in to stop this happening (using Regular Expressions) we get a 200 and an empty response rather than the expected 404. Any ideas?

+2  A: 

I always prefix my images with HttpRuntime.AppDomainAppVirtualPath

Like this;

background="<%=HttpRuntime.AppDomainAppVirtualPath %>/Content/images/aqua.jpg"

Also I think your imgs folder needs to be under the Content folder.

src="/content/imgs/img1.gif

The shortcut to this is actually the "~" (tilde) character, but I prefer to write it out.

src="~/content/imgs/img1.gif"

You could also create a static variable to hold this value,

public static string VP = HttpRuntime.AppDomainAppVirtualPath;

Then on you page do it this way,

background="<%=VP%>/Content/images/aqua.jpg"

This is much easier than writting a helper to return a simple string.

Tony Borf
The thing with "accidentially" is that in most cases it happens unintentionally. Still +1 for HttpRuntime.AppDomainAppVirtualPath even though it doesn't really answer the question.
Michael Stum
"accidentially" in programming is called a bug, and needs to be corrected manually.
Tony Borf
You should really use Url.Content for this rather than reinventing it in every view you write.
Craig Stuntz
Agree with Tony here. Thanks for the tip but we are looking to provide a safeguard against carelessness here.
Gaz
Sorry, I meant agree with Michael
Gaz
You realy can't safegaurd against carelessness in programming all you get is sloppy code.
Tony Borf
The problem is this Tony: Sloppy mark-up may result in a URL of the type I describe in my question. This can then cause use an image request to be routed through to the MVC and TempData to be emptied thus resulting in a subtle and intermittent bug. We would like to avoid these sort of bugs by routing these accidental requests away.The question I asked is about routing, not about resolving image urls.
Gaz
This question seems to be more about Quality Control and Testing than anything else. If your software is tested properly you should discover that you have a missing image on a page. You could also write a script to read your source code looking for these kind of mistakes. It's bad practice to add code just to correct programming mistakes on the fly.
Tony Borf
A: 

I'd suggest you'll use a helper method, and then from there make sure to stick a slash in front of the paths (e.g. "/content/img/img1.gif").

See tip #2 at http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx

synhershko