简体   繁体   English

如何在 ASP.NET 核心 Razor 页面中获得完整的 URL?

[英]How to get FULL URL in ASP.NET Core Razor Pages?

I want to get the full URL , not just the Path , not just the Query , and not RouteValues .我想获得完整的 URL ,而不仅仅是Path ,不仅仅是Query ,而不是RouteValues

The entire URL as it has come in the raw form.整个 URL 以原始形式出现。

How can I do that in ASP.NET Core Razor Pages?如何在 ASP.NET 核心 Razor 页面中做到这一点?

You can use the PageLink method of IUrlHelper to get the absolute URL to a page.您可以使用 IUrlHelper 的PageLink方法获取绝对的IUrlHelper到一个页面。

In the page handler, IUrlHelper can be accessed via the Url property:在页面处理程序中,可以通过IUrlHelper属性访问Url

public async Task<IActionResult> OnPostAsync()
{
    string url = Url.PageLink("/PageName", "PageHandler", routeValues);
    ...
}

If you want to generate a URL to a controller action, use ActionLink .如果要生成 URL 到 controller 操作,请使用ActionLink

You can use the UriHelper extension methods GetDisplayUrl() or GetEncodedUrl() to get the full URL from the request.您可以使用UriHelper扩展方法GetDisplayUrl()GetEncodedUrl()从请求中获取完整的 URL。

GetDisplayUrl() GetDisplayUrl()

Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString) suitable only for display.以仅适用于显示的完全未转义形式(QueryString 除外)返回请求 URL 的组合组件。 This format should not be used in HTTP headers or other HTTP operations.此格式不应用于 HTTP 标头或其他 HTTP 操作。

GetEncodedUrl() GetEncodedUrl()

Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers and other HTTP operations.以适用于 HTTP 标头和其他 HTTP 操作的完全转义形式返回请求 URL 的组合组件。

Usage:用法:

using Microsoft.AspNet.Http.Extensions;
...
string url = HttpContext.Request.GetDisplayUrl();
// or
string url = HttpContext.Request.GetEncodedUrl();

You could create a extension class to use the IHttpContextAccessor interface to get the HttpContext.您可以创建一个扩展 class 来使用 IHttpContextAccessor 接口来获取 HttpContext。 Once you have the context, then you can get the HttpRequest instance from HttpContext.Request and use its properties Scheme, Host, Protocol etc as in:获得上下文后,您可以从 HttpContext.Request 获取 HttpRequest 实例并使用其属性 Scheme、Host、Protocol 等,如下所示:

string scheme = HttpContextAccessor.HttpContext.Request.Scheme;

For example, you could require your class to be configured with an HttpContextAccessor:例如,您可能需要为 class 配置 HttpContextAccessor:

public static class UrlHelperExtensions
{        
  private static IHttpContextAccessor HttpContextAccessor;
  public static void Configure(IHttpContextAccessor httpContextAccessor)
  {           
      HttpContextAccessor = httpContextAccessor;  
  }

  public static string AbsoluteAction(
      this IUrlHelper url,
      string actionName, 
      string controllerName, 
      object routeValues = null)
  {
      string scheme = HttpContextAccessor.HttpContext.Request.Scheme;
      return url.Action(actionName, controllerName, routeValues, scheme);
  }
  ....
}

Which is something you can do on your Startup class (Startup.cs file):您可以在 Startup class(Startup.cs 文件)上执行以下操作:

public void Configure(IApplicationBuilder app)
{
    ...

    var httpContextAccessor = 
        app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
        UrlHelperExtensions.Configure(httpContextAccessor);

    ...
}

You could probably come up with different ways of getting the IHttpContextAccessor in your extension class, but if you want to keep your methods as extension methods in the end you will need to inject the IHttpContextAccessor into your static class.您可能会想出不同的方法在您的扩展 class 中获取 IHttpContextAccessor,但如果您希望最终将您的方法保留为扩展方法,您需要将 IHttpContextAccessor 注入您的 static ZA2F2ED4F8EBC2CBB4C2A 中。 (Otherwise you will need the IHttpContext as an argument on each call) (否则,您将需要 IHttpContext 作为每次调用的参数)

You can try to use HttpContext.Request.Scheme + HttpContext.Request.Host to get https://localhost:xxxx ,then use HttpContext.Request.Path + HttpContext.Request.QueryString to get path and query:您可以尝试使用HttpContext.Request.Scheme + HttpContext.Request.Host获取https://localhost:xxxx ,然后使用HttpContext.Request.Path + HttpContext.Request.QueryString获取路径和查询:

var request = HttpContext.Request;
var _baseURL = $"{request.Scheme}://{request.Host}";
var fullUrl = _baseURL+HttpContext.Request.Path + HttpContext.Request.QueryString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM