「ActionResult クラス (System.Web.Mvc)」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
imported>Administrator
imported>Administrator
 
(同じ利用者による、間の5版が非表示)
1行目: 1行目:
ActionResult クラスとは、[[ASP.NET MVC]]のアクション・メソッドの戻り値として使う各種クラス(ActionResultを継承したクラス)のベースクラス。
+
ActionResult クラスとは、[[ASP.NET MVC]]の[[アクション・メソッド]]の戻り値として使う各種クラス(ActionResultを継承したクラス)のベースクラスである。
  
アクションメソッド内でActionResult(を継承したクラス)の中身を穴埋めしてやると、ASP.NET MVCのフレームワーク側でその内容に従い[[HTML]]や[[JSON]]などのHTTPレスポンスが生成される。
+
[[アクションメソッド]]内でActionResult(を継承したクラス)の中身を穴埋めしてやると、[[ASP.NET MVC]]のフレームワーク側でその内容に従い[[HTML]]や[[JSON]]などのHTTPレスポンスが生成される。
  
たとえばViewResultであればビュー(HTMLテンプレート)を呼び出してレンダリングしたり、FileResultであればファイルを指定するだけでフレームワーク側で適切なHTTPレスポンスヘッダを生成し簡単にダウンロードダイアログを表示したりできる。
+
たとえば[[ViewResult]]であれば[[ビューエンジン]](HTMLテンプレートエンジン)を呼び出して[[HTML]]をレンダリングしたり、[[FileResult]]であればファイルを指定するだけでフレームワーク側で適切なHTTPレスポンスヘッダを生成し簡単にダウンロードダイアログを表示したりできる。
  
ContentResultだけでを用いてゴリゴリ書けば20世紀のPerlで書かれたウェブアプリケーションばりのソースコードになること受けあい。困ったらContentResult。まじおすすめ。
+
[[ContentResult]]だけでを用いてゴリゴリ書けば20世紀の[[Perl]]で書かれた[[ウェブアプリケーション]]ばりの[[ソースコード]]になること受けあい。困ったらContentResult。まじおすすめ。
  
 
== ActionResultの種類 ==
 
== ActionResultの種類 ==
14行目: 14行目:
 
** [[FileContentResult]] - バイト配列をHTTPレスポンスに書き込む。
 
** [[FileContentResult]] - バイト配列をHTTPレスポンスに書き込む。
 
** [[FilePathResult]] - 指定されたファイルパスの中身をHTTPレスポンスに書き込む。
 
** [[FilePathResult]] - 指定されたファイルパスの中身をHTTPレスポンスに書き込む。
** [[FileStreamResult]] - Streamクラスの中身をHTTPレスポンスに書き込む。
+
** [[FileStreamResult]] - [[Streamクラス]]の中身をHTTPレスポンスに書き込む。
 
* [[HttpUnauthorizedResult]] - 認証要求を送信する。
 
* [[HttpUnauthorizedResult]] - 認証要求を送信する。
 
* [[JavaScriptResult]] - [[JavaScript]](文字列)を直接HTTPレスポンスに書き込む。
 
* [[JavaScriptResult]] - [[JavaScript]](文字列)を直接HTTPレスポンスに書き込む。
22行目: 22行目:
 
* [[PartialViewResult]] - 指定されたビューをレンダリングして、HTMLフラグメント(htmlタグやbodyタグのないAJAX差込用のHTMLパーツ)として送信する。
 
* [[PartialViewResult]] - 指定されたビューをレンダリングして、HTMLフラグメント(htmlタグやbodyタグのないAJAX差込用のHTMLパーツ)として送信する。
 
* [[ViewResult]] - 指定されたビューをレンダリングして送信する。通常はこれ。
 
* [[ViewResult]] - 指定されたビューをレンダリングして送信する。通常はこれ。
 +
 +
[[ASP.NET MVC 3]]で追加されたActionResult。
 +
* [[HttpNotFoundResult]]
 +
* [[HttpStatusCodeResult]]
 +
* [[RedirectResult]]  - [[コンストラクタ]]の引数が追加され、[[301リダイレクト]]か[[302リダイレクト]]かを選べるようになった。
  
 
=== 独自ActionResult ===
 
=== 独自ActionResult ===
42行目: 47行目:
 
         public ImageFormat ImageFormat { get; set; }
 
         public ImageFormat ImageFormat { get; set; }
  
         private readonly Dictionary<ImageFormat, string> ContentTypes = new Dictionary<ImageFormat, string>()
