C# Asp net属性路由、POST和PUT在子资源上不起作用

C# Asp net属性路由、POST和PUT在子资源上不起作用,c#,asp.net,asp.net-web-api,attributerouting,C#,Asp.net,Asp.net Web Api,Attributerouting,我在web api项目中定义了以下控制器: [RoutePrefix("api/installations")] public class InstallationsController : BaseController { // GET all [Authorize] [Route("")] public async Task<IHttpActionResult> Get(){ /*...*/} // GET single [Aut

我在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){ /*...*/}
}
从浏览器中,我只得到了以下回答,我发现了多个类似的问题,但没有解决方案可以解决我的问题:

{
    "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."
}
我已开始尝试使用以下问题中的代码进行调试:


看起来我的路由/函数出现在RouteEntry列表中,但它似乎与我的请求不匹配,我找不到任何关于如何进一步调试的好建议。任何提示都将不胜感激

这是因为您的操作包含
installationId
的参数,但您的路由配置了
id
,因此它们不匹配

或者改变路线:

[Route("{installationId:int}/newimage")]
或签名:

public async Task<IHttpActionResult> PutNewImageUri(int id){ /*...*/}
public异步任务PutNewImageUri(int-id){/*…*/}

这是因为您的操作包含一个installationId的参数,但是您的路由配置了
id
,因此它们不匹配

或者改变路线:

[Route("{installationId:int}/newimage")]
或签名:

public async Task<IHttpActionResult> PutNewImageUri(int id){ /*...*/}
public异步任务PutNewImageUri(int-id){/*…*/}

这是因为您的操作包含一个installationId的参数,但是您的路线配置了id,所以它们不匹配吗?哦,天哪,当然是。。早些时候,我在另一个控制器上遇到了完全相同的问题,当时我可能也犯了完全相同的错误。这是我关于StackOverflow的第一个问题,我可以把你的评论作为答案吗,或者我应该删除它,因为标题与我的实际问题无关?我添加了一个答案,并给出了一些建议,以防用户再次遇到此问题。这是因为您的操作包含
installationId
的参数,但您的路线配置了
id
,因此它们不匹配吗?哦,天哪,当然是。。早些时候,我在另一个控制器上遇到了完全相同的问题,当时我可能也犯了完全相同的错误。这是我关于StackOverflow的第一个问题,我是否可以将你的评论标记为答案,或者我是否应该删除它,因为标题与我的实际问题有点无关?我已经添加了一个带有建议的答案,以防用户再次遇到此问题。非常感谢,我没有意识到参数名称必须匹配,认为只有顺序和/或类型才重要。它现在工作得很好!非常感谢,我没有意识到参数名必须匹配,认为只有顺序和/或类型才重要。它现在工作得很好!