0

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

1

2 Answers 2

2

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.

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

Comments

2

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/

3 Comments

That, I think, is for ASP.NET Forms not for MVC
Please, read my question ... I am using Url.Content but that gives me the relative path.
Yes, I know Action and RouteUrl have a protocol option but that is for action not for files ... And Url.Content does not have it ... That is the problem

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.