'Get link/API path in .NET 6

I have create a program that using .NET Framework 4.7.2 and I want to convert to .NET 6 (just for training purpose or future use.

The way when I get the link like "/jsonAPI/prxy001" in .NET 4.7.2 like this :

log.EndPoint = HttpContext.Current.Request.Url.AbsolutePath.ToString();

log.Endpoint is just a model

And I try to use it in .NET 6 said that "Current" is not available in HttpContext. I think the way or reference is defference. Can you tell me how?

P.S = I generate that not in controller, but in another helper class.



Solution 1:[1]

Inject HttpContextAccessor in the startup configureservices method like below

services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

then inject it via constructor in any class you need

public class Test 
 {
    private IHttpContextAccessor context;

     public Test(IHttpContextAccessor ctx) {
      this.context = ctx;
     }
 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sumit S