【发布时间】:2017-01-18 09:24:00
【问题描述】:
我在 web api 项目中定义了以下控制器:
[RoutePrefix("api/installations")]
public class InstallationsController : BaseController
{
// GET all
[Authorize]
[Route("")]
public async Task<IHttpActionResult> Get(){ /*...*/}
// GET single
[Authorize]
[Route("{id:int}")]
public async Task<IHttpActionResult> GetInstallation(int id){ /*...*/}
//POST new
[Authorize]
[Route("")]
public async Task<IHttpActionResult> PostInstallation(ViewModels.Installation installation){ /*...*/}
//PUT update
[Authorize]
[Route("{id:int}")]
public async Task<IHttpActionResult> PutInstallation(int id, ViewModels.Installation installation){ /*...*/}
// DELETE
[Authorize]
[Route("{id:int}")]
public async Task<IHttpActionResult> DeleteInstallation(int id){ /*...*/}
[Authorize]
[Route("{id:int}/newimage")]
[HttpPut]
public async Task<IHttpActionResult> PutNewImageUri(int installationId){ /*...*/}
}
上述所有路线都有效,除了最后一条,我基本上想对“api/installations/1/newimage”进行 PUT(我也尝试过 POST,但没有运气)并获取上传二进制文件的链接数据到 Blob 存储。我的问题似乎是对“{id:int}”字段“之后”的任何内容的任何 POST 或 PUT(可能还有 DELETE)都不起作用。 GET 实际上工作正常,因为我在另一个控制器中有这个:
[RoutePrefix("api/customers")]
public class CustomersController : BaseController
// GET related items
[Authorize]
[Route("{id:int}/{subitem}")]
public async Task<IHttpActionResult> GetCustomerChild(int id, string subItem)
这将在对“api/customers/1/anystring”的 GET 请求中调用,并且当我只有“/installations”而不是“/{subitem}”作为变量时它也有效。将我的 PUT 处理程序更改为“{id:int}/{imageAction}”也不起作用。
在我的 WebApiConfig 类中,我只有以下内容(没有基于约定的路由):
config.MapHttpAttributeRoutes();
从浏览器我只得到这个响应,我发现了多个类似的问题,但没有解决我的问题的解决方案:
{
"message": "No HTTP resource was found that matches the request URI 'https://localhost:44370/api/installations/6/newimage'.",
"messageDetail": "No action was found on the controller 'Installations' that matches the request."
}
我已经开始尝试使用以下问题的代码进行调试:get a list of attribute route templates asp.net webapi 2.2
看起来我的路由/函数显示在 RouteEntries 列表中,但它似乎与我的请求不匹配,而且我找不到任何关于如何进一步调试的好建议。任何指针将不胜感激!
【问题讨论】:
-
是不是因为您的操作包含
installationId的参数,但您的路由配置了id,所以它们不匹配? -
天哪,当然是这样。我之前在另一个控制器上也遇到过同样的问题,当时我可能也犯了同样的错误。这是我在 StackOverflow 上的第一个问题,我可以以某种方式将您的评论标记为答案,还是应该删除它,因为标题与我的实际问题无关?
-
我添加了一个带有建议的答案,以防用户再次遇到此问题。
标签: c# asp.net asp.net-web-api attributerouting