创建一个路由模板,匹配 ASP.NET Core 中以特定模式开头的所有 url
Creating a route template that matches all urls that starts with a specific pattern in ASP.NET Core
我有一个基于 ASP.NET Core 5 / .NET 5 Framework 编写的应用程序。我需要创建一个路由模板,它将引导任何以 /homes-for-sale-in-{city}-{id:int}
开头的 URL 定义 controller/action.
这里有一些 urls
的例子
/homes-for-sale-in-los-angeles-100/condo,apartment_type
/homes-for-sale-in-los-angeles-100/0-5000_price/10_page
/homes-for-sale-in-los-angeles-100/condo_type/0-5000_price/2_page
.... yes there are many more
我尝试了以下模式
[Route("/homes-for-sale-in-{city}-{id:int}{*.}", Name = "HomesForSaleByCity")]
public async Task<IActionResult> Display(string city, id? id)
{
return View();
}
但是我得到这个错误:
RoutePatternException: A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter.
我如何添加一个模式来重定向任何以模式开头的 url?
我认为您需要在路由中使用正则表达式,可以参考 this page 一次。
成功了
[Route("/homes-for-sale-in-{city}-{id:int}/{**filters}", Name = "HomesForSaleByCity")]
public async Task<IActionResult> Display(string city, id? id, string filters = "")
{
return View();
}
我有一个基于 ASP.NET Core 5 / .NET 5 Framework 编写的应用程序。我需要创建一个路由模板,它将引导任何以 /homes-for-sale-in-{city}-{id:int}
开头的 URL 定义 controller/action.
这里有一些 urls
的例子/homes-for-sale-in-los-angeles-100/condo,apartment_type
/homes-for-sale-in-los-angeles-100/0-5000_price/10_page
/homes-for-sale-in-los-angeles-100/condo_type/0-5000_price/2_page
.... yes there are many more
我尝试了以下模式
[Route("/homes-for-sale-in-{city}-{id:int}{*.}", Name = "HomesForSaleByCity")]
public async Task<IActionResult> Display(string city, id? id)
{
return View();
}
但是我得到这个错误:
RoutePatternException: A path segment that contains more than one section, such as a literal section or a parameter, cannot contain a catch-all parameter.
我如何添加一个模式来重定向任何以模式开头的 url?
我认为您需要在路由中使用正则表达式,可以参考 this page 一次。
成功了
[Route("/homes-for-sale-in-{city}-{id:int}/{**filters}", Name = "HomesForSaleByCity")]
public async Task<IActionResult> Display(string city, id? id, string filters = "")
{
return View();
}