无法读取Request.Form变量

时间:2014-11-20 12:52:07

标签: c# asp.net visual-studio

我无法从非常简单的外部页面发布到简单的aspx页面读取request.form名称/值对。这适用于其他环境,但不适用于我的本地开发环境。我正在使用VS2013 Update 4.在web.config中是否有一些安全设置阻止值被发布?我做错了什么?

这是我的发送页面(sender.html):

<html>
<body>
    Post values to receiver.aspx page  
    <form action="http://localhost:61034/receiver.aspx" method="post" enctype="application/x-www-form-urlencoded ">

        <input name="Field1" type="text" value="1" id="Field1" /><br />
        <input name="Field2" type="text" value="some text information here" id="Field2" /><br />

        <input id="Submit1" type="submit" value="submit" />

    </form>

</body>
</html>

这是我的接收页面(receiver.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="receiver.aspx.cs" Inherits="receiver"  %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body bgcolor="#FFFFFF">
    <form id="form1" runat="server">
        <%
            string f1 = Request.Form["Field1"];
            string f2 = Request.Form["Field2"];
            Response.Write("Field1= " + f1 );
            Response.Write("<br>Field2= " + f2);

        %>
    </form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

尝试在代码中使用HttpContext.Current

string f1 = System.Web.HttpContext.Current.Request.Form["Field1"];
string f2 = System.Web.HttpContext.Current.Request.Form["Field2"];
System.Web.HttpContext.Current.Response.Write("Field1= " + f1 );
System.Web.HttpContext.Current.Response.Write("<br>Field2= " + f2);

您也可以使用如下导入指令

<%@ Import namespace="MyProgram.MyNamespace" %>

或使用您的root web.config文件,如下所示

<system.web>    
<pages>
   <namespaces>
      <add namespace="System" />
      <add namespace="System.Collections" />
      <add namespace="System.Collections.Specialized" />
      <add namespace="System.Configuration" />
      <add namespace="System.Text" />
      <!-- etc -->
   </namespaces>
</pages>
</system.web>

答案 1 :(得分:0)

您可能将较小的值设置为最大请求长度限制。您应该检查web.config

<system.web>
  <httpRuntime maxRequestLength="10240" />
</system.web>

maxRequestLength值以KB为单位。或者您只能为您的页面设置它:

<location path="receiver.aspx">
    <system.web>
        <httpRuntime maxRequestLength="10240" />
    </system.web>
</location>

10240表示10Mb。

另外,请确保在您的aspx页面中禁用Request Validation

<@ Page validateRequest="false" %>

答案 2 :(得分:0)

我试图重现这个问题。我在Visual Studio 2012和2013更新4中都进行了测试。它对我来说非常合适。

enter image description here

这是代码

<强>发送方

<!DOCTYPE html>
<html>
<body>
    Post values to receiver.aspx page
    <form action="http://localhost:50226/receiver.aspx" method="post" enctype="application/x-www-form-urlencoded ">

        <input name="Field1" type="text" value="1" id="Field1" /><br />
        <input name="Field2" type="text" value="some text information here" id="Field2" /><br />

        <input id="Submit1" type="submit" value="submit" />
    </form>
</body>
</html>

<强>接收器

<%@ Page Title="Home Page" Language="C#"  AutoEventWireup="true" CodeBehind="receiver.aspx.cs" Inherits="WebFormsSample._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body bgcolor="#FFFFFF">
    <form id="form1" runat="server">
        <%
            string f1 = Request.Form["Field1"];
            string f2 = Request.Form["Field2"];
            Response.Write("Field1= " + f1 );
            Response.Write("<br>Field2= " + f2);

        %>
    </form>
</body>
</html>

我能看到的唯一区别是Inherits="WebFormsSample._Default"我不确定是否有任何问题。