30

I want to generate complete Url (with domain name etc) of any file in MVC. Example: A .jpg file or an exe file.

Example: If I give "~/images/abc.jpg" it should return "http://www.mywebsite.com/images/abc.jpg"

I am aware of the Url.Action overload that takes the protocol as a parameter. But Url.Action can be used only for Actions.

I want something like Url.Content function that takes protocol as a parameter.

Do you know if any method to get complete url of any file?

I have tried: VirtualPathUtility.ToAbsolute, ResolveClientUrl, ResolveUrl but all of these don't seem to work.

3 Answers 3

59
new Uri(Request.Url, Url.Content("~/images/image1.gif"))
Sign up to request clarification or add additional context in comments.

5 Comments

I'm not sure which is the most efficient way, but I find this the easiest method to use and remember.
this should be the answer!! it is simple and done by default by NET so it is the best on flexibiity and maintainability
It's beautiful but it didn't work for me.. I still get something like main/images/load.png the domain is missing.
How would you use this in a view? I tried assigning it to a javascript variable and got a 500 error.
@RobertSmith You probably need to use Uri.AbsoluteUri to get a string.
51

You can use the following code to replace "~/" to absoulute URL.

System.Web.VirtualPathUtility.ToAbsolute("~/")

Edit:

First you need to define a method.

public static string ResolveServerUrl(string serverUrl, bool forceHttps)
{
    if (serverUrl.IndexOf("://") > -1)
        return serverUrl;

    string newUrl = serverUrl;
    Uri originalUri = System.Web.HttpContext.Current.Request.Url;
    newUrl = (forceHttps ? "https" : originalUri.Scheme) +
        "://" + originalUri.Authority + newUrl;
    return newUrl;
} 

Now call this method will return the complete absolure url.

ResolveServerUrl(VirtualPathUtility.ToAbsolute("~/images/image1.gif"),false))

The output will be http://www.yourdomainname.com/images/image1.gif

4 Comments

The above code returns "/". That's it. Not the domain name with protocol like http:// www.mywebsite /
i have updated the answer. now it will fullfill the requirment
Adding strings is not the cleanest way. You should use built-in classes like Uri or UriBuilder.
Excellent. I combined your code with the answer here stackoverflow.com/questions/5539156/… to provide an @Url.Absolute extension method. Thanks
-5

Try use this.

Url.Action("~/images/image1.gif", "/", null, Request.Url.Scheme)

2 Comments

OP talked about problems associated with this exact solution. The correct answer to this question was given 2 hours after the question was asked 3 years ago...
This one actually almost works but it puts in an extra path level if the image folder is at a higher level than the current view.

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.