ASP.NET MVC的核心:如何获得绑定到一个字符串不包含类型原始JSON? [英] ASP.NET Core MVC : How to get raw JSON bound to a string without a type?

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

问题描述

类似<一个href=\"http://stackoverflow.com/questions/13041808/mvc-controller-get-json-object-from-http-body\">this有关以前版本的ASP.NET老问题,我希望得到一个HTTP POST的请求体绑定到一个字符串。看来,该方法结合,但为空,当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

我传入体内以下内容: {一:1}

我不想绑定到一个名为字符串变量。我要绑定任何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.

下面类似的问题 给我的想法也许我应该写的 [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.

推荐答案

由于我评论,该解决方案是使用 [FromBody]动态数据我的参数列表,使用动态而不是字符串,我将获得 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.

然后我可以转换为方法中的字符串转换传入 JObject (Newtonsoft.JSON在JSON内存中的数据类型)转换为字符串。

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

href=\"http://stackoverflow.com/questions/23135403/web-api-frombody-is-null-from-web-client\">这里对方的回答。

Found at other answer here.

样code,感谢Jeson Martajaya:

Sample code, thanks to Jeson Martajaya:

使用动态:

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

样code。与JObject:

Sample code with JObject:

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

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

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