Url helper for full url in asp.net mvc-3

前端 未结 4 1742
离开以前
离开以前 2021-02-07 01:52

Writing

@Url.Content(\"~/Something/Something.html\")

in razor renders

/AppFolder/Something/Something.html

I

4条回答
  •  误落风尘
    2021-02-07 02:16

    The @Url.RouteURL() does not quiet answer this question. It does work for named routes but falls short for arbitrary virtual paths. Here is quick helper method that generates full outbound url. You can create overloads for various schemes (http[s]) depending on the degree of control desired.

    public static class UrlHelperExtension
    {
        public static string ContentFullPath(this UrlHelper url,string virtualPath)
        {
            var result = string.Empty;
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
    
            result = string.Format("{0}://{1}{2}",
                                   requestUrl.Scheme,
                                   requestUrl.Authority, 
                                   VirtualPathUtility.ToAbsolute(virtualPath));
            return result;
        }
    }
    

提交回复
热议问题