asp.net core 获取当前请求的url

定义一个类,写一个扩展方法,需要引入 Microsoft.AspNetCore.Http.Abstractions包

public static class HttpRequestExtensions
    {
        public static string GetAbsoluteUri(this HttpRequest request)
        {
            return new StringBuilder()
             .Append(request.Scheme)
             .Append("://")
             .Append(request.Host)
             .Append(request.PathBase)
             .Append(request.Path)
             .Append(request.QueryString)
             .ToString();
        }
    }

使用:

 var url = HttpRequestExtensions.GetAbsoluteUri(Request);
原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878879.html