如何在ASP.NET MVC中查找动作的绝对URL?

我需要做这样的事情:

<script type="text/javascript"> token_url = "http://example.com/your_token_url"; </script> 

我正在使用MVC的Beta版本,但我不知道如何获得一个行动的绝对url。 我想要做这样的事情:

 <%= Url.AbsoluteAction("Action","Controller")) %> 

有这样的帮手或页面方法吗?

点击这里获取更多信息,但本质上不需要扩展方法。 它已经被烘烤了,只是不是非常直观的方式。

 Url.Action("Action", null, null, Request.Url.Scheme); 

扩展UrlHelper

 namespace System.Web.Mvc { public static class HtmlExtensions { public static string AbsoluteAction(this UrlHelper url, string action, string controller) { Uri requestUrl = url.RequestContext.HttpContext.Request.Url; string absoluteAction = string.Format( "{0}://{1}{2}", requestUrl.Scheme, requestUrl.Authority, url.Action(action, controller)); return absoluteAction; } } } 

然后像这样调用它

 <%= Url.AbsoluteAction("Dashboard", "Account")%> 

编辑 – RESHARPER注释

对接受的答案的最高评价是This answer is the better one, this way Resharper can still validate that the Action and Controller exists. 所以这里是一个例子,你可以得到相同的行为。

 using JetBrains.Annotations namespace System.Web.Mvc { public static class HtmlExtensions { public static string AbsoluteAction( this UrlHelper url, [AspMvcAction] string action, [AspMvcController] string controller) { Uri requestUrl = url.RequestContext.HttpContext.Request.Url; string absoluteAction = string.Format( "{0}://{1}{2}", requestUrl.Scheme, requestUrl.Authority, url.Action(action, controller)); return absoluteAction; } } } 

支持信息:

  • 为ASP.NET MVC中的自定义助手提供智能感知,导航等等
 <%= Url.Action("About", "Home", null, Request.Url.Scheme) %> <%= Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme) %> 

我以@Charlino的回答为指导,提出了这个问题。

UrlHelper的ASP.NET MVC文档显示,如果传入主机名和协议,Url.Action将返回完全限定的Url。我创build了这些帮助程序来强制提供主机名和协议。 多个重载镜像Url.Action的重载:

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

我不确定是否有内置的方法来实现,但是您可以推出自己的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

相同的结果,但有点清洁(没有string连接/格式):

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

也许这(?):

 <%= Request.Url.GetLeftPart(UriPartial.Authority) + Url.Action("Action1", "Controller2", new {param1="bla", param2="blabla" }) %> 

env:dotnet核心版本1.0.4

 Url.Action("Join",null, null,Context.Request.IsHttps?"https":"http");