Resolve virtual path in MVC

Banshee picture Banshee · Sep 19, 2010 · Viewed 12.4k times · Source

Hi,

I got a adress like this : ~/Content/Files/AdImages/20/20_thumb.jpeg, I need this to be resolved. This was done in ASP.net with Control.ResolveUrl().

According to this article http://stephenwalther.com/blog/archive/2009/02/18/asp.net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx, I should use somthing like this :

urlHelper.Content("~/Content/Files/AdImages/20/20_thumb.jpeg")

This does however returns the following url : /Content/Files/AdImages/20/20_thumb.jpeg even when Im not in the root of the solution(Content is placed in the root)?

How can I resolve this?

note that this is from within a html helper extension.

//SnowJim

Answer

Steve Michelotti picture Steve Michelotti · Sep 19, 2010

The job of the ~ is to always resolve to the root so you are seeing the correct behavior. If you want this to be relative to the path where you currently are then your path should not start with a ~ or a /