我需要做这样的事情:
我正在使用MVC的Beta版本,但我无法弄清楚如何获取动作的绝对URL.我想做这样的事情:
<%= Url.AbsoluteAction("Action","Controller")) %>
是否有帮助器或Page方法?
单击此处获取更多信息,但实际上不需要扩展方法.它已经烘焙过,而不是以非常直观的方式.
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")%>
编辑 - 再生注释
对于已接受的答案,最受欢迎的评论是,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.我创建了这些帮助程序以强制提供主机名和协议.多次重载反映了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); } } }