C# - 如何列出发布到 ASPX 页面的变量名称和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1495431/
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-08-06 18:06:08  来源:igfitidea点击:

C# - How to list out variable names and values posted to ASPX page

c#htmlwebforms

提问by NYARROW

I am dynamicaly generating a HTML form that is being submitted to a .aspx webpage. How do I determine in the resulting page what variable names were submitted and what the values were? Using: Request["VarName"].toChar(); Works, but assumes that I know all of the variable names. How can I get the names and values?

我正在动态生成一个提交到 .aspx 网页的 HTML 表单。如何在结果页面中确定提交了哪些变量名称以及值是什么?使用:Request["VarName"].toChar(); 有效,但假设我知道所有变量名称。如何获取名称和值?

Ideally, the solution would work for both POST and GET submissions...

理想情况下,该解决方案适用于 POST 和 GET 提交...

Thank you!

谢谢!

采纳答案by JohnFx

Have you tried something like this:

你有没有尝试过这样的事情:

For post request:

对于发布请求:

foreach(string key in Request.Form.Keys ) 
{
   Response.Write ( Request.Form[key] );
}  

For a get request:

对于获取请求:

foreach(string key in Request.QueryString.Keys ) 
{
   Response.Write ( Request.QueryString[key] );
}

回答by Jon Skeet

Each of Request.Cookies, Request.Form, Request.QueryStringand Request.ServerVariablesis a NameValueCollectionor similar. You can ask each of those for its keys, and proceed from there.

每一个Request.CookiesRequest.FormRequest.QueryString和请求。ServerVariables是一个NameValueCollection或类似的。您可以向每个人询问其密钥,然后从那里继续。

foreach (string key in Request.QueryString.Keys)
{
    string value = Request.QueryString[key];
    // etc
}

Quite which collections you need will depend on the purpose of this - if it's just for diagnostics, I'd be tempted to dump them all out.

您需要哪些集合取决于此目的 - 如果它只是用于诊断,我很想将它们全部丢弃。

回答by Guffa

Don't read from the Requestcollection, that is a composite collection that contains form and querystring values, but also cookies and server variables. Read from the specific collections where you want the data from, i.e. the Request.Form(for POST) and Request.QueryString(for GET).

不要从Request集合中读取,这是一个包含表单和查询字符串值的复合集合,还有 cookie 和服务器变量。从您想要数据的特定集合中读取,即Request.Form(对于 POST)和Request.QueryString(对于 GET)。

This is how you can get all the keys and values into a string:

这是将所有键和值放入字符串的方法:

StringBuilder builder = new StringBuilder();
builder.AppendLine("Form values:");
foreach (string key in Request.Form.Keys) {
   builder.Append("  ").Append(key).Append(" = ").AppendLine(Request.Form[key]);
}
builder.AppendLine("QueryString values:");
foreach (string keu in Request.QueryString.Keys) {
   builder.Append("  ").Append(key).Append(" = ").AppendLine(Request.QueryString[key]);
}
string values = builder.ToString();

回答by David Andres

JohnFx's method works well. You can also include query string, server variable, and cookie data into the mix by using the Request.Params collection, as in:

JohnFx 的方法效果很好。您还可以使用 Request.Params 集合将查询字符串、服务器变量和 cookie 数据包含到组合中,如下所示:

foreach(string key in Request.Params.Keys)
{
  Response.Write(String.Format("{0}: {1}<br />", key, Request.Params[key]));
}

You need to be careful when using the Request.Paramscollection. If a QUERYSTRING variable and FORM variable both share key "Name" and have values "Val1" and "Val2", then the value of Request.Params["Name"]will be "Val1, Val2."

使用Request.Params集合时需要小心。如果 QUERYSTRING 变量和 FORM 变量都共享键“Name”并且具有值“Val1”和“Val2”,则值Request.Params["Name"]将是“Val1, Val2”。

回答by Cade Roux

You should use Request.Formas a collection. Note that multi-valued parameters (a multi-select) are turned into a collection themselves. There's an example on this page.

您应该使用Request.Form作为集合。请注意,多值参数(多选)本身会变成一个集合。这个页面上有一个例子。

回答by Rob

In the declaration of the .ASPX add Trace='True'

在 .ASPX 的声明中添加 Trace='True'

<%@ Page Language="VB" Trace="True" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

回答by Tarik

In addition to the answers above, if you want to combine collection of QueryString, Form, ServerVariables, and Cookies items, then the best way is to use HttpRequest.Params

除了上面的答案,如果你想组合 QueryString、Form、ServerVariables 和 Cookies 项的集合,那么最好的方法是使用HttpRequest.Params

回答by Rob

In the declaration of the .ASPX add Trace='True'

在 .ASPX 的声明中添加 Trace='True'

<%@ Page Language="CS" Trace="True" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

You will be able to see the Forms data, Session, Application data etc.

您将能够看到表单数据、会话、应用程序数据等。