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

3

7

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?

Garter answered 9/1, 2021 at 0:50 Comment(3)
Could you please show a wider example of your AuthenticationHandler? How are you obtaining the object Context? Did you try adding the using Microsoft.AspNetCore.Http after installing the package? There should be no problem in the package targeting netstandard, it should be compatible :)Springs
@NPinheiro, I didn't want to go into detail about the AuthenticationHandler because it was out of scope, but it is based on the example here: jasonwatmore.com/post/2019/10/21/…Garter
I have similar issue I am trying it in a middlware to get the Controller and action name.Kery
U
2

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">
Unbounded answered 7/10, 2022 at 19:36 Comment(0)
C
2

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" />
Confucianism answered 6/3, 2023 at 16:52 Comment(0)
M
1

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;
        }
...

Mattah answered 17/10, 2022 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.