Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

asp.net mvc - 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...