ASP.NET MVC读取原始JSON数据后 [英] ASP.NET MVC Read Raw JSON Post Data

查看:190
本文介绍了ASP.NET MVC读取原始JSON数据后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是推动我疯了。我使用ASP.NET MVC。我有与作为由另一台服务器(不在我的控制)调用的回调URL的HttpPost作用的控制器。我想动态读取JSON张贴到它,而无需使用的WebAPI或模型绑定。该网址也有传递给它的查询字符串参数。

This is driving me crazy. I'm using ASP.NET MVC. I have a controller with an HttpPost action that acts as a callback URL that is called by another server (not under my control). I want to dynamically read JSON posted to it without using WebAPI or Model Binding. The URL also has a query string parameter passed to it.

回调URL看起来是这样的:

The callback URL looks something like this:

http://domain.com/callback?secret=1234

我试着阅读使用贴输入:

I've tried reading the posted input using:

[HttpPost]
public ActionResult Callback( String secret )
{
    String jsonData = new StreamReader(this.Request.InputStream).ReadToEnd();

    // ...
}

不过jsonData始终为空或空。

However "jsonData" is always null or empty.

我只是想获得张贴的输入,将其放入JsonFx这样我就可以动态访问的内容。如何做到这一点的最简单的方式任何想法?

I just want to get the posted input and stick it into JsonFx so I can dynamically access the contents. Any ideas on how to do this the easiest possible way?

更新

我发现下面的...

虽然上述不起作用(jsonData将为空或空),则下列情况,如果我什么配置的小选项我有呼叫服务器上,从而忽略了秘密查询字符串参数,这是所有我可以为此做的,因为它不是我的服务器。在这种情况下,jsonData将具有正确的贴JSON字符串

While the above DOES NOT work (jsonData will be null or empty), the following DOES if I configure what little options I have on the calling server so as to omit the "secret" query string parameter, which is about all I can do on that end since it is not my server. In this case, jsonData will have the correct posted JSON string:

[HttpPost]
public ActionResult Callback( /* String secret */ )
{
    String jsonData = new StreamReader(this.Request.InputStream).ReadToEnd();

    // ...
}

这是很无奈的解决,我不知道一个简单的方法来同时接受查询字符串并张贴了标准的MVC控制器上的JSON数据。

This is very frustrating to work around and I don't know an easy way to accept both a query string and posted JSON data on a standard MVC controller.

我有一个回调控制器与行动方法接受各种数据(通过GET,通过表单POST,通过JSON POST,通过JSON POST瓦特/查询字符串,等等)从不同的第三方服务器。这些商人型的回调,我有超过用来传递信息的格式或方法无法控制的。我只需要接受回调和处理应该在那里的信息。

I have a "callback controller" with Action methods that accept various data (via GET, via form POST, via JSON POST, via JSON POST w/ a Query String, etc.) from different third-party servers. These are merchant-type callbacks where I have no control over the formats or methods used to convey information. I just need to accept the callbacks and process the information that should be there.

在我的控制器所有这一切工作正常,但中JSON POST瓦特/查询字符串的情况。

All of it works fine in my Controller, except the case of "JSON POST w/ a Query String".

此出现(至少对我来说)是在标准的ASP.NET MVC控制器的一个缺点。 ???

任何人都可以提出一个解决的办法,可以在一个标准的ASP.NET MVC控制器一起使用?

Can anyone suggest a solution to this that can be used in a standard ASP.NET MVC controller?

推荐答案

如果你考虑到一个事实,即ASP.NET MVC模型绑定已读取流,所以你应该倒带你最初的做法应该工作:

Your initial approach should work if you take into consideration the fact, that ASP.NET MVC model binding has already read the stream, so you should rewind it:

[HttpPost]
public ActionResult Callback(string secret)
{
    Request.InputStream.Seek(0, SeekOrigin.Begin);
    string jsonData = new StreamReader(Request.InputStream).ReadToEnd();

    // ...
}

这篇关于ASP.NET MVC读取原始JSON数据后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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