无法访问 .net 标准中的 httpcontext 扩展方法

Aym*_*udi 3 c# asp.net-core .net-standard asp.net-core-3.1

我有这个 .NET Standard 库,我想在其中编写一个 .NET Core 中间件。

在里面我想做:

Endpoint endpoint = httpContext.GetEndpoint();
Run Code Online (Sandbox Code Playgroud)

GetEndpoint()扩展方法不能得到解决。

我已经引用Microsoft.AspNetCore.Http并且我已经将Microsoft.AspNetCore.Http.AbstractionsMicrosoft.AspNetCore.Mvc.Core包添加到项目中。

有没有办法解决这个问题,我错过了什么吗?

crg*_*den 9

我假设您正在为 ASP.NET Core 3.1 编写中间件,因为您包含了“asp.net-core-3.1”标签。

要使用该扩展,您需要定位netcoreapp3.*而不是netstandard2.*

<TargetFramework>netcoreapp3.1</TargetFramework>
Run Code Online (Sandbox Code Playgroud)

(您可以在文档页面的下拉菜单中查看该扩展适用于哪些 ASP.NET Core 版本)

您还需要:

  1. 使用Microsoft.NET.Sdk.WebMSBuild SDK:
    <Project Sdk="Microsoft.NET.Sdk.Web">
    
    Run Code Online (Sandbox Code Playgroud)
  2. 或添加框架参考:
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    
    Run Code Online (Sandbox Code Playgroud)

参考:在类库中使用 ASP.NET Core API