定义一个类,写一个扩展方法,需要引入 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);