简体   繁体   中英

Why do I get different results from the form post method “GET” vs. the “POST” method in the following?

I have the following HTML/ASP.NET code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Ny test</title>
    <link rel="stylesheet" type="text/css" href="StyleSheet.css" />
</head>
<body>

    <form action="Default.aspx" runat="server" method="post">

    Name: <input type="text" id="navn" runat="server"/>
    <input type="submit" id="submit" value="Submit!" runat="server" />
    <input type="reset" />
    <br />

    <%if (Request.Form["submit"] != null)
      {
          Response.Write("<br/>");
          Response.Write("Submit button pushed");
      }
      if (Request.Form["navn"] != null && Request.Form["navn"] != "")
      {
          Response.Write("<br/>");
          Response.Write("Name OK");
      } 
    %>
    </form>
</body>
</html>

When using the "POST" form post method I get the following output:

Submit button pushed Name OK

When using the "GET" form post method NOTHING is printed out?!

Request.Form contains information that is sent using POST . When you use GET the information will be in the Request.QueryString collection. In your case this means that Request.Form["submit"] is null .

If you want to support both then you would be able to use the Request.Item collection which includes values from:

  • Request.Cookies
  • Request.Form
  • Request.QueryString
  • Request.ServerVariables

However, doing this you may get some unexpected results if you use a parameter name that is used in one of the other collections.

要使用您的代码处理POSTGET ,您只需删除.Form ,即用Request["navn"]替换Request.Form["navn"] Request["navn"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM