加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET Web API正文值限制

发布时间:2020-12-16 06:27:02 所属栏目:asp.Net 来源:网络整理
导读:我正在研究ASP.NET Web API,但在关于来自请求体的复杂类型的解释中,作者混淆了我: PROFESSIONAL ASP.NET MVC 4:第11章 – ASP.NET Web API “[..] complex types (everything else) are taken from the body. There is an additional restriction as well:
我正在研究ASP.NET Web API,但在关于来自请求体的复杂类型的解释中,作者混淆了我:

PROFESSIONAL ASP.NET MVC 4:第11章 – ASP.NET Web API

“[..] complex types (everything else) are taken from the body. There is
an additional restriction as well: Only a single value can come from
the body,and that value must represent the entirety of the body.
[…]”

Brad Wilson

他对这种“单一价值可以来自身体”的意思是什么? API格式化程序只能从主体中解析单个类型的对象吗?你能举例说明一下吗?

解决方法

Only a single value can come from the body

假设您有这样的请求体.

{“Id”:12345,“FirstName”:“John”,“LastName”:“West”}

您希望将此JSON绑定到类似此类型的参数.

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

动作方法可以像void Post(Employee emp).并且它不能像这样 – void Post(Employee john,Employee duplicateJohn).只有一个值可以来自身体.

and that value must represent the entirety of the body

假设你有这样的请求体.

{“Id”:12345,“LastName”:“West”}

你有两个像这样的DTO.

public class Identifier
{
    public int Id { get; set; }
}

public class Name
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

您不能拥有像void Post(标识符ID,名称名称)这样的操作方法,并期望将主体部分绑定到这两个参数.整体必须只绑定一个值.所以,有一个类似的

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

并将请求主体完整地绑定到一个值,如void Post(Employee emp)是允许的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读