Reputation: 39364
I have the following on an Asp.Net MVC 5 site:
@Url.Content("~/shareimage.jpg")
This gives me a relative url. But I need the absolute url.
How can I get the absolute url of a file?
thank You, Miguel
Upvotes: 0
Views: 906
Reputation: 549
Url.Action or url.RouteUrl where a protocol is specified gives you the absolute url.
<%= Url.Action("About", "Home", null, "http") %><br />
<%= Url.RouteUrl("Default", new { Action = "About" }, "http") %><br />
http://captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/
Upvotes: 2
Reputation: 74227
This is a duplicate of this question: asp.net mvc image path and virtual directory.
I use this helper myself:
public static class Helpers
{
public static Uri FullyQualifiedUri( this HtmlHelper html , string relativeOrAbsolutePath )
{
Uri baseUri = HttpContext.Current.Request.Url ;
string path = UrlHelper.GenerateContentUrl( relativeOrAbsolutePath, new HttpContextWrapper( HttpContext.Current ) ) ;
Uri instance = null ;
bool ok = Uri.TryCreate( baseUri , path , out instance ) ;
return instance ; // instance will be null if the uri could not be created
}
}
which should give you a fully-qualifed absolute URI for pretty much any URI your throw at it.
Upvotes: 2