我认为没有内置的方式来做你需要的。您必须手动完成。例如如下:
public static class UrlExtension
{
public static string ToAbsoluteUrl(this string relativeUrl, HttpContext httpContext)
{
string http = "http" + (httpContext.Request.IsSecureConnection ? "s" : string.Empty);
string host = httpContext.Request.Url.Host;
string port = httpContext.Request.Url.Port == 80 ? string.Empty : string.Format(":{0}", httpContext.Request.Url.Port);
return string.Format("{0}://{1}{2}{3}", http, host, port, relativeUrl);
}
}
例子:
<a href="@Url.Action("Index", "Home").ToAbsoluteUrl(HttpContext.Current)">Index</a>
<a href="@Url.Action("TestAction", "Home").ToAbsoluteUrl(HttpContext.Current)">TestAction</a>
渲染结果:
<a href="http://localhost/">Index</a>
<a href="http://localhost/Home/TestAction">TestAction</a>