我需要这样做:
<script type="text/javascript">
token_url = "http://example.com/your_token_url";
</script>
我正在使用MVC的Beta版本,但我不知道如何获得一个动作的绝对url。我想这样做:
<%= Url.AbsoluteAction("Action","Controller")) %>
是否有用于此的helper或Page方法?
我需要这样做:
<script type="text/javascript">
token_url = "http://example.com/your_token_url";
</script>
我正在使用MVC的Beta版本,但我不知道如何获得一个动作的绝对url。我想这样做:
<%= Url.AbsoluteAction("Action","Controller")) %>
是否有用于此的helper或Page方法?
当前回答
<%= Url.Action("About", "Home", null, Request.Url.Scheme) %>
<%= Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme) %>
其他回答
我不确定是否有一个内置的方法来做到这一点,但你可以滚动你自己的HtmlHelper方法。
如下所示
namespace System.Web.Mvc
{
public static class HtmlExtensions
{
public static string AbsoluteAction(this HtmlHelper html, string actionUrl)
{
Uri requestUrl = html.ViewContext.HttpContext.Request.Url;
string absoluteAction = string.Format("{0}://{1}{2}",
requestUrl.Scheme,
requestUrl.Authority,
actionUrl);
return absoluteAction;
}
}
}
然后这样叫它
<%= Html.AbsoluteAction(Url.Action("Dashboard", "Account"))%> »
HTHs, 查尔斯
Env: dotnet核心版本1.0.4
Url.Action("Join",null, null,Context.Request.IsHttps?"https":"http");
点击这里获取更多信息,但基本上不需要扩展方法。它已经存在了,只是不是以非常直观的方式。
Url.Action("Action", null, null, Request.Url.Scheme);
使用 @Charlino的回答作为指导,我想出了这个。
ASP。UrlHelper的NET MVC文档显示了Url。如果传入主机名和协议,Action将返回一个完全限定的Url。我创建这些帮助程序是为了强制提供主机名和协议。多重重载反映Url重载。行动:
using System.Web.Routing;
namespace System.Web.Mvc {
public static class HtmlExtensions {
public static string AbsoluteAction(this UrlHelper url, string actionName) {
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
return url.Action(actionName, null, (RouteValueDictionary)null,
requestUrl.Scheme, null);
}
public static string AbsoluteAction(this UrlHelper url, string actionName,
object routeValues) {
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
return url.Action(actionName, null, new RouteValueDictionary(routeValues),
requestUrl.Scheme, null);
}
public static string AbsoluteAction(this UrlHelper url, string actionName,
RouteValueDictionary routeValues) {
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
return url.Action(actionName, null, routeValues, requestUrl.Scheme, null);
}
public static string AbsoluteAction(this UrlHelper url, string actionName,
string controllerName) {
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
return url.Action(actionName, controllerName, (RouteValueDictionary)null,
requestUrl.Scheme, null);
}
public static string AbsoluteAction(this UrlHelper url, string actionName,
string controllerName,
object routeValues) {
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
return url.Action(actionName, controllerName,
new RouteValueDictionary(routeValues), requestUrl.Scheme,
null);
}
public static string AbsoluteAction(this UrlHelper url, string actionName,
string controllerName,
RouteValueDictionary routeValues) {
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
return url.Action(actionName, controllerName, routeValues, requestUrl.Scheme,
null);
}
public static string AbsoluteAction(this UrlHelper url, string actionName,
string controllerName, object routeValues,
string protocol) {
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
return url.Action(actionName, controllerName,
new RouteValueDictionary(routeValues), protocol, null);
}
}
}
相同的结果,但稍微干净一点(没有字符串连接/格式化):
public static Uri GetBaseUrl(this UrlHelper url)
{
Uri contextUri = new Uri(url.RequestContext.HttpContext.Request.Url, url.RequestContext.HttpContext.Request.RawUrl);
UriBuilder realmUri = new UriBuilder(contextUri) { Path = url.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
return realmUri.Uri;
}
public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName)
{
return new Uri(GetBaseUrl(url), url.Action(actionName, controllerName)).AbsoluteUri;
}