JsonResult

 

一、JsonResult 可以操作下列屬性

1、ContentEncoding

2、ContentType

3、Data

4、JsonRequestBehavior

5、MaxJsonLength

6、RecursionLimit

 

二、System.Web.Mvc.Controller 內含有六種 Json 多載方法

1、protected internal JsonResult Json(object data);

2、protected internal JsonResult Json(object data, string contentType);

3、protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding);

4、protected internal JsonResult Json(object data, JsonRequestBehavior behavior);

5、protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior);

6、protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior);

 

三、範例

來看利用 Json 多載方法所做成的 JsonResult 範例

HomeController.cs

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

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public JsonResult Index()
        {
            Dictionary<string, string> oDictionary = new Dictionary<string, string>();
            oDictionary.Add("Name", "Mary");
            return Json(oDictionary, "application/json", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
        }
    }
}

來看執行結果,發現確實有做成 JSON 格式回傳

 

參考資料:

JsonResult Class

Controller Class