How to access current absolute Uri from any ASP .Net Core class?

后端 未结 2 1530
忘掉有多难
忘掉有多难 2021-02-12 22:26

I am trying to figure out how to access the current absolute Uri -- i.e. the absolute url of the view that is currently being rendered -- from a user class in .

2条回答
  •  猫巷女王i
    2021-02-12 22:56

    You may use the GetDisplayUrl extension method.

    var url = httpContextAccessor.HttpContext?.Request?.GetDisplayUrl();
    

    Assuming httpContextAccessor is an object of IHttpContextAccessor which was injected via DI.

    This extension method is defined in Microsoft.AspNetCore.Http.Extensions namespace. So you need to have a using statement to include it in your file.

    using Microsoft.AspNetCore.Http.Extensions;

提交回复
热议问题