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

概è¦

Pageクラスメンãƒãƒ¼ã®ï¼‘ã¤ã€‚
クライアント(ブラウザ)ã‹ã‚‰ã‚µãƒ¼ãƒãƒ¼ã¸ã‚¢ã‚¯ã‚»ã‚¹ã™ã‚‹éš›ã®ãƒªã‚¯ã‚¨ã‚¹ãƒˆæƒ…報をå–å¾—ã™ã‚‹ã€‚
リクエスト情報ã¯
・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);
  }
}

Follow me!