我需要这样做:

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

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

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

是否有用于此的helper或Page方法?


当前回答

扩展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仍然可以验证动作和控制器是否存在。这里有个例子,你可以得到同样的行为。

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) %>

我不确定是否有一个内置的方法来做到这一点,但你可以滚动你自己的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, 查尔斯

扩展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仍然可以验证动作和控制器是否存在。这里有个例子,你可以得到同样的行为。

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

Env: dotnet核心版本1.0.4

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

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

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