asp.net mvc-3中完整Url的Url帮助程序

asp.net mvc-3中完整Url的Url帮助程序,asp.net,urlhelper,asp.net-mvc-3,Asp.net,Urlhelper,Asp.net Mvc 3,书写 @Url.Content("~/Something/Something.html") 在剃刀渲染中 /AppFolder/Something/Something.html 有没有一种方法可以像http://www.something.com/AppFolder/Something/Something.html没有骇人听闻的黑客?(如将协议和域存储在AppConfig中,并将字符串连接到其中) 是否有类似于@Url.FullPath(“~/asdf/asdf”)或类似的帮助程序?请参阅以

书写

@Url.Content("~/Something/Something.html")
在剃刀渲染中

/AppFolder/Something/Something.html
有没有一种方法可以像
http://www.something.com/AppFolder/Something/Something.html
没有骇人听闻的黑客?(如将协议和域存储在
AppConfig
中,并将字符串连接到其中)

是否有类似于
@Url.FullPath(“~/asdf/asdf”)
或类似的帮助程序?

请参阅以获取答案

基本上,你需要做的就是包括协议参数,例如

Url.Action("About", "Home", null, "http")
@Url.RouteURL()不能安静地回答这个问题。它确实适用于命名路由,但不适用于任意虚拟路径。 下面是生成完整出站url的快速助手方法。根据所需的控制程度,可以为各种方案(http[s])创建重载

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;
    }
}

对于任何需要在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>;
//在控制器中工作
var requestUri=this.Request.requestUri;
//只有http/s和主机名;ymmv
字符串baseUrl=requestUri.Scheme+“:/”+requestUri.Authority+“/”;
//为任何你需要的目的建立你的url
字符串url=baseUrl+“SomeOtherController?id=“+”;

您可以使用帮助器生成完整的url,包括协议。注意
url.Action
中的第一个小写字母

var url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
var fullUrl = url.Action("YourAction", "YourController", new { id = something }, protocol: System.Web.HttpContext.Current.Request.Url.Scheme);
输出


https://www.yourdomain.com/YourController/YourAction?id=something

你找到答案了吗?我在找同样的东西!感谢您的帮助。有没有关于在Mono中使用Url.Action的建议?那是个老问题!您可能会因此得到一个徽章:d这是一个比使用
String.Format
更好的答案+1.