「JsonResult クラス (System.Web.Mvc)」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator |
imported>Administrator |
||
(同じ利用者による、間の1版が非表示) | |||
32行目: | 32行目: | ||
} | } | ||
</source> | </source> | ||
+ | |||
+ | == 似たような実装例 == | ||
+ | JsonRequestBehavior.AllowGetが面倒なので[[DynamicJson]]で実装してみた。 | ||
+ | とくに意味はない。 | ||
+ | |||
+ | なんかよくわからないけど、[[契約による設計]]([[System.Diagnostics.Contracts 名前空間]])を使ってみたかった。 | ||
+ | とくに意味はない。 | ||
+ | |||
+ | <source lang="csharp"> | ||
+ | using System; | ||
+ | using System.Text; | ||
+ | using System.Web; | ||
+ | using System.Web.Mvc; | ||
+ | using System.Diagnostics.Contracts; | ||
+ | using Codeplex.Data;// DynamicJson | ||
+ | |||
+ | public class JsonResult2 : ActionResult | ||
+ | { | ||
+ | public static readonly string ContentTypeDefault = "application/json"; | ||
+ | |||
+ | public string ContentType { get; set; } | ||
+ | public Encoding ContentEncoding { get; set; } | ||
+ | public object Data { get; set; } | ||
+ | |||
+ | public JsonResult2() | ||
+ | { | ||
+ | } | ||
+ | |||
+ | public JsonResult2(object data) | ||
+ | { | ||
+ | this.ContentEncoding = null; | ||
+ | this.Data = data; | ||
+ | } | ||
+ | |||
+ | public override void ExecuteResult(ControllerContext context) | ||
+ | { | ||
+ | // 契約による設計:事前条件 | ||
+ | Contract.Requires(context != null); | ||
+ | |||
+ | // 実装 | ||
+ | HttpResponseBase response = context.HttpContext.Response; | ||
+ | if (string.IsNullOrEmpty(this.ContentType)) | ||
+ | { | ||
+ | response.ContentType = ContentTypeDefault; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | response.ContentType = this.ContentType; | ||
+ | } | ||
+ | |||
+ | if (this.ContentEncoding != null) | ||
+ | { | ||
+ | response.ContentEncoding = this.ContentEncoding; | ||
+ | } | ||
+ | |||
+ | // DynamicJSONでシリアライズ | ||
+ | string json = DynamicJson.Serialize(this.Data); | ||
+ | response.Output.Write(json); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == XmlResultクラス == | ||
+ | [[ASP.NET MVC]]にはJsonResultクラスが標準であるのに何故かXmlResultクラスがない。当然、誰もが疑問に思うところなようで[[ASP.NET MVC]]向けのライブラリを作っているオープンソースプロジェクトの[[MvcContrib]]がXmlResultクラスを提供している。 | ||
+ | * http://mvccontrib.codeplex.com/ | ||
== 関連項目 == | == 関連項目 == |
2012年5月18日 (金) 04:34時点における最新版
JsonResultクラスとは、ASP.NET MVCのアクション・メソッドの戻り値(ActionResultおよびその派生クラス)として、あらゆるオブジェクトをJSON形式に変換して送信するクラスである。
使い方[編集 | ソースを編集]
基本的にはJsonResultインスタンスのDataプロパティに、送信したいオブジェクトのインスタンスを入れてやればよい。 なお、デフォルトでGETリクエストを弾く謎仕様なのでJsonRequestBehaviorプロパティの値には注意すること。
using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
// サンプルデータ
var obj = new {
Name = "hage",
Age = 32
};
// JsonResult
// デフォルトでGETリクエストを弾く謎仕様なので注意。
var jr = new JsonResult {
Data = obj,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
return jr;
// ControllerにJsonResultを生成してくれるJsonメソッドが
// いるので、そいつを使った方がシンプルではある。
// return this.Json(obj, JsonRequestBehavior.AllowGet);
}
}
似たような実装例[編集 | ソースを編集]
JsonRequestBehavior.AllowGetが面倒なのでDynamicJsonで実装してみた。 とくに意味はない。
なんかよくわからないけど、契約による設計(System.Diagnostics.Contracts 名前空間)を使ってみたかった。 とくに意味はない。
using System;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics.Contracts;
using Codeplex.Data;// DynamicJson
public class JsonResult2 : ActionResult
{
public static readonly string ContentTypeDefault = "application/json";
public string ContentType { get; set; }
public Encoding ContentEncoding { get; set; }
public object Data { get; set; }
public JsonResult2()
{
}
public JsonResult2(object data)
{
this.ContentEncoding = null;
this.Data = data;
}
public override void ExecuteResult(ControllerContext context)
{
// 契約による設計:事前条件
Contract.Requires(context != null);
// 実装
HttpResponseBase response = context.HttpContext.Response;
if (string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = ContentTypeDefault;
}
else
{
response.ContentType = this.ContentType;
}
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
// DynamicJSONでシリアライズ
string json = DynamicJson.Serialize(this.Data);
response.Output.Write(json);
}
}
XmlResultクラス[編集 | ソースを編集]
ASP.NET MVCにはJsonResultクラスが標準であるのに何故かXmlResultクラスがない。当然、誰もが疑問に思うところなようでASP.NET MVC向けのライブラリを作っているオープンソースプロジェクトのMvcContribがXmlResultクラスを提供している。
関連項目[編集 | ソースを編集]
- ActionResult クラス (System.Web.Mvc) - ASP.NET MVCにおけるアクションの戻り値となるベースクラス。
- ASP.NET MVC
参考文献[編集 | ソースを編集]