ASP.NET Webフォーム Requestオブジェクト

概要

Pageクラスメンバーの1つ。
クライアント(ブラウザ)からサーバーへアクセスする際のリクエスト情報を取得する。
リクエスト情報は
・HTTPメソッド
・リクエストヘッダー
・リクエスト本体(クエリ情報)
から成り、Requestオブジェクトにはそれぞれの情報を取得する為の各種プロパティが用意されている。

ポストデータの取得

Request.Formプロパティ
※~.aspx
<body>
  <form id=”myForm” runat=”server” method=”post”>
    <asp:TextBox ID=”myText” runat=”server”></asp:TextBox>
    <asp:Button ID=”myButton” runat=”server” Text=”Button” OnClick=”myButton_Click” />
  </form>
</body>
※~.aspx.cs
public partial class index : System.Web.UI.Page
{
  protected void myButton_Click(object sender, EventArgs e)
  {
    string textValue = Page.Request.Form[name:”myText”];
     string textValue = this.myText.Text; と同義(こちらの方が良い)
  }
}

クエリ情報の取得

Request.QueryStringプロパティ
※~.aspx
<body>
  <form id=”myForm” runat=”server” method=”post”>
    <asp:HyperLink
      ID=”myLink”
      runat=”server”
      NavigateUrl=”~/index.aspx?prm1=10&prm2=test”>
      リンク
    </asp:HyperLink>
  </form>
</body>
※~.aspx.cs
public partial class index : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string prm1 = Page.Request.QueryString[name: “prm1”];
    ⇒ prm1 : 10
    string prm2 = Page.Request.QueryString[name: “prm2”];
    ⇒ prm2 : test
  }
}

リクエストヘッダー情報の取得

public partial class index : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    言語設定取得
    string language = Page.Request.UserLanguages[0].ToString();
    ⇒ language : ja等(ブラウザ設定に依る)
    
    (言語設定の利用方法は「ASP.NET 多言語対応」参照)
    
    HTTPメソッド取得
    string method = Page.Request.HttpMethod.ToString();
    ⇒ method : get等
    リンク元ページURL取得(遷移時)
    string url = Page.Request.UrlReferrer.ToString();
  }
}

リクエストURLの取得

仮想/物理パスやクエリ情報の有無によってプロパティが異なる。
アプリケーションパス
「ファイル名を指定して実行」にファイル名だけを入力して実行できる様なアプリケーション専用のパス
追加パス
パス形式パラメーター(例ではplus)
仮想パス
インタネット上に公開しているURL
物理パス
Webサーバー上のファイルパス

※index.aspx
<body>
 <form id=”form1″ runat=”server”>
  <asp:HyperLink
   ID=”HyperLink1″
   runat=”server”
   NavigateUrl=”~/next.aspx/plus?prm1=100&prm2=test”
   Text=”リンク”
   Target=”_blank”></asp:HyperLink>
 </form>
</body>
※next.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
  Response.Write(“ApplicationPath : ” + Page.Request.ApplicationPath);
  Response.Write(“PhysicalApplicationPath : ” + Page.Request.PhysicalApplicationPath);
  Response.Write(“CurrentExecutionFilePath : ” + Page.Request.CurrentExecutionFilePath);
  Response.Write(“FilePath : ” + Page.Request.FilePath);
  Response.Write(“Path : ” + Page.Request.Path);
  Response.Write(“PhysicalPath : ” + Page.Request.PhysicalPath);
  Response.Write(“RawUrl : ” + Page.Request.RawUrl);
  Response.Write(“Url : ” + Page.Request.Url.ToString());
  Response.Write(“PathInfo : ” + Page.Request.PathInfo);
  
}
※結果(next.aspx)
ApplicationPath : /
PhysicalApplicationPath : C:\Test\
CurrentExecutionFilePath : /next.aspx
FilePath : /next.aspx
Path : /next.aspx/plus
PhysicalPath : C:\Test\next.aspx
RawUrl : /next.aspx/plus?prm1=100&prm2=test
Url : http:localhost:63707/next.aspx/plus?prm1=100&prm2=test
PathInfo : /plus

プロパティ 種類 物理/仮想 クエリ情報 追加パス
ApplicationPath アプリケーションパス 仮想 × ×
PhysicalApplicationPath アプリケーションパス 物理 × ×
CurrentExecutionFilePath ファイルパス 仮想 × ×
FilePath ファイルパス 仮想 × ×
Path ファイルパス 仮想 ×
PhysicalPath ファイルパス 物理 × ×
RawUrl ファイルパス 仮想
Url ファイルパス 仮想(フルURL)
PathInfo 追加パス 仮想 ×

クッキーの取得

ローカルに保存されるテキスト情報
ブラウザを終了してもPCを再起動しても次のページに遷移しても削除されるまでは残る。
※index.aspx
<body>
  <form id=”myForm” runat=”server”>
    <asp:TextBox ID=”myText” runat=”server”></asp:TextBox><br />
    <asp:Button ID=”btnSave” runat=”server” Text=”保存” OnClick=”btnSave_Click” />
  </form>
</body>
※index.aspx.cs
public partial class index : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      if (Request.Cookies[“email”] != null)
      {
        クッキーの内容をテキストボックスに表示
        this.myText.Text = Request.Cookies[“email”].Value;
      }
    }
  }
  
  protected void btnSave_Click(object sender, EventArgs e)
  {

    クッキー作成
    (テキストボックスの内容をクッキーに保存)
    HttpCookie myCookie = new HttpCookie(name: “email”, value: this.myText.Text);
    
    クッキー有効期限を7日間に設定
    myCookie.Expires = DateTime.Now.AddDays(value: 7);
    
    SSL通信のみを許可する
    myCookie.Secure = true;
    
    HTTPプロトコル経由でのみアクセスできる
    (古いブラウザでは非対応)

    myCookie.HttpOnly = true;
    
    クッキーが有効になるドメイン/フォルダを設定
    myCookie.Domain = “http:office-yone.com/”;
    
    クッキーをクライアントに送信
    Response.AppendCookie(cookie: myCookie);
  }
}