【问题标题】:NameValueCollection from Form.Post show FIELDS and VALUES来自 Form.Post 的 NameValueCollection 显示 FIELDS 和 VALUES
【发布时间】:2012-04-06 20:30:06
【问题描述】:

我需要从 POST 中获取所有字段和值。

我有一个只返回字段但不返回值的跟随。

NameValueCollection authForm = Request.Form;
String[] a = authForm.AllKeys;

for (i = 0; i < a.Length; i++)
{
    frm += ("Form: " + a[i] + " : " + "<br>");
}

Response.Write(frm);

我可以在 frm 字符串中添加什么来显示 VALUES ?

更新:

我用的初始代码

    NameValueCollection authForm = Request.Form;
    foreach (string key in authForm.AllKeys)
    {
        frm += ("Key: " + key + ", Value: " + authForm[key] + "<br/>");
    }

效果很好。我将尝试下面的新变化。

【问题讨论】:

    标签: c# asp.net collections


    【解决方案1】:
    NameValueCollection authForm = Request.Form;
    StringBuilder sb = new StringBuilder();
    foreach (string key in authForm.AllKeys)
    {
        sb.AppendFormat(
            "Key: {0}, Value: {1}<br/>", 
            HttpUtility.HtmlEncode(key), 
            HttpUtility.HtmlEncode(authForm[key])
        );
    }
    Response.Write(sb.ToString());
    

    【讨论】:

    • +1,但如果你使用string.FormatStringBuilder.AppendFormat 会更漂亮:-)
    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2011-11-22
    • 2014-10-17
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多