Getting full url of any file in ASP.Net MVC

Gautam Jain picture Gautam Jain · Jan 15, 2010 · Viewed 30.3k times · Source

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.

Answer

arni picture arni · Dec 29, 2013
new Uri(Request.Url, Url.Content("~/images/image1.gif"))