asp.net中的post和get请求操作

ASP.NET中的post和get请求操作

主要是为了测试一下当post请求中有get参数的时候,服务器端是如何处理这两种不同的参数的。是一起当post参数处理,还是分开处理。

客户端post_test.html

<html>

  <head>

    Post_Get Test

  </head>

  <body>

    <form method="post" name="form" action="http://localhost:53994/post_get/Default.aspx?getvar=2">

    <input type="text" name="postvar" value="4" />

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

    </form>

  </body>

</html>

服务器端 Default.aspx

<body>

    <form id="form1" runat="Server">

    <div>

        <asp:Label ID="Label4" runat="server" Text="postRequest:">

        </asp:Label><asp:Label ID="Label1" runat="server" ></asp:Label>

        <br />

        <asp:Label ID="Label5" runat="server" Text="postFormGet:">

        </asp:Label><asp:Label ID="Label2" runat="server" ></asp:Label>

        <br />

        <asp:Label ID="Label6" runat="server" Text="getQueryString:">

        </asp:Label><asp:Label ID="Label3" runat="server" ></asp:Label>

        <br />

        <asp:Label ID="Label7" runat="server" Text="getFormGet:"></asp:Label>

        <asp:Label ID="Label8" runat="server" ></asp:Label>

        <br />

        <asp:Label ID="Label9" runat="server" Text="getRequest:"></asp:Label>

        <asp:Label ID="Label10" runat="server" ></asp:Label>

    </div>

    </form>

</body>

服务器端Default.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 _Default : System.Web.UI.Page

{

    string post_name1 ;

    string post_name2;

    string get_name1;

    string get_name2;

    string get_name3;

    protected void Page_Load(object sender, EventArgs e)

    {

        FormTest();

    }

    private void FormTest()

    {

        if (Request["postvar"] != null)

        {

            post_name1 = Request["postvar"].ToString();

        }

        if (Request.Form.Get("postvar") != null)

        {

            post_name2 = Request.Form.Get("postvar").ToString();

        }

        if (Request.QueryString["getvar"] != null)

        {

            get_name1 =Request.QueryString["getvar"].ToString();

        }

        if (Request.Form.Get("getvar") != null)

        {

            get_name2 = Request.Form.Get("getvar").ToString();

        }

        if (Request["gettvar"] != null)

        {

            get_name3 = Request["gettvar"].ToString();

        }

        Label1.Text = post_name1;

        Label2.Text = post_name2;

        Label3.Text = get_name1;

        Label8.Text = get_name2;

        Label10.Text = get_name3;

    }

}

运行服务器端,打开post_test.html,结果如下图所示

表明当表单提交为post且含有get参数时,Request.QueryString和Request才能获得get参数,其他方法都只能获得post参数

原文地址:https://www.cnblogs.com/baozi/p/3200013.html