在正文中将int/string(简单类型)发布到asp.net核心Web API 2.1无法正常工作

编程入门 行业动态 更新时间:2024-10-24 04:45:58
本文介绍了在正文中将int/string(简单类型)发布到asp核心Web API 2.1无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我简直没有运气将邮递员的url编码的表单值发送到使用file-> new project创建的香草asp core 2.1 Web api.我什么都没做,但是新的模型验证功能似乎仍然起作用,并向邮递员返回了400 Bad Request.谁能告诉我我在做什么错?

I'm simply having no luck sending an url encoded form value from postman to a vanilla asp core 2.1 web api created with file->new project. I do nothing to it whatsoever but still the new model validation feature seems to kick in and returns a 400 Bad Request to postman. Can anyone tell me what I'm doing wrong?

控制器操作:

// POST api/values [HttpPost] public void Post([FromBody] string value) { }

原始请求(如提琴手所示):

Raw request (as seen in fiddler):

POST localhost:60843/api/values HTTP/1.1 Content-Type: application/x-www-form-urlencoded cache-control: no-cache Postman-Token: a791eee7-63ff-4106-926f-2s67a8dcf37f User-Agent: PostmanRuntime/7.3.0 Accept: */* Host: localhost:60843 accept-encoding: gzip, deflate content-length: 7 Connection: keep-alive value=test

原始响应:

HTTP/1.1 400 Bad Request Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 Server: Kestrel X-SourceFiles: =?UTF-8?BQzpcUmVwb3NcVGVzdGJlZFxNb2RlbEJpbmRpbmdcTW9kZWxCaW5kaW5nXGFwaVx2YWx1ZXM=?= X-Powered-By: ASP.NET Date: Thu, 25 Oct 2018 15:23:49 GMT 21 {"":["The input was not valid."]} 0

再次请注意,这是Visual Studio 2017中asp Web API的默认模板.

Note again that this is the default template for asp web api in Visual Studio 2017.

一个有趣的事实是,如果我添加Swashbuckle并转到swagger ui端点并使用内置的"try it"功能,那么开箱即用也会产生错误.

An interesting fact is that if I add Swashbuckle and go to the swagger ui endpoint and use the built in "try it" functionality it produces an error as well, out of the box.

我已经将其用于复杂类型和json主体,但是我无法使用简单类型,因此我尝试了各种不同的内容类型.

I've gotten this to work with a complex type and a json body, but I can not get the simple type to work and I've tried with all sorts of different content-types.

推荐答案

对于那些偶然发现此问题的人,默认情况下从2.1版开始自动应用模型绑定,如果模型绑定失败,则返回400错误请求(更改为早期版本,需要您检查ModelState.IsValid以查看模型绑定是否成功).

For those who stumble upon this question, starting with version 2.1 automatic model binding is applied by default and a 400 Bad Request is returned if model binding fails (change from earlier versions which required you to check ModelState.IsValid to see if model binding was successful).

现在将简单类型发布到asp核心控制器操作时,您现在必须指定其来源.可以推断出复杂的类型,但不能推断出诸如int和string之类的简单类型(如果它们位于消息正文中.如果它们位于查询字符串或route(url)中,则可以推断出它们).我看到在主体中传递值的两个选项是:

When posting simple types to an asp core controller action you now have to specify where it is coming from. Complex types can be inferred but simple types like int and string cannot (if they're in the message body that is. They are inferred if they're in either the query string or route(url)). The two options I see to pass values in the body itself are:

  • 通过url编码的表单值(将 [FromForm] 添加到操作中的参数)
  • By way of url encoded form value (add [FromForm] to your parameter in the action)
  • 邮递员的请求:

    POST your-url/api/values HTTP/1.1 Content-Type: application/x-www-form-urlencoded value=test

    动作方法签名:

    [HttpPost] public void Post([FromForm]string value) { }

  • 通过json正文(将 [FromBody] 添加到操作中的参数)
  • By way of a json body (add [FromBody] to your parameter in the action)
  • 邮递员的请求

    POST your-url/api/values HTTP/1.1 Content-Type: application/json "test"

    动作方法签名:

    [HttpPost] public void Post([FromBody]string value) { }

    更多推荐

    在正文中将int/string(简单类型)发布到asp.net核心Web API 2.1无法正常工作

    本文发布于:2023-10-29 09:53:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1539387.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:中将   无法正常   核心   类型   简单

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!