差分

ナビゲーションに移動 検索に移動

ActionResult クラス (System.Web.Mvc)

1,755 バイト追加, 2012年3月15日 (木) 04:59
* [[PartialViewResult]] - 指定されたビューをレンダリングして、HTMLフラグメント(htmlタグやbodyタグのないAJAX差込用のHTMLパーツ)として送信する。
* [[ViewResult]] - 指定されたビューをレンダリングして送信する。通常はこれ。
 
=== 独自ActionResult ===
ActionResultクラスを継承することでオレオレActionResultを作ることができる。
 
==== 例:ImageResult ====
Image(System.Drawing)のインスタンスを突っ込んで、ImageFormart(System.Drawing.Imaging)で指定された形式で出力するActionResultの例。
<source lang="csharp">
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);
}
}
</source>
== 関連項目 ==
匿名利用者

案内メニュー