asp.net-mvc 如何在 ASP.NET MVC 中找到操作的绝对 url?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/434604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:16:23  来源:igfitidea点击:

How do I find the absolute url of an action in ASP.NET MVC?

asp.net-mvc

提问by Mike Comstock

I need to do something like this:

我需要做这样的事情:

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

I'm using the Beta version of MVC, but I can't figure out how to get the absolute url of an action. I'd like to do something like this:

我正在使用 MVC 的 Beta 版本,但我不知道如何获取操作的绝对 url。我想做这样的事情:

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

Is there a helper or Page method for this?

是否有帮助程序或 Page 方法?

回答by Adam Boddington

Click herefor more information, but esentially there is no need for extension methods. It's already baked in, just not in a very intuitive way.

单击此处了解更多信息,但基本上不需要扩展方法。它已经完成了,只是不是以一种非常直观的方式。

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

回答by Charlino

Extend the UrlHelper

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

Then call it like this

然后这样调用

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

EDIT - RESHARPER ANNOTATIONS

编辑 - RESHARPER 注释

The most upvoted comment on the accepted answer is This answer is the better one, this way Resharper can still validate that the Action and Controller exists.So here is an example how you could get the same behaviour.

对接受的答案最受好评的评论是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;
        }
    }
}

Supporting info:

支持信息:

回答by Ryan Sampson

<%= Url.Action("About", "Home", null, Request.Url.Scheme) %>
<%= Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme) %>

回答by Raleigh Buckner

Using @Charlino 's answer as a guide, I came up with this.

使用 @Charlino 的答案作为指导,我想出了这个。

The ASP.NET MVC documentation for UrlHelpershows that Url.Action will return a fully-qualified Url if a hostname and protocol are passed in. I created these helpers to force the hostname and protocol to be provided. The multiple overloads mirror the overloads for Url.Action:

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

    }
}

回答by Charlino

I'm not sure if there is a built in way to do it, but you could roll your own HtmlHelper method.

我不确定是否有内置的方法来做到这一点,但您可以推出自己的 HtmlHelper 方法。

Something like the following

类似于以下内容

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

Then call it like this

然后这样调用

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

HTHs, Charles

HTH,查尔斯

回答by Dashrath

Complete answer with arguments would be :

带参数的完整答案是:

var url = Url.Action("ActionName", "ControllerName", new { id = "arg_value" }, Request.Url.Scheme);

and that will produce an absolute url

这将产生一个绝对网址

回答by veggerby

Same result but a little cleaner (no string concatenation/formatting):

相同的结果,但更简洁(没有字符串连接/格式):

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

回答by tytusse

Maybe this (?):

也许这个(?):

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

回答by guhyeon

env: dotnet core version 1.0.4

环境:dotnet 核心版本 1.0.4

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