+
         private readonly Dictionary<ImageFormat, string> ContentTypes =  
        {
+
            new Dictionary<ImageFormat, string>()
            {ImageFormat.Bmp , "image/bmp"},
+
            {
            {ImageFormat.Gif , "image/gif"},
+
                {ImageFormat.Bmp , "image/bmp"},
            {ImageFormat.Icon, "image/vnd.microsoft.icon"},
+
                {ImageFormat.Gif , "image/gif"},
            {ImageFormat.Jpeg, "image/jpeg"},
+
                {ImageFormat.Icon, "image/vnd.microsoft.icon"},
            {ImageFormat.Png , "image/png"},
+
                {ImageFormat.Jpeg, "image/jpeg"},
            {ImageFormat.Tiff, "image/tiff"},
+
                {ImageFormat.Png , "image/png"},
            {ImageFormat.Wmf , "image/wmf"},
+
                {ImageFormat.Tiff, "image/tiff"},
        };
+
                {ImageFormat.Wmf , "image/wmf"},
 +
            };
  
 
         public override void ExecuteResult(ControllerContext context)
 
         public override void ExecuteResult(ControllerContext context)
75行目: 81行目:
  
 
== 関連項目 ==
 
== 関連項目 ==
 +
* [[ASP.NET MVC/アクション]]
 
* [[ASP.NET MVC]]
 
* [[ASP.NET MVC]]
  

2012年4月3日 (火) 01:54時点における最新版

ActionResult クラスとは、ASP.NET MVCアクション・メソッドの戻り値として使う各種クラス(ActionResultを継承したクラス)のベースクラスである。

アクションメソッド内でActionResult(を継承したクラス)の中身を穴埋めしてやると、ASP.NET MVCのフレームワーク側でその内容に従いHTMLJSONなどのHTTPレスポンスが生成される。

たとえばViewResultであればビューエンジン(HTMLテンプレートエンジン)を呼び出してHTMLをレンダリングしたり、FileResultであればファイルを指定するだけでフレームワーク側で適切なHTTPレスポンスヘッダを生成し簡単にダウンロードダイアログを表示したりできる。

ContentResultだけでを用いてゴリゴリ書けば20世紀のPerlで書かれたウェブアプリケーションばりのソースコードになること受けあい。困ったらContentResult。まじおすすめ。

ActionResultの種類[編集 | ソースを編集]

ASP.NET MVCではActionResultクラスを継承した様々な便利クラスがあらかじめ用意されているので使うとよい。

  • ContentResult - 文字列を直接HTTPレスポンスに書き込む。
  • EmptyResult - 空のHTTPレスポンスを返す。
  • FileResult - ファイルをダウンロードさせるためのベースクラス。
  • HttpUnauthorizedResult - 認証要求を送信する。
  • JavaScriptResult - JavaScript(文字列)を直接HTTPレスポンスに書き込む。
  • JsonResult - Objectクラスの中身を突っ込むとJSONに変換して送信する。
  • RedirectResult - リダイレクトする。
  • RedirectToRouteResult - 指定されたルート名およびルート値の結果を、HTMLフラグメント(htmlタグやbodyタグのないAJAX差込用のHTMLパーツ)として送信する。
  • PartialViewResult - 指定されたビューをレンダリングして、HTMLフラグメント(htmlタグやbodyタグのないAJAX差込用のHTMLパーツ)として送信する。
  • ViewResult - 指定されたビューをレンダリングして送信する。通常はこれ。

ASP.NET MVC 3で追加されたActionResult。

独自ActionResult[編集 | ソースを編集]

ActionResultクラスを継承することでオレオレActionResultを作ることができる。

例:ImageResult[編集 | ソースを編集]

Image(System.Drawing)のインスタンスを突っ込んで、ImageFormart(System.Drawing.Imaging)で指定された形式で出力するActionResultの例。

    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using System.Drawing;
    using System.Drawing.Imaging;

    public class ImageResult : ActionResult
    {
        public ImageResult() { }

        public Image Image { get; set; }
        public ImageFormat ImageFormat { get; set; }

        private readonly Dictionary<ImageFormat, string> ContentTypes = 
            new Dictionary<ImageFormat, string>()
            {
                {ImageFormat.Bmp , "image/bmp"},
                {ImageFormat.Gif , "image/gif"},
                {ImageFormat.Icon, "image/vnd.microsoft.icon"},
                {ImageFormat.Jpeg, "image/jpeg"},
                {ImageFormat.Png , "image/png"},
                {ImageFormat.Tiff, "image/tiff"},
                {ImageFormat.Wmf , "image/wmf"},
            };

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (this.Image == null)
            {
                throw new ArgumentNullException("Image");
            }
            if (this.ImageFormat == null)
            {
                throw new ArgumentNullException("ImageFormat");
            }

            // output 
            var res = context.HttpContext.Response;
            res.Clear();
            res.ContentType = this.ContentTypes[this.ImageFormat];
            this.Image.Save(res.OutputStream, this.ImageFormat);
        }
    }

関連項目[編集 | ソースを編集]

外部リンク[編集 | ソースを編集]