skip to Main Content

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?

5

Answers


  1. 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.

    Login or Signup to reply.
  2. 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();
    
    Login or Signup to reply.
  3. 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).

    Login or Signup to reply.
  4. 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;
    
    Login or Signup to reply.
  5. You can do it by this way. In .net core

    @using Microsoft.AspNetCore.Http
    @{
        string url = Context.Request.Path;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search