ルート"test-{id}"
試合api/something/test-something-1
。あなたは、このようなルートの競合、あなたがより良いルートを区別するために、ルートの制約を使用する必要がありますが存在する場合、それは、ルートテンプレートtest-{id}
でmethod1
を呼び出すことapi/something/test-something-1
を呼ぶ理由です
。
[Route("api/something")]
public class MyController : Controller {
[HttpGet("test-{id:int}")]//Matches GET api/something/test-1
public Task<IActionResult> method1(int id) {
//...
}
[HttpGet("test-something-{id}")]//Matches GET api/something/test-something-any_id_here
public Task<IActionResult> method2(string id) {
//...
}
}
id
も同様int
すると仮定されている場合は、同様に二番目にルート制約条件を適用することができます。
[HttpGet("test-something-{id:int}")]//Matches GET api/something/test-something-1
public Task<IActionResult> method2(int id) {
//...
}
参考:Routing in ASP.NET Core : Route Constraint Reference
読む[尋ねる]と[MCVE]提供。このコードはあまりにも間違っています。回答を確認できるマシンの背後にいない場合は、質問を投稿しないでください。できるだけお待ちください。 – CodeCaster