MVC JsonResult的用法

时间:2023-03-09 13:43:12
MVC JsonResult的用法

本文导读:当客户端调用某个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; }
}
MVC JsonResult的用法
MVC JsonResult的用法 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>

MVC JsonResult的用法

<html>

MVC JsonResult的用法

<head runat="server">

MVC JsonResult的用法

    <title>Index2</title>

MVC JsonResult的用法

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

MVC JsonResult的用法

<script type="text/javascript">
MVC JsonResult的用法 var login = function () {
MVC JsonResult的用法 var data = { "username": $.trim($("#username").val()), "pwd": $.trim($("#pwd").val()) }
MVC JsonResult的用法
MVC JsonResult的用法// $.post("/Home/Login", data, function (message) {
MVC JsonResult的用法// if (message.success) {
MVC JsonResult的用法// alert(message.msg);
MVC JsonResult的用法// }
MVC JsonResult的用法// else {
MVC JsonResult的用法// alert(message.msg);
MVC JsonResult的用法// }
MVC JsonResult的用法// }, "json");
MVC JsonResult的用法
MVC JsonResult的用法 $.ajax({ type: "post", url: "/Home/Login", data: data, success: function (message) {
MVC JsonResult的用法 if (message.Success) {
MVC JsonResult的用法 alert(message.Msg);
MVC JsonResult的用法 }
MVC JsonResult的用法 else {
MVC JsonResult的用法 alert(message.Msg);
MVC JsonResult的用法 }
MVC JsonResult的用法 }, dataType: "json"
MVC JsonResult的用法 });
MVC JsonResult的用法 }
MVC JsonResult的用法 </script>
MVC JsonResult的用法</head>
MVC JsonResult的用法<body>
MVC JsonResult的用法 <div id="nav">
MVC JsonResult的用法 <a href="/Home/Index">ajax+Handler</a>&nbsp; <a>ajax+action</a>
MVC JsonResult的用法 </div>
MVC JsonResult的用法 <div>
MVC JsonResult的用法 <h3>
MVC JsonResult的用法 Login</h3>
MVC JsonResult的用法 Username:<input id="username" name="username" type="text" /><br />
MVC JsonResult的用法 Userpass:<input id="pwd" name="pwd" type="password" /><br />
MVC JsonResult的用法 <button type="button" onclick="login()">
MVC JsonResult的用法 Submit</button>
MVC JsonResult的用法 </div>
MVC JsonResult的用法</body>
MVC JsonResult的用法</html> 2、控制器
using System.Web.Mvc;

MVC JsonResult的用法MVC JsonResult的用法

namespace Mvc1.Controllers

MVC JsonResult的用法

{
MVC JsonResult的用法 public class HomeController : Controller
MVC JsonResult的用法 {
MVC JsonResult的用法 //
MVC JsonResult的用法 // GET: /Home/
MVC JsonResult的用法
MVC JsonResult的用法 public ActionResult Index()
MVC JsonResult的用法 {
MVC JsonResult的用法 return View();
MVC JsonResult的用法 }
MVC JsonResult的用法 //
MVC JsonResult的用法 // GET: /Home/Index2
MVC JsonResult的用法 public ActionResult Index2()
MVC JsonResult的用法 {
MVC JsonResult的用法 return View();
MVC JsonResult的用法 }
MVC JsonResult的用法
MVC JsonResult的用法 // Post: /Home/Login
MVC JsonResult的用法 [HttpPost]
MVC JsonResult的用法 public JsonResult Login()
MVC JsonResult的用法 {
MVC JsonResult的用法 string username=Request["username"];
MVC JsonResult的用法 string pwd = Request["pwd"];
MVC JsonResult的用法
MVC JsonResult的用法 message msg = null;
MVC JsonResult的用法
MVC JsonResult的用法 if (username == "rain" && pwd == "m123")
MVC JsonResult的用法 {
MVC JsonResult的用法 msg = new message(true, "Success");
MVC JsonResult的用法 }
MVC JsonResult的用法 else
MVC JsonResult的用法 {
MVC JsonResult的用法 msg = new message(false, "Fail");
MVC JsonResult的用法 }
MVC JsonResult的用法
MVC JsonResult的用法 return Json(msg);
MVC JsonResult的用法 }
MVC JsonResult的用法 }
MVC JsonResult的用法
MVC JsonResult的用法 class message
MVC JsonResult的用法 {
MVC JsonResult的用法 bool success;
MVC JsonResult的用法 string msg;
MVC JsonResult的用法
MVC JsonResult的用法 public message(bool success, string msg)
MVC JsonResult的用法 {
MVC JsonResult的用法 this.success = success;
MVC JsonResult的用法 this.msg = msg;
MVC JsonResult的用法 }
MVC JsonResult的用法
MVC JsonResult的用法 public bool Success
MVC JsonResult的用法 {
MVC JsonResult的用法 get { return success; }
MVC JsonResult的用法 set { success = value; }
MVC JsonResult的用法 }
MVC JsonResult的用法 public string Msg
MVC JsonResult的用法 {
MVC JsonResult的用法 get { return msg; }
MVC JsonResult的用法 set { msg = value; }
MVC JsonResult的用法 }
MVC JsonResult的用法 }
MVC JsonResult的用法} 转自:http://www.studyofnet.com/news/594.html