Request.Form在VS 2015中无效

时间:2016-04-02 17:23:42

标签: visual-studio-2015 request.form

我无法理解为什么以下代码在VS 2012中工作而不在VS 2015中工作:

getdata.html:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <form id="getcase" name="getcase" action="submit2crm.aspx">
            <input id="Text1" type="text" name="txt" />
            <input id="Submit1" type="submit" value="submit" />
        </form>
    </body>
    </html>

submit2crm.aspx.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class w2c_submit2crm : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
         {

            System.Text.StringBuilder displayValues = new System.Text.StringBuilder();
    System.Collections.Specialized.NameValueCollection postedValues = Request.Form;
    String nextKey;
    for (int i = 0; i < postedValues.AllKeys.Length; i++)
    {
        nextKey = postedValues.AllKeys[i];
        if (nextKey.Substring(0, 2) != "__")
        {
            displayValues.Append("<br>");
            displayValues.Append(nextKey);
            displayValues.Append(" = ");
            displayValues.Append(postedValues[i]);
        }
    }
    Label1.Text = displayValues.ToString();
}
}
表单提交后,

postedValues仍为空。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

自己发现问题: 这是友好的URL功能。为了使Request.Form正常工作,我在配置文件RouteConfig.cs中将settings.AutoredirectMode更改为Off:

    var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Off;
        routes.EnableFriendlyUrls(settings);

RouteConfig.cs位于App_Start文件夹中。

本文帮助我了解问题所在: http://www.mikesdotnetting.com/article/293/request-form-is-empty-when-posting-to-aspx-page