asp.net-core - ASP 核心中间件中的当前 URL?

标签 asp.net-core asp.net-core-mvc url-routing asp.net-mvc-routing

有没有办法可以访问 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/

相关文章:

c# - 身份验证异常 : AuthenticationMechanismTooWeak: 5. 7.14

asp.net-core - 如何在不提交的情况下触发 ASP .NET Core MVC 客户端验证

c# - 为什么 MyService 中的 DbContext (ASP.NET) 为空?

javascript - 如何在最后加载$scope.$?

asp.net - ASP.NET URL重写

debugging - 使用 ASP.Net Core 在 VSCode 中调试 Typescript

c# - 解析 nlog.config 时出现 NLog 异常 - 找不到目标 : 'EventLog'

c# - 在 asp.net core mvc 中创建实体列表

razor - ASP.NET Core - 像 MVC 一样的脚手架标识

ajax - CRUD中的R-功能和披露漏洞之间的界线在哪里?