27

Is there any easy (built in) way in an asp.net mvc view to get the absolute path of a file in the content folder?

At the moment I'm using

@Url.Content("~/Content/images/logo.png")

But the path returned isn't absolute.

I know it is possible to build its own helper for such cases but I'd like to know if there's any easier way...

mosquito87
  • 4,270
  • 11
  • 46
  • 77
  • http://stackoverflow.com/a/1268755/802791 – Mario S Apr 18 '13 at 13:21
  • possible duplicate of [ASP.NET MVC - Find Absolute Path to the App\_Data folder from Controller](http://stackoverflow.com/questions/1268738/asp-net-mvc-find-absolute-path-to-the-app-data-folder-from-controller) – AJ. Apr 18 '13 at 13:25
  • I have to check when the system is online as I use it for sending emails. Will let you know tomorrow. Thank you. – mosquito87 Apr 22 '13 at 15:40
  • `Url.Content("~/...")` does return the absolute path. – ZippyV Apr 24 '13 at 11:10
  • try this helper. it may help you. http://stackoverflow.com/questions/2069922/getting-full-url-of-any-file-in-asp-net-mvc – Idrees Khan Apr 24 '13 at 11:47
  • if I may make an observation - you're not looking for an absolute path - this path is already returned by Url.Content. I guess you're looking for a Url containiing domain name, optionally port and the path to the image. – tomalone Nov 06 '17 at 19:57

5 Answers5

42

This works for me:

A helper:

using System;
using System.Web;
using System.Web.Mvc;

public static class UrlExtensions
{
    public static string Content(this UrlHelper urlHelper, string contentPath, bool toAbsolute = false)
    {
        var path = urlHelper.Content(contentPath);
        var url = new Uri(HttpContext.Current.Request.Url, path);

        return toAbsolute ? url.AbsoluteUri : path;
    }
}

Usage in cshtml:

@Url.Content("~/Scripts/flot/jquery.flot.menuBar.js", true)

// example output:
// http://example.com/directory/Scripts/flot/jquery.flot.menuBar.js
T-moty
  • 2,679
  • 1
  • 26
  • 31
Jeff Tian
  • 5,210
  • 3
  • 51
  • 71
  • 1
    Note, that, `HttpContext.Current` does not exist in Core. Use `urlHelper.RequestContext.HttpContext` – Alex from Jitbit Feb 25 '19 at 15:36
  • 1
    I would suggest not making the last parameter defaulted/optional. There is already a UrlHelper.Content instance method that takes a single string parameter. – AaronLS May 16 '19 at 21:06
  • To get this to work on ASP.NET Core 3.1, I the helper had to extend the interface: (`this IUrlHelper urlHelper`) – Nicholas Betsworth Mar 23 '21 at 12:18
16

This will generate an absolute url to an image (or file)

Request.Url.Scheme + "://" + Request.Url.Authority + Url.Content("~/Content/images/logo.png")

This works in Asp.net Core

Context.Request.Scheme + "://" + Context.Request.Host + Url.Content("~/images/logo.png")
jamesSampica
  • 12,230
  • 3
  • 63
  • 85
5

Url.Content does return the absolute path. What you want is the domain (and port). You can get the current domain by using:

Request.Url.Authority

Then combine this string with the absolute path string of your image. It will return the domain name and if you are on a different port will also include the port number.

ZippyV
  • 12,540
  • 3
  • 37
  • 52
  • 2
    You also need to add the Scheme. Somethig like this: string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~/Content/images/logo.png")) – vfportero May 09 '14 at 08:39
3
new Uri(Request.Url, Url.Content("~/Content/images/logo.png"))

this calls the .ToString() of Uri. You can also put Uri in a variable and call .AbsoluteUri.

juFo
  • 17,849
  • 10
  • 105
  • 142
0
HttpContext.Current.Server.MapPath("~/Content/images/logo.png");
  • 1
    The output of this one is: C:\Users\...\Projects\...\Content\images\logo.png What I really need is: http://localhost:57001/Content/images/logo.png (in development environment) and http://www.mydomain.com/Content/images/logo.png (in productive environment) – mosquito87 Apr 23 '13 at 09:51
  • 3
    @mosquito87 From the way your question was written, it wasn't really obvious what you want, so the answer was correct in relation to what you wrote (though not in relation to what you had in mind). As such I don't believe it deserved all the down votes that it got. – jahu May 16 '14 at 13:04
  • 1
    Dang, you got him there: He relied on the context of people reading the question knowing what web development was. – Josiah Nov 13 '16 at 20:05