Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

402
Visualizações
How to see endpoint name from HttpContext?

Let's say I have a razor page application, and there is a razor page "Product.cshtml", how can I see "Product.cshtml" string in the HttpContext object (assuming I can access the HttpContext object in the middleware before endpoint middleware as the code below shows)? I tried to use EndpointHttpContextExtensions.GetEndpoint(HttpContext) extension method, still couldn't find anything that might contain "Product.cshtml".

// startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
   app.UseRouting();
   app.UseMiddleware<GetEndPointMiddleWare>();
   app.UseEndpoints(endpoints => {
      endpoints.MapRazorPages();
   });
}

public class GetEndPointMiddleWare
{
   private RequestDelegate next;
   public QueryStringMiddleWare(RequestDelegate nextDelegate) {
      next = nextDelegate;
   }
   public async Task Invoke(HttpContext context) {
      ...
      await next(context);
      // I want to print "Product.cshtml" here
      // var endpoint = EndpointHttpContextExtensions.GetEndpoint(context); // <---- didn't find any property that has the string I want          
   }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The path to the page is within the endpoint metadata as part of a PageActionDescriptor object, so that could be accessed and parsed. An example of middleware that inspects the page path:

// Startup.cs -> Configure()

app.UseRouting();

app.Use(next => context =>
{
    var endpoint = context.GetEndpoint();
    if (endpoint is null)
    {
        return Task.CompletedTask;
    }

    if (endpoint.Metadata.FirstOrDefault(i => i is PageActionDescriptor) is PageActionDescriptor pageDescriptor)
    {
        string pagePath = pageDescriptor.RelativePath;
        Console.WriteLine($"Page path: {pagePath}"); // '/Pages/Product.cshtml'
    }

    return Task.CompletedTask;
});

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapDefaultControllerRoute();
});

(Adapted from docs example)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda