2

I'm using Url.Content("~/something.jpg") to get an absolute path in a view without problem.

I would like to use it in a controller but it doesn't work. It returns "/something.jpg" instead of "http://www.mydomain.com/something.jpg".

I tried several tricks found on google and stackoverflow without success.

Why Url.Content() doesn't work in a controller ?

Any idea will be helpful.

Thank you

5
  • 1
    the tilde ~ will point to the route of the site, so URL.Content() is doing what's expected. Can you post the code you're using in the controller? Commented May 22, 2014 at 13:44
  • I only do that as the first instruction : String sUrlPublication = Url.Content("~/Publication/PublicationXML/"); and it returns : "/Publication/PublicationXML/" without the route of the site Commented May 22, 2014 at 13:48
  • @user3463579: What makes you think this is incorrect behavior? If the application is in the root of the domain then ~ would resolve to /. Commented May 22, 2014 at 13:53
  • Because i get "mydomain.com/Publication/PublicationXML" with exactly the same request from a view. I probably misunderstood something but Url.content() doesn't have the same behaviour between a view or a controller ? Commented May 22, 2014 at 14:01
  • 1
    possible duplicate of How to Generate absolute urls with https in MVC3? Commented May 22, 2014 at 14:18

3 Answers 3

2

I finally found what i'm looking for by using new Uri(Request.Url, Url.Content("~/something.jpg"));. It returns :

http://www.mydomain.com/something.jpg;

Using only Url.Content() in a controller seems to be not enough. We have to use the Uri class to have the full URL.

Anyway, thank you everybody for your help

Sign up to request clarification or add additional context in comments.

1 Comment

This helped me a lot!
1

I am not sure why this is not working in controller probably because Url.Content is an mvc helper but I now how you can do what you want.If you want to get absolute path to your file you can try something like this:

var url=Path.Combine(Server.MapPath("../uploads"), "something.jpg");

In conclusion I think that what you need is Server.MapPath()

1 Comment

It returns the path on my hard drive ("d:\something.jpg") and not what I'm looking for : the full URL ("www.mydomain.com/something.jpg").
0

You could have a config entry that holds your URL, and append that to the front...

var sUrlPublication = ConfigHelper.SiteUrl + Url.Content("~/....");

Add the URL to AppSettings...

<appSettings>
  <add key="SiteURL" value="...."/>
</appSettings>

You could add a ConfigHelper class...

using System.Configuration;

public static class ConfigHelper
{
  public static string SiteUrl
    {
      get { return ConfigurationManager.AppSettings["SiteURL"]; }
    }
}

EDIT: Or as mentioned by Robert

var sUrlPublication = Request.Url.Host  + Url.Content("~/....");

3 Comments

or look at the Request object to find the domain the request is coming in on dynamically
@RobertLevy, as in using the ServerVariables?
No I mean just use Request.Url.Host

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.