加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JsonResult类型

发布时间:2020-12-16 19:00:21 所属栏目:百科 来源:网络整理
导读:一、介绍 后台数据是通过二个基于某种CLR 类型的对象来承载的,当客户端调用某个Action 方法并希望以JSON 的格式返回请求的数据时, ASP.NET MVC 中 System.Web.Mvc.J sonResult将CLR 对象转换成JSON 格式予以响应; 返回JsonResult类型,通过调用Json()方法

一、介绍

后台数据是通过二个基于某种CLR 类型的对象来承载的,当客户端调用某个Action 方法并希望以JSON 的格式返回请求的数据时, ASP.NET MVC 中

System.Web.Mvc.J sonResult将CLR 对象转换成JSON 格式予以响应;

返回JsonResult类型,通过调用Json()方法返回Json格式数据到试图

二、源代码分析

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
{
/// <summary>
/// 支持HTTP-GET请求
/// </summary>
AllowGet,
/// <summary>
/// 不支持HTTP-GET请求
/// 默认类型
/// </summary>
DenyGet
}

Data属性:一个object 类型的属性,表示需要被转换成JSON 格式的数据对象;

ContentEncoding:表示为当前响应设置的编码方式;

ContentType:表示为当前响应设置的媒体类型(默认采用的媒体类型为"applicaion/json");

JsonRequestBehavior属性:开启对HTTP-GET 请求的支持,该属性是JsonRequestBehavior 枚举类型,

两个枚举项AllowGetDenyGet(默认值) 分别表示允许/拒绝支持对HTTP-GET 请求的响应。

(JsonResult 在默认的情况下不能作为对HTTP-GET 请求的响应,在这种情况下并会直接抛出一个InvalidOperationException异常,

通过它的 JsonRequestBehavior 属性开启对HTTP-GET 请求的支持);

MaxJsonLength:被反序列化和序列化生成的JSON 字符串的长度,默认值为2097152 (Ox200000 ,等同于4MB 的Unicode 字符串数据);

RecursionLimit:用于设置被序列化对象和反序列化生成对象结构的允许的层级数,默认值为100;


ExecuteResult()方法具体实现:

public override void ExecuteResult(ControllerContext context)
{
//确认是否用于响应HTTP-GET 请求
if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Compare(context.HttpContext.Request.HttpMethod,"GET",true) == 0)
{
throw new InvalidOperationException();
}

HttpResponseBase response = context.HttpContext.Response;
//设直媒体类型和编码方式
response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
if (this.ContentEncoding != null) { response.ContentEncoding = this.ContentEncoding; }

//创建JavaScriptSerializer 将数据对象序列化成JSON 字符串并写入当前HttpResponse
if (null == this.Data) return;
JavaScriptSerializer serializer = new JavaScriptSerializer()
{
MaxJsonLength = this.MaxJsonLength.HasValue ? this.MaxJsonLength.Value : 0x200000,
RecursionLimit = this.RecursionLimit.HasValue ? this.RecursionLimit.Value : 100
};
response.Write(serializer.Serialize(this.Data));
}


三. CLR 对象到JSON 格式字符串的序列化过程使用的对象:

CLR 对象到JSON 格式字符串的序列化过程通过序列化器System.Web.Script. Serialization.JavaScriptSerializer 来完成。 JavaScriptSerializer 的SerializeDeserialize方法实现了CLR 对象的序列化和对JSON 字符串的反序列化。

public class JavaScriptSerializer
{
// 序列化方法
public string Serialize(object obj);

// 反序列化方法
public object Deserialize(string input,Type targetType);

//被反序列化和序列化生成的JSON 字符串的长度,默认值为2097152 (Ox200000 ,等同于4MB 的Unicode 字符串数据)
public int MaxJsonLength { get; set; }

//用于设置被序列化对象和反序列化生成对象结构的允许的层级数,默认值为100

public int RecursionLimit { get; set; }
}


四.抽象类Con仕oller定义了如下一系列的Json 方法用于根据指定的数据对象、编码方式以及JsonRequestl3ehavior 来创建相应的JsonResult:

public abstract class Controller : ControllerBase,IActionFilter,IAuthorizationFilter,IDisposable,..........
{

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

}

五.例子

[HttpPost]

public class AccountController : Controller

{

public JsonResult LoginGet( )
{

var result = new {
flag = "ok",
msg = "提供密码错误!"
};

return Json(result);

}

}

试图:

<script type="text/javascript">

$(function () {
$('#btLogin').click(function () {
//对象集合
// var models = [];
// models.push({ UserName: userName,Password: password });

//Json对象作为参数(名称必须与模型属性名相同)
var models = {
"UserName": userName,
"Password": password
};

//模型绑定
//var models = new Object();
//models.UserName = userName;
//models.Password = password;

$.ajax({
url: '/Account/Login',
data: JSON.stringify(models),
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if( data.flag =='ok') {

alert(data.msg);

} },error: function (XMLHttpRequest,textStatus,errorThrown) { $.show_warning('提示','错误'); } }); }); }); </script>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读