我想定义一条只需要匹配前几个部分的路线,而忽略其余部分:
[Route("public/portfolio/{publicPortfolioId}")]
这个想法是,以public/portfolio/myid
开头的所有内容都将匹配。这将导致以下项目的匹配项:
public/portfolio/myid
public/portfolio/myid/something
public/portfolio/myid/something/that/doesnt/exist
答案 0 :(得分:2)
使用 catch-all 参数。
[Route("public/portfolio/{publicPortfolioId}/{*remainder}")]
有两种使用方式。
忽略其余部分:
[Route("public/portfolio/{publicPortfolioId}/{*remainder}")]
public IActionResult DemoAction(int publicPortfolioId)
{
return Json(new
{
publicPortfolioId
});
}
捕获其余部分:
[Route("public/portfolio/{publicPortfolioId}/{*remainder}")]
public IActionResult DemoAction(int publicPortfolioId, string remainder)
{
return Json(new
{
publicPortfolioId,
remainder
});
}
另请参阅:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.1