【发布时间】:2017-03-31 19:39:35
【问题描述】:
我正在使用 asp.net 核心创建 web api。 api端点根据guidelines here逻辑映射到资源的关系 所以我的 API 看起来像
http://tax.mydomain.com/api/v1/clients/1/batches/12/start
其中Client 是Batch 的父级,1 是clientid,12 是batchid,Start 是POST 操作方法。
这里是对应的控制器
public class TaxController : Controller
{
[HttpPost]
[Route("clients/{clientid}/batches/{batchid}/start")]
public void Start([FromRoute]string clientId, [FromRoute]string batchId,
[FromBody]IEnumerable<string> urls)
{
// do something
}
}
因为api/v1 对所有控制器都是通用的,所以我在启动的Configure 方法中配置了它。我也希望Home 作为默认控制器。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMvc(routes =>
{
routes.MapRoute("default","api/v1/{controller=Home}/{action=Index}/{id?}");
});
}
但是客户端没有找到 api http://tax.mydomain.com/api/v1/clients/1/batches/12/start 的错误
【问题讨论】:
-
把
[Route("clients/{clientid}/batches/{batchid}/start")]变成[Route("api/v1/clients/{clientid}/batches/{batchid}/start")]
标签: asp.net-core asp.net-core-webapi coreclr