Get absolute path of file on content

前端 未结 5 545
轮回少年
轮回少年 2020-12-15 03:58

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


        
5条回答
  •  囚心锁ツ
    2020-12-15 04:24

    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
    

提交回复
热议问题