写作
@Url.Content("~/Something/Something.html")在razor渲染中
/AppFolder/Something/Something.html有没有一种方法可以像http://www.something.com/AppFolder/Something/Something.html一样呈现完整的网址,而不会遭到可怕的黑客攻击?(比如将协议和域存储在AppConfig中,并将字符串连接到其中)
有没有像@Url.FullPath("~/asdf/asdf")或类似的助手?
发布于 2011-11-08 17:19:48
@Url.RouteURL()并没有很好地回答这个问题。它确实适用于命名路由,但不适用于任意虚拟路径。下面是生成完整的出站url的快速助手方法。您可以根据所需的控制程度为各种方案(https)创建重载。
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;
}
}发布于 2011-10-12 03:30:57
有关答案,请参阅this blog post。
基本上,您需要做的全部工作包括协议参数,例如
Url.Action("About", "Home", null, "http")发布于 2017-07-03 19:22:07
对于任何需要在WebAPI 2.2和/或MVC5中构建URL的人来说,这对我很有效:
// works in a controller
var requestUri = this.Request.RequestUri;
// just the http/s and the hostname; ymmv
string baseUrl = requestUri.Scheme + "://" + requestUri.Authority + "/";
// build your url for whatever purpose you need it for
string url = baseUrl + "SomeOtherController?id=" + <some_magic_value>;https://stackoverflow.com/questions/7089915
复制相似问题