在 C#/ASP.NET 中获取 POST 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/976613/
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
Get POST data in C#/ASP.NET
提问by
I am trying to get POST data, but I'm having no luck. My code is below. When I click the form button nothing happens.
我正在尝试获取 POST 数据,但我没有运气。我的代码如下。当我单击表单按钮时,什么也没有发生。
I expected at least my IDE to snap at A.Ret()
, but nothing happens whatsoever.
我希望至少我的 IDE 能够捕捉到A.Ret()
,但没有任何反应。
File Test.cs
文件测试.cs
using System.Web;
public class A
{
public static string ret() {
var c = HttpContext.Current;
var v = c.Request.QueryString; // <-- I can see get data in this
return c.Request.UserAgent.ToString();
return c.Request.UserHostAddress.ToString();
return "woot";
}
}
File Default.aspx
文件 Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="aspnetCSone._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server" method="post" action="Default.aspx">
<input type=hidden name="AP" value="99" />
<input type=button value="Submit" />
<div>
<a id="aa">a</a>
<% = A.ret() %>
</div>
</form>
</body>
</html>
回答by Jon Skeet
Try using:
尝试使用:
string ap = c.Request["AP"];
That reads from the cookies, form, query string or server variables.
从 cookie、表单、查询字符串或服务器变量中读取。
Alternatively:
或者:
string ap = c.Request.Form["AP"];
to just read from the form's data.
只是从表单的数据中读取。
回答by Darin Dimitrov
c.Request["AP"]
will read posted values. Also you need to use a submit button to post the form:
c.Request["AP"]
将读取发布的值。您还需要使用提交按钮来发布表单:
<input type="submit" value="Submit" />
instead of
代替
<input type=button value="Submit" />
回答by e-info128
The following is OK in HTML4, but not in XHTML. Check your editor.
以下内容在 HTML4 中可以,但在 XHTML 中不行。检查你的编辑器。
<input type=button value="Submit" />
回答by Mike Gledhill
I'm a little surprised that this question has been asked so many times before, but the most reuseable and friendly solution hasn't been documented.
我有点惊讶这个问题之前已经被问过很多次了,但是最可重用和最友好的解决方案还没有被记录下来。
I often have webpages using AngularJS, and when I click on a Save button, I'll "POST" this data back to my .aspx page or .ashx handler to save this back to the database. The data will be in the form of a JSON record.
我经常有使用 AngularJS 的网页,当我单击“保存”按钮时,我会将这些数据“POST”回我的 .aspx 页面或 .ashx 处理程序以将其保存回数据库。数据将采用 JSON 记录的形式。
On the server, to turn the raw posted data back into a C# class, here's what I would do.
在服务器上,要将原始发布的数据转换回 C# 类,这就是我要做的。
First, define a C# class which will contain the posted data.
首先,定义一个包含发布数据的 C# 类。
Supposing my webpage is posting JSON data like this:
假设我的网页正在发布这样的 JSON 数据:
{
"UserID" : 1,
"FirstName" : "Mike",
"LastName" : "Mike",
"Address1" : "10 Really Street",
"Address2" : "London"
}
Then I'd define a C# class like this...
然后我会像这样定义一个 C# 类......
public class JSONRequest
{
public int UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
}
(These classes can be nested, but the structure must match the format of the JSON data. So, if you're posting a JSON User record, with a list of Order records within it, your C# class should also contain a List<>
of Order records.)
(这些类可以嵌套,但结构必须与 JSON 数据的格式匹配。因此,如果您要发布 JSON 用户记录,其中包含订单记录列表,则您的 C# 类还应包含List<>
订单记录.)
Now, in my .aspx.cs or .ashx file, I just need to do this, and leave JSON.Net to do the hard work...
现在,在我的 .aspx.cs 或 .ashx 文件中,我只需要这样做,而让 JSON.Net 来完成这些艰苦的工作......
protected void Page_Load(object sender, EventArgs e)
{
string jsonString = "";
HttpContext.Current.Request.InputStream.Position = 0;
using (StreamReader inputStream = new StreamReader(this.Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}
JSONRequest oneQuestion = JsonConvert.DeserializeObject<JSONRequest>(jsonString);
And that's it. You now have a JSONRequest
class containing the various fields which were POSTed to your server.
就是这样。您现在有一个JSONRequest
包含各种字段的类,这些字段已发布到您的服务器。