HTMLフォームからの投稿を受け取るジェネリックハンドラ、Webフォームを作成します。
プロジェクトの作成
ASP.NETのプロジェクトを新規作成します。テンプレートの選択で[Empty]を指定します。
![](https://resources.ipentec.com/document/image/csharp-asp-net-create-generic-handler-to-get-form-data-2015-02?width=600)
ソリューションエクスプローラから右クリックポップアップメニューの[追加]メニューの[新しい項目...]メニューを選択します。
[新しい項目の追加]ダイアログボックスが表示されます。
[Web]カテゴリの中から[ジェネリック ハンドラー]を追加します。ファイル名は"GetForm.ashx"とします。
ソリューションエクスプローラから右クリックポップアップメニューの[追加]メニューの[HTMLページ]を選択します。ファイル名は"Form.html"とします。
コード
以下のコードを記述します。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form method="post" action="GetForm.ashx">
<div>Value-01<input name="value01" type="text" /></div>
<div>Value-02<input name="value02" type="text" /></div>
<input type="submit" value="POST" />
</form>
</body>
</html>
解説
<form method="post" action="GetForm.ashx">
<div>Value-01<input name="value01" type="text" /></div>
<div>Value-02<input name="value02" type="text" /></div>
<input type="submit" value="POST" />
</form>
テキスト入力フィールドが2つとサブミットボタンがあるフォームを作成します。テキスト入力フィールドのnameは"value01","value02"とします。フォームのアクションはPOSTにします。またaction(データ送信先)は先に追加した"GetForm.ashx"のジェネリックハンドラーを指定します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HtmlFormGenericHandler
{
/// <summary>
/// GetForm の概要の説明です
/// </summary>
public class GetForm : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.HttpMethod == "GET") {
context.Response.ContentType = "text/plain";
context.Response.Write("OK+");
}
else if (context.Request.HttpMethod == "POST") {
string value01 = context.Request.Form["value01"];
string value02 = context.Request.Form["value02"];
context.Response.ContentType = "text/html";
context.Response.Write("<html>");
context.Response.Write("<head></head>");
context.Response.Write("<body>");
context.Response.Write(string.Format("<p>value01 = {0:s}</p>", value01));
context.Response.Write(string.Format("<p>value02 = {0:s}</p>", value02));
context.Response.Write("</body>");
context.Response.Write("</html>");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
解説
if (context.Request.HttpMethod == "GET") {
...
}
else if (context.Request.HttpMethod == "POST") {
...
}
context.Request.HttpMethodでアクセスメソッドを取得します。今回はフォームからのポストに応答するため、メソッドがPOSTの場合のみ実装します。
string value01 = context.Request.Form["value01"];
string value02 = context.Request.Form["value02"];
フォームからPOSTされた情報はcontext.Request.Formで取得します。[]内にはHTMLフォーム側で設定した、POST元のフィールドの名前を指定します。
context.Response.ContentType = "text/html";
context.Response.Write("<html>");
context.Response.Write("<head></head>");
context.Response.Write("<body>");
context.Response.Write(string.Format("<p>value01 = {0:s}</p>", value01));
context.Response.Write(string.Format("<p>value02 = {0:s}</p>", value02));
context.Response.Write("</body>");
context.Response.Write("</html>");
受け取った情報をHTML形式で出力します。
実行結果
Webアプリケーションを配置し、"form01.htm"にアクセスします。下図の画面が表示されます。
テキストボックスに値を入力し、[POST]ボタンを押します。
ポストした情報が画面に表示されます。HTMLフォームからジェネリックハンドラーへ情報送信ができています。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-06
作成日: 2012-03-21