ASP.NET 2.0 でREST風Webサービスを作成するhttp://d.hatena.ne.jp/kendik/20110723/1311439270
という すばらしい記事を見て作ってみました。
何点か ぱっとわからないことがありました。
ですので、今後のために書きました。
最初 .asmx で作っていました。
新しいクラスは、picture.cs というファイルで作りました。
picture.cs
namespace hoge.REST
{
public class picture : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest( HttpContext context )
{
using( var conn = new SqlConnection( connectionString ) )
using( var cmd = conn.CreateCommand() ) {
SqlCommand com = new SqlCommand( "select hoge", conn );
com.Parameters.AddWithValue( "@id", context.Session["HOGE"].ToString() );
SqlDataAdapter dp = new SqlDataAdapter( com );
// 中略
context.Response.ContentType = "image/png";
byte[] buffer = stream.ToArray();
context.Response.ContentType = "image/png";
context.Response.BinaryWrite( buffer );
context.Response.Flush();
}
}
public bool IsReusable
{
get { return false; }
}
}
}
記事のとおりですと Session情報がとれませんで、調べた結果
IRequiresSessionState
を追加すると Session情報が取得できました。
http://forums.asp.net/t/1058823.aspx/1
config.xml
<system.web>
<httpHandlers>
<add type="hoge.REST.picture, hoge" path="Service1/*" verb="GET" />
</httpHandlers>
</system.web>
の add の意味は、
type 1番目:クラス名
type 2番目:DLL名
path:クライアントからのリクエストのパス
verb:アクセスの種類
だと思われます。
http://msdn.microsoft.com/ja-jp/library/7d6sws33(v=vs.80).aspx
ちなみに <clear />
してしまうと 元々作っていた物にアクセスできなくなったので、はずしました。