tags:

views:

126

answers:

3

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.

Thanks.

A: 

I'm afraid I don't have a complete answer but I can hopefully point you in the right direction.

You can look at the source code for ASP.NET MVC and see how the Html.ActionLink helper is implemented in the "mega overload" that takes in a bunch of parameters. It has all the login in there to handle null values for missing parameters. Just look at all the code paths that handle the null values for missing host, port, protocol, and so forth. By combining it all you should be able to write your own method that produces a fully qualified URL.

I don't know of any built-in method in ASP.NET to do this (but I'm also not sure that there isn't one).

Eilon
A: 

Here is the answer for you http://stephenwalther.com/blog/archive/2009/02/18/asp.net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx Stephan Walther explain with example.

We followed the same link to solve our problem.

Happy coding

Ravia
It appears that 'goths' already knows how to get these kinds of URLs. The request was for a helper method that can create a fully qualified URL that has the protocol, hostname, and all the other data. Presumably this URL needs to be usable outside of a web document, such as in an email or a database where the hostname is required.
Eilon
The article suggests using VirtualPathUtility.ToAbsolute(contentPath, this.RequestContext.HttpContext.Request.ApplicationPath);But ApplicationPath is always "/". So it returns /images/abc.jpg instead of http:// mywebsite.com/images/abc.jpg Am I missing something?
goths
+2  A: 

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

Adeel
The above code returns "/". That's it. Not the domain name with protocol like http:// www.mywebsite /
goths
i have updated the answer. now it will fullfill the requirment
Adeel
Yes, Thanks. Reference: http://www.west-wind.com/Weblog/posts/154812.aspx
goths