Answers:
单击此处以获取更多信息,但是从本质上讲,不需要扩展方法。它已经烤熟了,只是不是以一种非常直观的方式。
Url.Action("Action", null, null, Request.Url.Scheme);
Request.Url.Scheme
因此,如果您使用HTTPS,则生成的URL也将使用HTTPS。
Html.ActionLink
(采用某种协议的任何方法,例如MVC 4中的最后2种)
扩展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;
}
}
}
支持信息:
<%= 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);
}
}
}
我不确定是否有内置方法可以执行此操作,但是您可以滚动自己的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"))%> »
查尔斯·HTH
结果相同,但更简洁(没有字符串连接/格式化):
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;
}