我認爲沒有內置的方法來做你需要的。你必須手動完成。例如象下面這樣:
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>
但是當我序列Url.Action()成XML的結果,只有純文本appeares: myController的/ MyAction。我如何將action方法返回的url傳遞給xml文件? – 2013-02-11 15:00:46
@AkseiChepovoi:你實際上在問如何獲取你的服務所暴露的域名。使用'HttpContext.Request.Url'屬性 – SLaks 2013-02-11 18:39:36
謝謝,我已經得到它 – 2013-02-11 19:14:17