views:

84

answers:

1

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

A: 

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 /

Steve Michelotti
Thanks, I Got the path ~/Content/Files/AdImages/20/20_thumb.jpeg, I am able to translate this to the server side absolut path. In this case do I however neeed the relative path to the image. This could result in a img src like ../../Content/Files/AdImages/20/20_thumb.jpeg. How do I do this?
SnowJim
Then I guess I'm not clear on what you're trying to do. Sounds like you understand the absolute path. I don't think I understand why you want the relative path. You can just type "../../Content/TheRestOfYourPath". Are you saying sometimes you want it to emit 3 .. like this "../../../Content"? If so, that's usually when you use absolute paths.
Steve Michelotti
The problem is that the img src will be /Content/Files/AdImages/20/20_thumb.jpeg and this is when the url looks somthing like this : http://localhost/Ad/List (not in the root as you can see). I Supose that the src should be translated to somthing like ../../Content/Files/AdImages/20/20_thumb.jpeg to get the correct path from the view to the image. This should not be hardcoded becourse the helper could be used on diffrent pages at diffrent places. In ASP.NET there was Control.ResolveUrl() to get the relative path, how do I do the same in MVC?
SnowJim
The thing to keep in mind is that the paradigm of MVC is fundamentally different than web forms specifically in this area. In MVC, routes to not map to physical paths for views like they do in web forms where you're typically serving up a specific *.aspx page. For static content (e.g., images, css, etc.) they *do* point to physical assets - but making that relative to an MVC view will be problematic for these reasons.
Steve Michelotti
Okay? Så its not possible to point to a img resrouse as I described, how do I do then? My file exists her ~/Content/Files/AdImages/20/20_thumb.jpeg, how can I chow this file in a img element?
SnowJim
<img src="<%=Url.Content("~/Content/Files/AdImages/20/20_thumb.jpeg")%>"/> or create an Html helper like Walther does in the link you posted.
Steve Michelotti
<img alt="temp" src="<%=Url.Content("~/Content/Files/AdImages/20/20_thumb.jpeg")%>" /> will generate <img src="/Content/Files/AdImages/20/20_thumb.jpeg" alt="temp"> .The Image can´t be found here? And If I try de HTML helper(Walthers) I will get the same result? The only thing that works if I put this manually ../Content/Files/AdImages/20/20_thumb.jpeg. I dont see why this is not working?
SnowJim
I have no manage to get it to work. I am now using VirtualPathUtility.ToAbsolute("~/Content/Files/AdImages/20/20_thumb.jpeg"). This will generate /Biss/Content/Files/AdImages/22/22_thumb.jpeg. Is this right way to do it?
SnowJim