How to get FULL URL in ASP.NET Core Razor Pages?
Asked Answered
P

5

16

I want to get the full URL, not just the Path, not just the Query, and not RouteValues.

The entire URL as it has come in the raw form.

How can I do that in ASP.NET Core Razor Pages?

Panicstricken answered 26/12, 2021 at 8:52 Comment(0)
W
20

You can use the UriHelper extension methods GetDisplayUrl() or GetEncodedUrl() to get the full URL from the request.

GetDisplayUrl()

Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString) suitable only for display. This format should not be used in HTTP headers or other HTTP operations.

GetEncodedUrl()

Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers and other HTTP operations.

Usage:

using Microsoft.AspNetCore.Http.Extensions;
...
string url = HttpContext.Request.GetDisplayUrl();
// or
string url = HttpContext.Request.GetEncodedUrl();
Westernism answered 26/12, 2021 at 12:29 Comment(5)
Working on .Net 6, get the full URL within Razor Page cshtml <meta property="og:url" content="@(ViewContext.HttpContext.Request.GetDisplayUrl())">Elbring
doesn't work or even existDecorative
@Decorative not sure what you mean, it definitely exists - check the linked docs. This Q&A applies to Razor pagesWesternism
No, it does not anymoreDecorative
@Decorative It still exists in .NET 8. You're probably missing the using.Nerve
A
12

You can do it by this way. In .net core

@using Microsoft.AspNetCore.Http
@{
    string url = Context.Request.Path;
}
Assonance answered 1/12, 2022 at 16:8 Comment(3)
Thank you! Core is always a bit different. Appreciated.Theola
Depending on your hosting situation, developers should take some caution using this and similar methods because of how the path is coming from the request context, which is allegedly derived from the Host header. See RouteUrl docs (Remarks section) about the issue (even though not the same class/property. But I am fairly certain it is a universal concern when using the HTTP context to derive absolute URL information.Dodds
Well in this is wrong in .NET 6. Context isn't even a member of AspNetCore.HttpSprage
S
8

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:

var request = HttpContext.Request;
var _baseURL = $"{request.Scheme}://{request.Host}";
var fullUrl = _baseURL+HttpContext.Request.Path + HttpContext.Request.QueryString;
Skardol answered 27/12, 2021 at 2:7 Comment(1)
For some reason I was skeptical that the Host property included the port number, but Yiyi is spot on, it does indeed include the port.Foreskin
C
6

You can use the PageLink method of IUrlHelper to get the absolute URL to a page.

In the page handler (or controller), IUrlHelper can be accessed via the Url property:

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.

Works in ASP.NET Core 3.0 and above.

Cumquat answered 26/12, 2021 at 9:12 Comment(0)
M
0

You could create an extension class to use the IHttpContextAccessor interface to get the 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:

string scheme = HttpContextAccessor.HttpContext.Request.Scheme;

For example, you could require your class to be configured with an 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 in your Startup class (Startup.cs file):

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. (Otherwise, you will need the IHttpContext as an argument on each call).

Masuria answered 26/12, 2021 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.