码迷,mamicode.com
首页 > Web开发 > 详细

MVC JsonResult的用法

时间:2015-08-16 18:05:52      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

本文导读:当客户端调用某个Action方法并希望以JSON的格式返回请求的数据时,ASP.NET MVC需要有一种机制将CLR对象转换成JSON格式予以响应,而这可以通过JsonResult来解决。下面介绍MVC中JsonResult的用法

一、MVC中JsonResult定义的代码片段

     public class JsonResult : ActionResult
     {    
       public override void ExecuteResult(ControllerContext context);
     
        public object                 Data { get; set; }  
        public Encoding               ContentEncoding { get; set; }
        public string                 ContentType { get; set; }    
        public JsonRequestBehavior    JsonRequestBehavior { get; set; }    
        public int?                   MaxJsonLength { get; set; }
        public int?                   RecursionLimit { get; set; }
    }
技术分享     
技术分享     public enum JsonRequestBehavior
     {
        AllowGet,
        DenyGet
     }

其中:JsonResult具有一个object类型的属性Data表示需要被转换成JSON格式的数据对象。属性ContentEncoding和ContentType表示为当前响应设置的编码方式和媒体类型,默认采用的媒体类型为“application/json”。

 

备注:

出于对安全的考虑,JsonResult在默认的情况下不能作为对HTTP-GET请求的响应,在这种情况下并会直接抛出一个InvalidOperationException异常。我们可以通过它的JsonRequestBehavior属性开启JsonResult对HTTP-GET请求的支持。该属性类型为JsonRequestBehavior枚举,两个枚举项AllowGet和DenyGet分别表示允许/拒绝支持对HTTP-GET请求的响应。JsonResult的JsonRequestBehavior属性在初始化的时候被设置为DenyGet,如果我们需要用创建的JsonResult来响应HTTP-GET请求,需要显式地将它的JsonRequestBehavior属性设置为AllowGet。

 

二、Controller中返回JsonResult的方法

 

在抽象类Controller同样定义如下一系列的Json方法用于根据指定的数据对象、编码方式以及JsonRequestBehavior来创相应的JsonResult。 

 
public abstract class Controller : ControllerBase,...

     {
         //其他成员
         protected internal JsonResult Json(object data);
         protected internal JsonResult Json(object data, string contentType);
         protected internal JsonResult Json(object data, JsonRequestBehavior behavior);
         protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding);
         protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior);
         protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior);
     }

三、MVC JsonResult的实例

 

1、视图页面

<!DOCTYPE html>

技术分享

<html>

技术分享

<head runat="server">

技术分享

    <title>Index2</title>

技术分享

    <script src="/Scripts/jquery-1.4.4。js" type="text/javascript"></script>

技术分享

<script type="text/javascript">
技术分享        var login = function () {
技术分享            var data = { "username": $.trim($("#username").val()), "pwd": $.trim($("#pwd").val()) }
技术分享
技术分享//            $.post("/Home/Login", data, function (message) {
技术分享//                if (message.success) {
技术分享//                    alert(message.msg);
技术分享//                }
技术分享//                else {
技术分享//                    alert(message.msg);
技术分享//                }
技术分享//            }, "json");
技术分享
技术分享            $.ajax({ type: "post", url: "/Home/Login", data: data, success: function (message) {
技术分享                if (message.Success) {
技术分享                    alert(message.Msg);
技术分享                }
技术分享                else {
技术分享                    alert(message.Msg);
技术分享                }
技术分享            }, dataType: "json"
技术分享            });
技术分享        }
技术分享    </script>
技术分享</head>
技术分享<body>
技术分享    <div id="nav">
技术分享        <a href="/Home/Index">ajax+Handler</a>&nbsp; <a>ajax+action</a>
技术分享    </div>
技术分享    <div>
技术分享        <h3>
技术分享            Login</h3>
技术分享        Username:<input id="username" name="username" type="text" /><br />
技术分享        Userpass:<input id="pwd" name="pwd" type="password" /><br />
技术分享        <button type="button" onclick="login()">
技术分享            Submit</button>
技术分享    </div>
技术分享</body>
技术分享</html>

2、控制器

using System.Web.Mvc;
技术分享技术分享
namespace Mvc1.Controllers
技术分享
{
技术分享    public class HomeController : Controller
技术分享    {
技术分享        //
技术分享        // GET: /Home/
技术分享
技术分享        public ActionResult Index()
技术分享        {
技术分享            return View();
技术分享        }
技术分享        //
技术分享        // GET: /Home/Index2
技术分享        public ActionResult Index2()
技术分享        {
技术分享            return View();
技术分享        }
技术分享
技术分享        // Post: /Home/Login
技术分享        [HttpPost]
技术分享        public JsonResult Login()
技术分享        {
技术分享            string username=Request["username"];
技术分享            string pwd = Request["pwd"];
技术分享
技术分享            message msg = null;
技术分享
技术分享            if (username == "rain" && pwd == "m123")
技术分享            {
技术分享                msg = new message(true, "Success");
技术分享            }
技术分享            else
技术分享            {
技术分享                msg = new message(false, "Fail");
技术分享            }
技术分享
技术分享            return Json(msg);
技术分享        }
技术分享    }
技术分享
技术分享    class message
技术分享    {
技术分享        bool success;
技术分享        string msg;
技术分享
技术分享        public message(bool success, string msg)
技术分享        {
技术分享            this.success = success;
技术分享            this.msg = msg;
技术分享        }
技术分享
技术分享        public bool Success
技术分享        {
技术分享            get { return success; }
技术分享            set { success = value; }
技术分享        }
技术分享        public string Msg
技术分享        {
技术分享            get { return msg; }
技术分享            set { msg = value; }
技术分享        }
技术分享    }
技术分享}

转自:http://www.studyofnet.com/news/594.html
 

MVC JsonResult的用法

标签:

原文地址:http://www.cnblogs.com/zkwarrior/p/4734684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!