首頁  >  文章  >  後端開發  >  談談Request和Response這兩個物件的使用

談談Request和Response這兩個物件的使用

零下一度
零下一度原創
2018-05-26 10:32:494299瀏覽


ASP.NET物件有以下幾個:

本文從「asp.net中透過from表單submit提交到後台的實例」來談談RequestResponse這兩個物件的使用。

(一)引入實例

        前台1d6e7d87652dd104f173dbf7284e2799中的表單程式碼:

<body>
    <form method="get" action="WebForm1.aspx">
        <table style="width:50%;">
            <tr>
                <td> </td>
                <td>
                    <input id="text1"  name="txtUserName" type="text" /></td>
                <td class="auto-style1"> </td>
            </tr>
            <tr>
                <td> </td>
                <td>
                    <input id="text2"  name="txtUserPwd" type="text" /></td>
                <td class="auto-style1"> </td>
            </tr>
            <tr>
                <td> </td>
                <td>
                    <input id="ccc" type="submit" value="提交" /></td>
                <td class="auto-style1"> </td>
            </tr>
        </table>
    </form>
</body>

        表單中的method方法,即表單的提交方法。

        表單中的action方法,指定表單的提交目標。

        action=「WebFrom1」,指的是表單的提交後指向WebForm1窗體。在該路徑的頁面中,用Request.From可以接受到Post方法的資料。用Requet.QuestString可以接受Get的資料。具體用Post還是用Get,可以在表單中的Method屬性中設定。

       # 後台的C#程式碼:

    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Request三种获取表单值得方法。

            #region  对于post方法递交表单的获取值方法
            //string userName = Request.Form.Get("txtUserName").ToString();
            //string userPwd = Request.Form.Get("txtUserPwd").ToString();
            #endregion

            #region  对于get方法递交表单的获取值方法
            //string userName = Request.QueryString["txtUserName"].ToString();  
            //string userPwd = Request.QueryString["txtUserPwd"].ToString();
            #endregion
           
            #region  对两者方法都适用的方法,运用Reuqest的索引值去获取所要求的表单值
            string userName = Request["txtUserName"].ToString();
            string userPwd = Request["txtUserPwd"].ToString();
            #endregion
            Response.Write("登陆的用户名为:" + userName + ";密码为:" + userPwd);

            if (userName=="a"&&userPwd=="b")
            {
                Response.Redirect("WebForm2.aspx");
            }
            else
            {
                Response.Redirect("login.html");
            }       
        }
    }

(二)Request物件與Response物件用法摘要

一、Request物件

         Request三種取得表單值得方法的具體實現,我已都寫入到後代程式碼的實例中了,在這裡就不贅述。

        這裡要注意的是:get和post方法的差異如下:

# get方法提交,直接定義一個url就可以傳值。缺點是,傳的值是明碼顯示的。因為瀏覽器顯示的字元是有長度的,所以他的資料顯示的時候是受限制的。

         post提交,是將資料作為整個集合提交,且對於post方法傳值的方法傳的參數不會在url中以明碼顯示。

二、Response物件

         response對象,最主要的用到的方法是respone.write(string )和responst.redirect(url).

         response.write(string)的作用是從伺服器端傳送至客戶端(寫入資料)。

         response.rediec("url")的功能是伺服器端重新導向另一個網頁。

【相關推薦】

1. 總結Asp.net內建物件之Request物件使用實例

2. 分享一個Request物件小案例

3. 分享asp中request物件五個取得客戶端資料的方法

4. 詳解ASP.NET 系統物件之Request

以上是談談Request和Response這兩個物件的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn