.net core 3.0 中是否有任何功能可以将 url 与其路由匹配
Is there any function in .net core 3.0 that can match url with its route
我有一个包含 URL 的字符串,我想获得匹配此 URL 的路由。
假设 URL 字符串包含值
http://localhost/PerformOperation/accountId/Operation/operationId
并且我有一条名为 "PerformOperations" 的路线与此 url
相匹配
PerformOperation/{accountId}/{action}/{operationId}
.
如何获得这条路线? MVC 显然会根据每个请求执行此操作,但我不知道如何手动执行此操作。我有一个路线列表,我必须从 URL 中找到匹配的路线。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseEndpointRouting();
//Your custom middleware that does something with routing data
app.Use((context, next) =>
{
// let assume we are asking for http://localhost:18159/home/index/22
var endpointFeature = context.Features[typeof(Microsoft.AspNetCore.Http.Features.IEndpointFeature)]
as Microsoft.AspNetCore.Http.Features.IEndpointFeature;
Endpoint endpoint = endpointFeature?.Endpoint;
if (endpoint != null)
{
var routePattern = (endpoint as Microsoft.AspNetCore.Routing.RouteEndpoint)?.RoutePattern
?.RawText;
// Example result : Name: AdRaker.Core.Web.Tracker.Controllers.HomeController.Index (AdRaker.Core.Web.Tracker)
var info1 = $"Name: {endpoint.DisplayName}";
// Example result: Route Pattern: Home/Index/{id?}
var info2 = $"Route Pattern: {routePattern}";
// Example result: Metadata Types: Microsoft.AspNetCore.Mvc.ControllerAttribute,
// Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor, Microsoft.AspNetCore.Routing.DataTokensMetadata,
// Microsoft.AspNetCore.Routing.RouteValuesAddressMetadata, Microsoft.AspNetCore.Mvc.Internal.ControllerActionFilter,
// Microsoft.AspNetCore.Mvc.ViewFeatures.SaveTempDataAttribute, Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter
var info3 = $"Metadata Types: {string.Join(", ", endpoint.Metadata)}";
}
// Forward data to next middleware
return next();
});
[...]
第一个中间件负责检查传入请求并将其与 endpoint.Without 匹配,端点数据将始终为空。
我有一个包含 URL 的字符串,我想获得匹配此 URL 的路由。
假设 URL 字符串包含值
http://localhost/PerformOperation/accountId/Operation/operationId
并且我有一条名为 "PerformOperations" 的路线与此 url
相匹配PerformOperation/{accountId}/{action}/{operationId}
.
如何获得这条路线? MVC 显然会根据每个请求执行此操作,但我不知道如何手动执行此操作。我有一个路线列表,我必须从 URL 中找到匹配的路线。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseEndpointRouting();
//Your custom middleware that does something with routing data
app.Use((context, next) =>
{
// let assume we are asking for http://localhost:18159/home/index/22
var endpointFeature = context.Features[typeof(Microsoft.AspNetCore.Http.Features.IEndpointFeature)]
as Microsoft.AspNetCore.Http.Features.IEndpointFeature;
Endpoint endpoint = endpointFeature?.Endpoint;
if (endpoint != null)
{
var routePattern = (endpoint as Microsoft.AspNetCore.Routing.RouteEndpoint)?.RoutePattern
?.RawText;
// Example result : Name: AdRaker.Core.Web.Tracker.Controllers.HomeController.Index (AdRaker.Core.Web.Tracker)
var info1 = $"Name: {endpoint.DisplayName}";
// Example result: Route Pattern: Home/Index/{id?}
var info2 = $"Route Pattern: {routePattern}";
// Example result: Metadata Types: Microsoft.AspNetCore.Mvc.ControllerAttribute,
// Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor, Microsoft.AspNetCore.Routing.DataTokensMetadata,
// Microsoft.AspNetCore.Routing.RouteValuesAddressMetadata, Microsoft.AspNetCore.Mvc.Internal.ControllerActionFilter,
// Microsoft.AspNetCore.Mvc.ViewFeatures.SaveTempDataAttribute, Microsoft.AspNetCore.Mvc.ModelBinding.UnsupportedContentTypeFilter
var info3 = $"Metadata Types: {string.Join(", ", endpoint.Metadata)}";
}
// Forward data to next middleware
return next();
});
[...]
第一个中间件负责检查传入请求并将其与 endpoint.Without 匹配,端点数据将始终为空。