REST web api请求正文未绑定 [英] REST web api request body not bound

查看:113
本文介绍了REST web api请求正文未绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ApiController-subclassed控制器中有2个asp.net web api操作。 POST动作方法有两种。一种方法是使用非null请求体。但是另一个动作方法总是得到null请求体。我不知道第二个POST动作不起作用的原因。请帮助。



以下POST操作方法与非空请求正文绑定一起正常工作:方法签名中的参数模型被绑定正确。



[HttpPost]

[路线(创建/ {sessionid:guid})]

public HttpResponseMessage CreateAccount(Guid sessionid,UserAccountDto model)

{

var result = new HttpResponseMessage(HttpStatusCode.OK);





返回Request.CreateResponse(HttpStatusCode.OK,型号);

}



Dto课程:



公共课UserAccountDto

{

公共长? AccountId {get;组; }

公共字符串电子邮件{get;组; }

公共字符串密码{get;组; }

public string Salt {get;组; }

公共字符串PlainTextPassword {get;组; }

}



使用插入Chrome的Advanced Rest Client工具调用API



POST http:// localhost:55540 / guests / create / 13DD8111-8A00-48CE-997F-28EE3C88895D



{

电子邮件:meme@me.com,

密码:fkdld,

盐:盐,

plaintextpassword:abc

}





然而,第二个ApiController POST操作方法不起作用。方法操作中的参数模型始终为 null ;它由于某种原因没有正确绑定。



[HttpPost]

[路线(转换/ {sessionid:guid }))

public IHttpActionResult ConvertAccount(Guid sessionid,LinkedAccountThinDto model)

{

var accountType = model.AccountType;

var accountKey = model.AccountKey;



返回Ok();

}

自定义Dto课程:



公共课LinkedAccountThinDto

{

公共字符串AccountType {get;组; }

公共字符串AccountKey {get;组; }

}





使用插入Chrome的Advanced Rest Client工具调用API:




POST http:// localhost:55540 / guests / convert / 13DD8111-8A00-48CE-997F-28EE3C88895D



{

accounttype:abcdf,

accountkey:1234567

}

I have 2 asp.net web api actions in an ApiController-subclassed controller. The 2 action methods are POST. One method is working with non-null request body. But the other action method always get null request body. I do not know the reason why the second POST action does not work. Please help.

The following POST action method works OK with non-null request body binding: parameter "model" in method signature is bound correctly.

[HttpPost]
[Route("create/{sessionid:guid}")]
public HttpResponseMessage CreateAccount(Guid sessionid, UserAccountDto model)
{
var result = new HttpResponseMessage(HttpStatusCode.OK);


return Request.CreateResponse(HttpStatusCode.OK, model);
}

Dto class:

public class UserAccountDto
{
public long? AccountId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Salt { get; set; }
public string PlainTextPassword { get; set; }
}

Call with API by using Advanced Rest Client tool plugged in Chrome

POST http://localhost:55540/guests/create/13DD8111-8A00-48CE-997F-28EE3C88895D

{
email:"meme@me.com",
password:"fkdld",
salt:"salt",
plaintextpassword :"abc"
}


However, the 2nd ApiController POST action method does not work. Parameter "model" in method action is always null; it is not bound correctly for some reason.

[HttpPost]
[Route("convert/{sessionid:guid}")]
public IHttpActionResult ConvertAccount(Guid sessionid, LinkedAccountThinDto model)
{
var accountType = model.AccountType;
var accountKey = model.AccountKey;

return Ok();
}
Custom Dto class:

public class LinkedAccountThinDto
{
public string AccountType { get; set; }
public string AccountKey { get; set; }
}


Call with API by using Advanced Rest Client tool plugged in Chrome:


POST http://localhost:55540/guests/convert/13DD8111-8A00-48CE-997F-28EE3C88895D

{
accounttype: "abcdf",
accountkey: "1234567"
}

推荐答案

请尝试以下表格中的请求格式:

Try request format in below form:
{
"accounttype": "abcdf",
"accountkey": "1234567"
}



为键名添加引号。


Add quotes to key name.


这篇关于REST web api请求正文未绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