Home > Blockchain >  Get link/API path in .NET 6
Get link/API path in .NET 6

Time:03-05

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.

CodePudding user response:

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

CodePudding user response:

try it :

log.EndPoint =  HttpContext.Request.Path.Value;
  • Related