asp.net-mvc 直接访问帖子数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5885457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:10:16  来源:igfitidea点击:

Access post data directly

asp.net-mvc

提问by AndrewC

I have an action in one of my controllers that is going to receive HTTP POST requests from outside of my MVC website.

我在我的一个控制器中有一个操作,它将接收来自我的 MVC 网站外部的 HTTP POST 请求。

All these POST requests will have the same parameters and I need to be able to parse the parameters.

所有这些 POST 请求都将具有相同的参数,我需要能够解析这些参数。

How can I access the post data from within the action?

如何从操作中访问帖子数据?

This is potentially a very simple question!

这可能是一个非常简单的问题!

Thanks

谢谢

回答by Jim Bolla

The POST data from your HTTP Reques can be obtained at Request.Form.

可以在 获取来自 HTTP 请求的 POST 数据Request.Form

回答by Brady Moritz

string data = new System.IO.StreamReader(Request.InputStream).ReadToEnd(); 

回答by Tomas McGuinness

Use

Request.InputStream 

This will give you raw access to the body of the HTTP message, which will contain all the POST variables.

这将为您提供对 HTTP 消息正文的原始访问权限,其中包含所有 POST 变量。

http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx

http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx

回答by TWilly

I was trying to access the POST data after I was inside of the MVC controller. The InputStream was already parsed by the controller so I needed to reset the position of the InputStream to 0 in order to read it again.

在我进入 MVC 控制器之后,我试图访问 POST 数据。InputStream 已经被控制器解析,所以我需要将 InputStream 的位置重置为 0 以便再次读取它。

This code worked for me...

这段代码对我有用...

 HttpContext.Current.Request.InputStream.Position = 0;
 var result = new System.IO.StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();

回答by Mahmoud Abdallah

Stream req = Request.InputStream;
            req.Seek(0, System.IO.SeekOrigin.Begin);
            string json = new StreamReader(req).ReadToEnd();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            dynamic items = serializer.Deserialize<object>(json);
            string id = items["id"];
            string image = items["image"];

///you can access paramters by name or index

///您可以通过名称或索引访问参数

回答by Michael Gattuso

The web server shouldn't care where the request is coming from. If your client application has a input control called username and it posts to your application it will pick up the same as if your posted if from your own application with an input called username.

Web 服务器不应该关心请求来自哪里。如果您的客户端应用程序有一个名为 username 的输入控件,并且它发布到您的应用程序,它将像您从您自己的应用程序中发布的输入控件一样使用名为 username 的输入。

One huge caveat is if you have implemented AntiForgeryValidation which will cause a big headache to allow an outside form to post.

一个巨大的警告是,如果您已经实施了 AntiForgeryValidation,这将导致允许外部表单发布的大麻烦。