How do I find the absolute url of an action in ASP.NET MVC?
我需要这样做:
1 2 | <script type="text/javascript"> token_url ="http://example.com/your_token_url"; |
我使用的是MVC的beta版本,但我不知道如何获取操作的绝对URL。我想这样做:
1 | <%= Url.AbsoluteAction("Action","Controller")) %> |
是否有帮助程序或页面方法?
单击此处获取更多信息,但本质上不需要扩展方法。它已经烤好了,只是不是很直观。
1 | Url.Action("Action", null, null, Request.Url.Scheme); |
延长URLhelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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; } } } |
那就这样说吧
1 | <%= Url.AbsoluteAction("Dashboard","Account")%> |
编辑-重新分析注释
对被接受的答案最乐观的评论是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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中的自定义帮助程序提供IntelliSense、导航等功能
1 2 | <%= Url.Action("About","Home", null, Request.Url.Scheme) %> <%= Url.RouteUrl("Default", new { Action ="About" }, Request.Url.Scheme) %> |
使用@查里诺作为向导的回答,我想出了这个。
urlhelper的ASP.NET MVC文档显示url.action将在传入主机名和协议时返回完全限定的url。我创建了这些帮助器来强制提供主机名和协议。多个重载镜像URL的重载。操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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方法。
像下面这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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; } } } |
那就这样说吧
1 | <%= Html.AbsoluteAction(Url.Action("Dashboard","Account"))%> ? |
HTHs查尔斯
带参数的完整答案是:
1 | var url = Url.Action("ActionName","ControllerName", new { id ="arg_value" }, Request.Url.Scheme); |
这将产生一个绝对的URL
结果相同,但更清晰(无字符串连接/格式):
1 2 3 4 5 6 7 8 9 10 11 | 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; } |
env: dotnet core version 1.0.4
1 | Url.Action("Join",null, null,Context.Request.IsHttps?"https":"http"); |
也许这个(?):
1 2 3 4 | <%= Request.Url.GetLeftPart(UriPartial.Authority) + Url.Action("Action1","Controller2", new {param1="bla", param2="blabla" }) %> |