5

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84

1 Answers1

16

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

crgolden
  • 4,332
  • 1
  • 22
  • 40
  • That's what I've just realized, those extensions aren't available in .Net standard, I had to target core 3.1 Complete answer, accepting, upvoting. – AymenDaoudi May 17 '20 at 08:12
  • +1. This answer is also useful when trying to use GetEnpoint in a class library (as opposed to using it in a Web API project). Unfortunately, simply adding the Microsoft.AspNetCore.Http.Abstractions as indicated [here](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.endpointhttpcontextextensions.getendpoint?view=aspnetcore-3.1) is not enough. – Alexei - check Codidact Dec 06 '20 at 08:56
  • "With the release of .NET Core 3.0, many ASP.NET Core assemblies are no longer published to NuGet as packages. Projects using the Microsoft.NET.Sdk or Microsoft.NET.Sdk.Razor SDK must reference ASP.NET Core to use ASP.NET Core APIs in the shared framework." Quote from Microsoft(https://learn.microsoft.com/en-us/aspnet/core/fundamentals/target-aspnetcore?view=aspnetcore-5.0&tabs=visual-studio#use-the-aspnet-core-shared-framework) Add following lines to your .csproj file – erhan355 Dec 28 '20 at 20:47