在ASP.NET中读取请求体 [英] Read Request Body in ASP.NET

查看:530
本文介绍了在ASP.NET中读取请求体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人如何读取ASP.NET请求主体?我使用REST客户端插件在Firefox,形成一个网站,我在本地托管资源的GET请求,并在请求体,我只是把字符串测试尝试读取它服务器

How does one read the request body in ASP.NET? I'm using the REST Client add-on for Firefox to form a GET request for a resource on a site I'm hosting locally, and in the Request Body I'm just putting the string "test" to try to read it on the server.

在服务器code(这是一个很简单的MVC动作)我有这样的:

In the server code (which is a very simple MVC action) I have this:

var reader = new StreamReader(Request.InputStream);
var inputString = reader.ReadToEnd();

但是,当我调试了进去, inputString 总是空空的。我不知道怎么回事(如萤火虫)确认请求主体确实正在正确发送,我想我只是假设附加正确这样做。也许我读值不正确?

But when I debug into it, inputString is always empty. I'm not sure how else (such as in FireBug) to confirm that the request body is indeed being sent properly, I guess I'm just assuming that the add-on is doing that correctly. Maybe I'm reading the value incorrectly?

推荐答案

也许我记错我的求学,但我认为GET请求不实际的有无的身体。 本页面状态的。

Maybe I'm misremembering my schooling, but I think GET requests don't actually have a body. This page states.

的HTML规范技术上定义GET和POST之间的差异,这样形成的数据前的手段是要带codeD(通过浏览器)到一个URL,而后者意味着表单数据到邮件正文中出现。

The HTML specifications technically define the difference between "GET" and "POST" so that former means that form data is to be encoded (by a browser) into a URL while the latter means that the form data is to appear within a message body.

因此​​,也许你正在做正确的事情,但你必须为了有一个消息正文POST数据?

So maybe you're doing things correctly, but you have to POST data in order to have a message body?

在回答您的评论,最正确的REST风格的方法是将每个值发送它自己的参数:

In response to your comment, the most "correct" RESTful way would be to send each of the values as its own parameter:

site.com/MyController/MyAction?id=1&id=2&id=3...

然后,如果你给它一个数组参数名称相同的动作会自动绑定这些:

Then your action will auto-bind these if you give it an array parameter by the same name:

public ActionResult MyAction(int[] id) {...}

或者,如果你是一个受虐狂你也许可以尝试一次拉出来的值的Request.QueryString 之一。

这篇关于在ASP.NET中读取请求体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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