Getting Absolute URL fron an ASP.NET MVC Action

后端 未结 7 628
北恋
北恋 2020-12-07 19:37

This probably is a dummy question but I cannot find a clear indication. I have a POCO class in a MVC3 web application whose only purpose is managing the backup of some files

7条回答
  •  悲&欢浪女
    2020-12-07 20:30

    This works for me:

    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)
    

提交回复
热议问题