C# 根据.NET Core 3.0中注册的端点手动匹配url
对于我的应用程序,我希望将url与所有已注册的路由进行匹配,以查看是否存在匹配 当有匹配时,我想从匹配中提取RouteValue 我在ASP.NET Core 2.1中实现了这一点,但我似乎无法像在.NET Core 3中检索路由那样检索路由 正在使用的ASP.NET Core 2.1示例:C# 根据.NET Core 3.0中注册的端点手动匹配url,c#,asp.net-core,C#,Asp.net Core,对于我的应用程序,我希望将url与所有已注册的路由进行匹配,以查看是否存在匹配 当有匹配时,我想从匹配中提取RouteValue 我在ASP.NET Core 2.1中实现了这一点,但我似乎无法像在.NET Core 3中检索路由那样检索路由 正在使用的ASP.NET Core 2.1示例: string url = "https://localhost/Area/Controller/Action"; // Try to match against the default route (bu
string url = "https://localhost/Area/Controller/Action";
// Try to match against the default route (but can be any other route)
Route defaultRoute = this._httpContextAccessor.HttpContext.GetRouteData().Routers.OfType<Route>().FirstOrDefault(p => p.Name == "Default");
RouteTemplate defaultTemplate = defaultRoute.ParsedTemplate;
var defaultMatcher = new TemplateMatcher(defaultTemplate, defaultRoute.Defaults);
var defaultRouteValues = new RouteValueDictionary();
string defaultLocalPath = new Uri(url).LocalPath;
if (!defaultMatcher.TryMatch(defaultLocalPath, defaultRouteValues))
{
return null;
}
string area = defaultRouteValues["area"]?.ToString();
string controller = defaultRouteValues["controller"]?.ToString();
string actiondefaultRouteValues["action"]?.ToString();
stringurl=”https://localhost/Area/Controller/Action";
//尝试与默认路由匹配(但可以是任何其他路由)
Route defaultRoute=this.\u httpContextAccessor.HttpContext.GetRouteData().Routers.OfType().FirstOrDefault(p=>p.Name==“默认”);
RouteTemplateDefaultTemplate=defaultRoute.ParsedTemplate;
var defaultMatcher=new TemplateMatcher(defaultTemplate,defaultRoute.Defaults);
var defaultRouteValues=新的RouteValueDictionary();
字符串defaultLocalPath=新Uri(url).LocalPath;
if(!defaultMatcher.TryMatch(defaultLocalPath,defaultRouteValues))
{
返回null;
}
string area=defaultRouteValues[“area”]?.ToString();
字符串控制器=defaultRouteValues[“控制器”]?.ToString();
字符串actiondefaultRouteValues[“action”]?.ToString();
是否有方法获取所有注册的端点(模板)并与这些模板匹配?在ASP.NET Core 2.1及以下版本中,通过实现
IRouter
接口将传入URL映射到处理程序来处理路由。与直接实现接口不同,您通常会依赖添加到中间件管道末尾的MvcMiddleware
实现
在ASP.NET Core 3.0中,我们使用端点路由,因此路由步骤与端点调用是分开的。实际上,这意味着我们有两个中间件:
执行实际路由的中间件,即。 计算将为给定请求URL调用哪个端点 路径EndpointRoutingMiddleware
- 调用端点的
端点中间件
public class TestController : Controller
{
private readonly EndpointDataSource _endpointDataSource;
public TestController ( EndpointDataSource endpointDataSource)
{
_endpointDataSource = endpointDataSource;
}
public IActionResult Index()
{
string url = "https://localhost/User/Account/Logout";
// Return a collection of Microsoft.AspNetCore.Http.Endpoint instances.
var routeEndpoints = _endpointDataSource?.Endpoints.Cast<RouteEndpoint>();
var routeValues = new RouteValueDictionary();
string LocalPath = new Uri(url).LocalPath;
//To get the matchedEndpoint of the provide url
var matchedEndpoint = routeEndpoints.Where(e => new TemplateMatcher(
TemplateParser.Parse(e.RoutePattern.RawText),
new RouteValueDictionary())
.TryMatch(LocalPath, routeValues))
.OrderBy(c => c.Order)
.FirstOrDefault();
if (matchedEndpoint != null)
{
string area = routeValues["area"]?.ToString();
string controller = routeValues["controller"]?.ToString();
string action = routeValues["action"]?.ToString();
}
return View();
}
}
公共类TestController:Controller
{
私有只读EndpointDataSource_EndpointDataSource;
公共TestController(EndpointDataSource EndpointDataSource)
{
_endpointDataSource=endpointDataSource;
}
公共IActionResult索引()
{
字符串url=”https://localhost/User/Account/Logout";
//返回Microsoft.AspNetCore.Http.Endpoint实例的集合。
var routeEndpoints=_endpointDataSource?.Endpoints.Cast();
var routeValues=新的RouteValueDictionary();
字符串LocalPath=新Uri(url).LocalPath;
//获取提供url的匹配点
var matchedEndpoint=routeEndpoints.Where(e=>newtemplatematcher(
Parse(例如RoutePattern.RawText),
新建RouteValueDictionary())
.TryMatch(本地路径、路由值))
.OrderBy(c=>c.Order)
.FirstOrDefault();
if(matchedEndpoint!=null)
{
字符串区域=路由值[“区域”]?.ToString();
字符串控制器=路由值[“控制器”]?.ToString();
string action=routeValues[“action”]?.ToString();
}
返回视图();
}
}
有关ASP.NET Core 3.0中端点路由的更多详细信息,请参阅