有没有办法可以访问 ASP Core 2.0 中间件中的当前请求 URL?
有什么我可以注入(inject)的吗?
最佳答案
HttpContext 对象将被传递给 Invoke
中间件的方法。您可以访问 Request
属性(property)。
您可以使用 GetDisplayUrl
扩展方法或GetEncodedUrl
扩展方法。
public Task Invoke(HttpContext context)
{
var url1 =context.Request.GetDisplayUrl();
var url2 = context.Request.GetEncodedUrl();
// Call the next delegate/middleware in the pipeline
return this._next(context);
}
这两种扩展方法在
Microsoft.AspNetCore.Http.Extensions
中定义。命名空间。因此,请确保您有一个 using 语句来包含命名空间using Microsoft.AspNetCore.Http.Extensions;
关于asp.net-core - ASP 核心中间件中的当前 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45572798/