什么是Request.InputStream以及何时使用它? [英] what is Request.InputStream and when to use it?

查看:1885
本文介绍了什么是Request.InputStream以及何时使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是非常简单的。什么是 Request.InputStream 以及何时使用它。是否总是用来读取POST请求,或在其发送只有一些参数发送整个HTML的身体吗?为什么我不应该通过将其在Ajax请求发送数据作为参数传递给我的服务器端code?

在这个例子我可以通过在数据参数:或者我可以读取 Request.InputStream 。什么时候应该使用哪一个呢?

例如:

在控制器:

 公众的ActionResult GetSomeData(字符串someData)
    {
        Request.InputStream.Position = 0;
        就是System.IO.StreamReader海峡=新就是System.IO.StreamReader(Request.InputStream);
        字符串SBUF = str.ReadToEnd();
        返回JSON(东西);
    }

Ajax请求:

  $阿贾克斯({
            键入:POST,
            网址:首页/ GetSomeData
            数据:{someData:'你好'},
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON
            成功:函数(MSG){
                警报(MSG);
                //将返回的HTML入的< DIV取代。
                $('#dvResult)HTML(MSG);
            }
        });


解决方案

Request.InputStream 允许您访问原始请求数据。如果这个数据,如使用一些标准格式格式化的应用程序/ x-WWW的形式urlen codeD 的multipart / form-data的或默认的模型绑定明白你不需要使用 Request.InputStream 其他格式。 ASP.NET将解析请求中的值,你将能够直接访问它们使用请求[...] 。当然,在ASP.NET MVC中,你甚至都不需要使用请求[...] ,因为你可以定义你的控制器动作将作为参数视图模型和离开模型绑定从请求转让其属性。

有,虽然您可能希望访问原始请求流病例。例如你已经发明了一些自定义的协议和客户端的请求流中发送一些自定义格式的数据。这些案件是非常罕见的,因为发明了定制协议是不是很常见。

现在回到你的问题。你的情况,你可以定义视图模型:

 公共类MyViewModel
{
    公共字符串SomeData {搞定;组; }
}

,你的控制器动作将作为参数:

 公众的ActionResult GetSomeData(MyViewModel模型)
{
    // model.SomeData将包含你好字符串,客户端发送
    返回JSON(东西);
}

和客户端我会建议你使用的是本机内置到现代的浏览器,以JSON的 JSON.stringify 方法上连载请求的Jav​​aScript字面成JSON字符串,而不是手工编写JSON像你一样:

  $。阿贾克斯({
    输入:POST,
    网址:'主页/ GetSomeData',
    数据:JSON.stringify({someData:'你好'}),
    的contentType:应用/ JSON的;字符集= UTF-8,
    成功:函数(MSG){
        警报(MSG);
        //将返回的HTML入的< DIV取代。
        $('#dvResult)HTML(MSG);
    }
});

Question is really simple. What is Request.InputStream and when to use it. Is it always used to read entire html body sent in the post request or only some parameters sent in it? Why should i not send data as a parameter to my server side code by passing it in the Ajax request?

In the example i can either pass the parameter in the data: or i can read the parameter in the Request.InputStream. When should i use which one?

Example:

In controller:

    public ActionResult GetSomeData(string someData)
    {
        Request.InputStream.Position = 0;
        System.IO.StreamReader str = new System.IO.StreamReader(Request.InputStream);
        string sBuf = str.ReadToEnd();
        return Json("something");
    }

Ajax Request:

        $.ajax({
            type: "POST",
            url: "Home/GetSomeData",
            data: "{someData:'Hello'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg);
                // Insert the returned HTML into the <div>.
                $('#dvResult').html(msg);
            }
        });

解决方案

Request.InputStream allows you to access the raw request data. If this data is formatted using some standard format such as application/x-www-form-urlencoded or multipart/form-data or some other format that the default model binder understands you do not need to use Request.InputStream. ASP.NET will parse the request values and you will be able to access them directly using Request[...]. Of course in ASP.NET MVC you don't even need to use Request[...] because you can define a view model which your controller action will take as parameter and leave the model binder assign its properties from the request.

There are cases though when you might want to access the raw request stream. For example you have invented some custom protocol and the client sends some custom formatted data in the request stream. Those cases are very rare since inventing custom protocols is not very common.

Now back to your question. In your case you could define a view model:

public class MyViewModel
{
    public string SomeData { get; set; }
}

which your controller action will take as argument:

public ActionResult GetSomeData(MyViewModel model)
{
    // model.SomeData will contain the Hello string that the client sent
    return Json("something");
}

and on the client I would recommend you using the JSON.stringify method which is natively built into modern browsers to JSON serialize the request javascript literal into a JSON string instead of manually writing the JSON as you did:

$.ajax({
    type: 'POST',
    url: 'Home/GetSomeData',
    data: JSON.stringify({ someData: 'Hello' }),
    contentType: 'application/json; charset=utf-8',
    success: function (msg) {
        alert(msg);
        // Insert the returned HTML into the <div>.
        $('#dvResult').html(msg);
    }
});

这篇关于什么是Request.InputStream以及何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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