Can't access httpcontext extension methods in .net standard

你离开我真会死。 提交于 2021-02-17 02:01:49

问题


I have this .NET Standard library where I want to write a .NET Core middleware.

Inside which I want to do :

Endpoint endpoint = httpContext.GetEndpoint();

The GetEndpoint() extension method can't be resolved.

I have referenced Microsoft.AspNetCore.Http and I have both Microsoft.AspNetCore.Http.Abstractions and Microsoft.AspNetCore.Mvc.Core packages added to the project.

Is there a solution to this, am I missing something?


回答1:


I assume you're writing a middleware for ASP.NET Core 3.1 since you included the "asp.net-core-3.1" tag.

To use that extension, you need to target netcoreapp3.* instead of netstandard2.*:

<TargetFramework>netcoreapp3.1</TargetFramework>

(You can see which ASP.NET Core versions that extension is available for in the dropdown menu on the documentation page)

You will also need to either:

  1. use the Microsoft.NET.Sdk.Web MSBuild SDK:
    <Project Sdk="Microsoft.NET.Sdk.Web">
    
  2. or add the framework reference:
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    

Reference: Use ASP.NET Core APIs in a class library



来源:https://stackoverflow.com/questions/61847821/cant-access-httpcontext-extension-methods-in-net-standard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!