我试图让这种方法起作用:
public class DocumentenController : ApiController
{
[HttpPost]
[Route("DeleteDocument/{name}/{planId}")]
public IHttpActionResult DeleteDocument(string name, int planId)
{
_documentenProvider.DeleteDocument(planId, name);
return Ok();
}
}
这是WebApiConfig:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: UrlPrefix + "/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional}
);
但是当我用这样的帖子调用它时,我得到了404:
http://localhost/QS-documenten/api/documenten/deletedocument/testing/10600349
解决这个问题的正确方法是什么?
答案 0 :(得分:4)
示例中的URL与控制器上的属性路由不匹配。
获得
http://localhost/QS-documenten/api/documenten/deletedocument/testing/10600349
开始工作,假设http://localhost/QS-documenten
是主机和根文件夹,api/documenten
是api前缀,然后向控制器添加路由前缀......
[RoutePrefix("api/Documenten")]
public class DocumentenController : ApiController {
//eg POST api/documenten/deletedocument/testing/10600349
[HttpPost]
[Route("DeleteDocument/{name}/{planId}")]
public IHttpActionResult DeleteDocument(string name, int planId) {
_documentenProvider.DeleteDocument(planId, name);
return Ok();
}
}
答案 1 :(得分:2)
您必须按如下方式发送请求:
http://localhost/QS-documenten/deletedocument/testing/10600349
使用route属性时,自定义路由会覆盖默认的api路由配置。