Daniel Thorne
Daniel Thorne

Reputation: 141

EndpointHttpContextExtensions: HttpContext class extensions (GetEndpoint) not found in .net5

I have been trying to use the extension method GetEndpoint() as detailed here:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.endpointhttpcontextextensions.getendpoint?view=aspnetcore-5.0

My project was initially targeting netstandard2.1 but then I read in the following post that this feature is for projects which target netcoreapp3.1.

Can't access httpcontext extension methods in .net standard

I do not want to target .Net Core 3.1 because the Entity Framework Core side of my project uses features which were delivered in the latest release, targeting .Net Standard 2.1.

So I tried targeting .Net 5 to see if it would appear, which it hasn't. I've also tried installing the package Microsoft.AspNetCore.Http.Abstractions but to no avail (and noted that this package targets netstandard2.0). I even tried changing the target framework down to netcoreapp3.1 and that didn't work. These extension methods are just not there.

Have I missed something or why are these methods not available in .Net 5 when they appear in the documentation?

Is there an alternative to using the GetEndpoint() extension method, if I can't get it to work?

My aim in all of this: I would like to use the following snippet in an AuthenticationHandler:

        var endpoint = Context.GetEndpoint();
        if (endpoint?.Metadata?.GetMetadata<IAllowAnonymous>() != null)
            return AuthenticateResult.NoResult();

Edit:

It turns out that I was missing the Framework Reference from the .csproj file

<FrameworkReference Include="Microsoft.AspNetCore.App" />) 

as described here.

However, I don't understand well enough what that delivers to my project in order to answer my question thoroughly as to why this extension method is not available through the normal NuGet packages?

Upvotes: 7

Views: 6803

Answers (3)

Saeed J.
Saeed J.

Reputation: 21

I had similar problem and solved using proper versions in the following package references:

<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />

Upvotes: 2

桑榆肖物
桑榆肖物

Reputation: 103

I have a similar problem, you can try my solution.

var endpoint = context.Features.Get<IEndpointFeature>()?.Endpoint;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.AspNetCore.Http.Features;

namespace Microsoft.AspNetCore.Http
{
    /// <summary>
    /// Extension methods to expose Endpoint on HttpContext.
    /// </summary>
    public static class EndpointHttpContextExtensions
    {
        /// <summary>
        /// Extension method for getting the <see cref="Endpoint"/> for the current request.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/> context.</param>
        /// <returns>The <see cref="Endpoint"/>.</returns>
        public static Endpoint? GetEndpoint(this HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return context.Features.Get<IEndpointFeature>()?.Endpoint;
        }
...

Upvotes: 2

SnakE
SnakE

Reputation: 2581

Recent versions of .NET have all the Microsoft assemblies bundled with the SDK, and your projects automatically reference them without the need to add any NuGet packages. All you need to do to access the ASP assemblies is to change your project SDK from Microsoft.NET.Sdk to Microsoft.NET.Sdk.Web:

<Project Sdk="Microsoft.NET.Sdk.Web">

Upvotes: 2

Related Questions