ASP.NET Core MVC:如何将原始 JSON 绑定到没有类型的字符串? [英] ASP.NET Core MVC : How to get raw JSON bound to a string without a type?

查看:29
本文介绍了ASP.NET Core MVC:如何将原始 JSON 绑定到没有类型的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于这个关于先前 ASP 的老问题.NET 版本,我想将 HTTP POST 的请求正文绑定到字符串.似乎该方法绑定,但 value 为空,当 ASP.NET 调用我的控制器方法时:

Similar to this old question about prior ASP.NET versions, I want to get the request body of an HTTP POST to be bound to a string. It seems that the method binds, but that value is null, when ASP.NET invokes my controller method:

namespace Demo.Controllers
{

    [Route("[controller]")]
    public class WebApiDemoController : Controller
    {
    ...

    // POST api/values
    [HttpPost]
    public System.Net.Http.HttpResponseMessage Post([FromBody]string value)
    {
       // expected: value = json string, actual: json = null.
    }

我还需要从溪流中抓取尸体吗?或者这应该只是工作?在测试上述方法时,我使用了以下 http 标头:

Do I still have to go grab the body from a stream? Or should this just work? When testing the above method, I used the following http headers:

Accept: Application/json
Content-Type: Application/json;charset=UTF-8

我在正文中传递以下内容:{ "a": 1 }

I'm passing in the following in the body: { "a": 1 }

我不想绑定到名为 a 的字符串变量.我想绑定我得到的任何 JSON,然后我想在我的方法中使用 JSON 内容,任何任意内容.

I do NOT want to bind to a string variable named a. I want to bind any JSON I get, and then I want to use the JSON content, any arbitrary content at all, from within my method.

如果我理解文档,[FromBody] 属性应该已经完成​​了我想要的,但我猜测 ASP.NET 核心 MVC 绑定机制不会将 json 绑定到字符串值",但也许我可以做一些其他事情,让我获得同等程度的灵活性.

If I understood the documentation, the [FromBody] attribute should have done what I wanted, but I'm guessing that the ASP.NET core MVC binding mechanism won't bind a json to a "string value", but perhaps I could do something else that gets me an equivalent level of flexibility.

一个类似的问题 here 给了我这个想法我应该写[FromBody] 动态数据 而不是使用[FromBody] 字符串值.

A similar question here gives me the idea maybe I should have written [FromBody] dynamic data instead of using [FromBody] string value.

更新:这种技巧在做之前应该考虑一下,因为如果你想让 .net 核心框架为你处理 JSON 和 XML 编码,你只是扼杀了这种能力.某些类型的 REST 服务器可以并且经常需要同时支持 XML 和 JSON 内容类型,至少我遇到过的具有标准文档的类型.

Update: This kind of trick should be thought about before doing it, because if you wanted to have the .net core framework handle JSON and XML encoding for you, you just killed that capability. Certain types of REST servers can and often do have requirements to support both XML and JSON content-types, at least ones I have encountered which have standards documents.

推荐答案

以下内容适用于 .net core 1.x,但不适用于 .net core 2.x.

The following works in .net core 1.x, but not in .net core 2.x.

正如我所评论的,解决方案是使用 [FromBody]dynamic data 作为我的参数列表,使用 dynamic 而不是 string,并且我会收到一个 JObject.

As I commented, the solution is to use [FromBody]dynamic data as my parameter list, using dynamic instead of string, and I will receive a JObject.

警告:如果您的架构要求单个 WebApi 服务器在生成 XML 和 JSON 方面同样流畅,则取决于内容类型标头条目,这种直接使用 JSON 的策略可能适得其反在你身上.(通过足够的工作可以在同一服务上同时支持 XML 和 JSON,但是随后您将进一步提升 MVC 资产管道的内容并将其向下移动到您的控制器方法中,结果证明这违背了 MVC 的精神, 模型以 POCO 已经解析的形式出现.)

Caution: If your architecture calls for a single WebApi server to be equally fluent in producing XML and JSON, depending on content-type header entries, this kind of direct-JSON-consumption strategy can backfire on you. (Supporting both XML and JSON on the same service is possible with sufficient work, but then you're taking stuff that was further UP the MVC asset pipeline and moving it down into your controller methods, which turns out to be against the spirit of MVC, where models come to you as POCOs already parsed.)

在方法内部转换为字符串后,将传入的 JObject(内存数据类型为 JSON 的 Newtonsoft.JSON)转换为字符串.

Once you convert to a string inside the method, converting the incoming JObject (Newtonsoft.JSON in memory data type for JSON) to a string.

可在 此处找到其他答案.

示例代码,感谢 Jeson Martajaya:

Sample code, thanks to Jeson Martajaya:

动态:

[HttpPost]
public System.Net.Http.HttpResponseMessage Post([FromBody]dynamic value)
{
   //...
}

使用 JObject 的示例代码:

Sample code with JObject:

[HttpPost]
public System.Net.Http.HttpResponseMessage Post([FromBody]Newtonsoft.Json.Linq.JObject value)
{
   //...
}

这篇关于ASP.NET Core MVC:如何将原始 JSON 绑定到没有类型的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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