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);

 

posted @ 2019-10-19 19:29  星空天宇  阅读(205)  评论(0编辑  收藏  举报