Asp.Net core get RouteData value from url

前端 未结 3 2007
感动是毒
感动是毒 2020-12-15 04:14

I\'m wokring on a new Asp.Net core mvc app. I defined a route with a custom constraint, which sets current app culture from the url. I\'m trying to manage localization for m

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 04:36

    This has been made easier with the addition of the Endpoint Routing feature. This article explains how to do it using the Endpoint Routing feature https://aregcode.com/blog/2019/dotnetcore-understanding-aspnet-endpoint-routing/

    var endpointFeature = context.Features[typeof(Microsoft.AspNetCore.Http.Features.IEndpointFeature)]
                                           as Microsoft.AspNetCore.Http.Features.IEndpointFeature;
    
    Microsoft.AspNetCore.Http.Endpoint endpoint = endpointFeature?.Endpoint;
    
    //Note: endpoint will be null, if there was no
    //route match found for the request by the endpoint route resolver middleware
    if (endpoint != null)
    {
        var routePattern = (endpoint as Microsoft.AspNetCore.Routing.RouteEndpoint)?.RoutePattern
                                                                                   ?.RawText;
    
        Console.WriteLine("Name: " + endpoint.DisplayName);
        Console.WriteLine($"Route Pattern: {routePattern}");
        Console.WriteLine("Metadata Types: " + string.Join(", ", endpoint.Metadata));
    }
    

提交回复
热议问题