Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get absolute path of file on content

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

like image 819
mosquito87 Avatar asked Apr 18 '13 13:04

mosquito87


People also ask

How do I find the absolute path of a file?

The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object.

How do you get the absolute path of a file in Linux?

To obtain the full path of a file, we use the readlink command. readlink prints the absolute path of a symbolic link, but as a side-effect, it also prints the absolute path for a relative path. In the case of the first command, readlink resolves the relative path of foo/ to the absolute path of /home/example/foo/.

Which command displays absolute path for specific file?

The pwd command displays the full, absolute path of the current, or working, directory.

How do I get the full path of a file in bash?

In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.


4 Answers

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
like image 161
Jeff Tian Avatar answered Oct 17 '22 22:10

Jeff Tian


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")
like image 20
James Sampica Avatar answered Oct 17 '22 22:10

James Sampica


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.

like image 5
ZippyV Avatar answered Oct 17 '22 22:10

ZippyV


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.

like image 3
juFo Avatar answered Oct 17 '22 22:10

juFo