28

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...

6

5 Answers 5

43

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Note, that, HttpContext.Current does not exist in Core. Use urlHelper.RequestContext.HttpContext
I would suggest not making the last parameter defaulted/optional. There is already a UrlHelper.Content instance method that takes a single string parameter.
To get this to work on ASP.NET Core 3.1, I the helper had to extend the interface: (this IUrlHelper urlHelper)
17

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") 

1 Comment

Thank you, I've been look for a good solution for the last hour. and yours works great.
6

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.

1 Comment

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"))
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.

Comments

0
HttpContext.Current.Server.MapPath("~/Content/images/logo.png"); 

3 Comments

The output of this one is: C:\Users\...\Projects\...\Content\images\logo.png What I really need is: localhost:57001/Content/images/logo.png (in development environment) and mydomain.com/Content/images/logo.png (in productive environment)
@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.
Dang, you got him there: He relied on the context of people reading the question knowing what web development was.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.