Get raw URL from Microsoft.AspNet.Http.HttpRequest

后端 未结 8 712
醉酒成梦
醉酒成梦 2020-12-02 21:40

The HttpRequest class in Asp.Net 5 (vNext) contains (amongst other things) parsed details about the URL for the request, such as Scheme, Host

相关标签:
8条回答
  • 2020-12-02 22:14

    This extension works for me:

    using Microsoft.AspNetCore.Http;

        public static class HttpRequestExtensions
        {
            public static string GetRawUrl(this HttpRequest request)
            {
                var httpContext = request.HttpContext;
                return $"{httpContext.Request.Scheme}://{httpContext.Request.Host}{httpContext.Request.Path}{httpContext.Request.QueryString}";
            }
        }
    
    0 讨论(0)
  • 2020-12-02 22:15

    The following extension method reproduces the logic from the pre-beta5 UriHelper:

    public static string RawUrl(this HttpRequest request) {
        if (string.IsNullOrEmpty(request.Scheme)) {
            throw new InvalidOperationException("Missing Scheme");
        }
        if (!request.Host.HasValue) {
            throw new InvalidOperationException("Missing Host");
        }
        string path = (request.PathBase.HasValue || request.Path.HasValue) ? (request.PathBase + request.Path).ToString() : "/";
        return request.Scheme + "://" + request.Host + path + request.QueryString;
    }
    
    0 讨论(0)
  • 2020-12-02 22:18

    In .NET Core razor:

    @using Microsoft.AspNetCore.Http.Extensions
    @Context.Request.GetEncodedUrl() //Use for any purpose (encoded for safe automation)
    

    You can also use instead of the second line:

    @Context.Request.GetDisplayUrl() //Use to display the URL only
    
    0 讨论(0)
  • 2020-12-02 22:22

    Add the Nuget package / using:

    using Microsoft.AspNetCore.Http.Extensions; 
    

    (In ASP.NET Core RC1 this was in Microsoft.AspNet.Http.Extensions)

    then you can get the full http request url by executing:

    var url = httpContext.Request.GetEncodedUrl();
    

    or

    var url = httpContext.Request.GetDisplayUrl();
    

    depending on the purposes.

    0 讨论(0)
  • 2020-12-02 22:25

    The other solutions did not fit well my needs because I wanted directly an URI object and I think it is better to avoid string concatenation (also) in this case so I created this extension methods than use a UriBuilder and works also with urls like http://localhost:2050:

    public static Uri GetUri(this HttpRequest request)
    {
        var uriBuilder = new UriBuilder
        {
            Scheme = request.Scheme,
            Host = request.Host.Host,
            Port = request.Host.Port.GetValueOrDefault(80),
            Path = request.Path.ToString(),
            Query = request.QueryString.ToString()
        };
        return uriBuilder.Uri;
    }
    
    0 讨论(0)
  • 2020-12-02 22:31

    In ASP.NET 5 beta5:

    Microsoft.AspNet.Http.Extensions.UriHelper.Encode(
        request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
    
    0 讨论(0)
提交回复
热议问题