C#中[FromBody]属性的功能是什么? [英] What is the function of [FromBody] Attribute in C#?

查看:377
本文介绍了C#中[FromBody]属性的功能是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否具有 [FromBody] 属性的功能?我的意思是,例如,当我使用它时:

Is there a function of [FromBody] attribute? I mean, when I use it for example:

公共异步任务SetUser([FromBody]用户)

当我使用时:

公共异步任务SetUser(用户)

服务器获取同一对象没有问题,因此,有必要对其进行设置,或者我可以不用担心删除它吗?

The server get the same object without problems, so, it's necessary set it, or I can remove it without worries?

礼物!

推荐答案

User 是复杂类型,因此默认情况下,服务器将尝试从请求正文中解析它.如果您输入的是简单类型,例如

User is a complex type, so by default the server will try to resolve it from the request body. If you had a simple type -- e.g.

public async Task SetUser(string userId)

服务器将尝试通过URL绑定解析该值.您可以通过指定

the server would try to resolve the value via URL binding. You can override that behaviour by specifying

public async Task SetUser([FromBody] string userId)

通常出于可读性考虑,我通常将 [FromBody] 留在签名中.

I usually leave [FromBody] in the signature simply for the sake of readability.

这篇关于C#中[FromBody]属性的功能是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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