HttpPut归因方法不会进行模型绑定

时间:2018-05-04 07:47:45

标签: c# asp.net asp.net-core-mvc

我正在研究.net核心中的API项目并遇到问题。我无法将客户端模型绑定到'客户端'在下面的方法中的变量,请注意它与[HttpPost]归因方法一起使用。

CODE

[Route("client/{id}")]
[HttpPut]
public IActionResult UpdateClient(string id, [FromBody] Client client)
{ 
    try
    {
        ObjectId objectid = new ObjectId(id);

        Client update = _oauthRepository.GetClient(objectid);

        if(update != null)
        {
            _oauthRepository.UpdateClient(objectid, client);

            return Json(objectid);
        }

        return Json(false);
    }
    catch(Exception e)
    {
        throw e;
    }
}

邮差

[Method: PUT] url: http://localhost:50122/client/5aeb1a29405f2558bc6eac84
Body (JSON(application/json)):

{
    "BsonID": "5aeb1a29405f2558bc6eac84",
    "clientID": "blaatje",
    "clientSecrets": [
        "secret"
    ],
    "allowedScopes": "api"
}

客户模式

namespace AuthServer.Models
{
    public class Client
    {
        [BsonId]
        public ObjectId BsonID { get; set; }
        public string ClientID { get; set; }
        public IEnumerable<string> ClientSecrets { get; set; }
        public string AllowedScopes { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,伙计们,我明白了。它不喜欢BsonID参数,可能是因为它是一个MongoDB属性[BsonId],它被MongoDB驱动程序转换为_id。请参阅以下请求。

<强>邮差

[Method: PUT] url: http://localhost:50122/client/5aeb1a29405f2558bc6eac84
Body (JSON(application/json)):

{
    //"BsonID": "5aeb1a29405f2558bc6eac84", // <-- Leave this line out and it works, probably because this is a MongoDB attributed property
    "clientID": "blaatje",
    "clientSecrets": [
        "secret"
    ],
    "allowedScopes": "api"
}

现在转到下一个错误,但是这个错误超出了这个问题的范围:)