【发布时间】:2021-02-01 07:04:59
【问题描述】:
我正在使用 ASP.NET Core 3.1 创建一个 Web api,并尝试将 URL 路由到控制器。到目前为止,我有一个这样的基本控制器:
[Route("abc")]
[ApiController]
public class ABCController : ControllerBase
{
// GET: abc/1234
[HttpGet("{id}")]
public async Task<ActionResult<string>> GetABCService(long id)
{
...
}
}
当我输入 http://myurl/abc/1234 时,这会正确地将我引导到该页面。我想要连接的下一个控制器是这样的:
[Route("xxx")]
[ApiController]
public class XXXController : ControllerBase
{
// GET: abc/1234/XXX
[HttpGet("{id}")]
public async Task<ActionResult<string>> GetXXXService(long id)
{
...
}
}
当我输入 http://myurl/abc/1234/xxx 时,它总是给我 404。我通过像这样设置我的端点使第一个工作:
app.UseEndpoints(endpoints =>{
endpoints.MapControllerRoute(
"abc",
"abc/{id}",
new {controller = "ABCController", action = "GetABCService"});
//My current endpoint mapping for the second controller:
endpoints.MapControllerRoute(
"xxx",
"abc/{id}/xxx",
new {controller = "XXXController", action = "GetXXXStatus" });
}
我不明白为什么我会在 http://myurl/abc/1234/xxx 得到 404。有什么见解吗?
【问题讨论】:
-
尝试在
"abc/{id}"之前编写"abc/{id}/xxx"的路由配置。
标签: c# asp.net-core asp.net-core-webapi asp.net-core-3.1 asp.net-apicontroller