自定义B/S开发框架中JsonResult类

自定义B/S开发框架中JsonResult类,在这里我们将JsonResult替换成我们的JsonNetResult 。

B/S开发框架JsonResult

B/S开发框架中我们经常会用到JsonResult,如果我们想自定义JSonResult成如下所示的JsonNetResult类,该怎么弄?

    public class JsonNetResult : JsonResult
    {
        public JsonNetResult()
        {
            Settings = new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,//忽略循环引用,B/S开发框架如果设置为Error,则遇到循环引用的时候报错(建议设置为Error,这样更规范)  
                DateFormatString = "yyyy-MM-dd HH:mm:ss",//日期格式化,默认的格式也不好看  
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()//json中属性开头字母小写的驼峰命名  
            };
        }
        public JsonSerializerSettings Settings { get; private set; }

        public override void ExecuteResult(ControllerContext context)//B/S开发框架重写JsonResult类的ExecuteResult方法  
        {
            if (context == null)
                throw new ArgumentNullException("context");
            //判断是否运行Get请求  
            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet
                && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
                throw new InvalidOperationException("JSON GET is not allowed");
            //设定响应数据格式。默认为json  
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
            //设定内容编码格式  
            if (this.ContentEncoding != null)
                response.ContentEncoding = this.ContentEncoding;
            if (this.Data == null)
                return;
            var scriptSerializer = JsonSerializer.Create(this.Settings);
            scriptSerializer.Serialize(response.Output, this.Data);
        }
    }
在过滤器中我们进行转换:
 public class JsonNetResultAttritube: IActionFilter
    {
        /// <summary>  
        /// 在这里我们将JsonResult替换成我们的JsonNetResult  
        /// </summary>  
        /// <param name="filterContext"></param>  
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                ActionResult result = filterContext.Result;
                if (result is JsonResult && !(result is JsonNetResult))
                {
                    JsonResult jsonResult = (JsonResult)result;
                    JsonNetResult jsonNetResult = new JsonNetResult();
                    jsonNetResult.ContentEncoding = jsonResult.ContentEncoding;
                    jsonNetResult.ContentType = jsonResult.ContentType;
                    jsonNetResult.JsonRequestBehavior = jsonResult.JsonRequestBehavior;
                    jsonNetResult.Data = jsonResult.Data;
                    jsonNetResult.MaxJsonLength = jsonResult.MaxJsonLength;
                    jsonNetResult.RecursionLimit = jsonResult.RecursionLimit;

                    filterContext.Result = jsonNetResult;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {

        }
    }

自定义B/S开发框架中JsonResult类,在这里我们将JsonResult替换成我们的JsonNetResult

网站&系统开发技术学习交流群:463167176

本站文章除注明转载外,均为本站原创或翻译,欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,共创和谐网络环境。
转载请注明:文章转载自:软件开发框架 » 自定义B/S开发框架中JsonResult类
本文标题:自定义B/S开发框架中JsonResult类
本文地址:http://www.hocode.com/OrgTec/UI/0019.html

相关文章: .Net MVC-视图层使用Ajax怎么实现视图局部数据异步刷新

电话
电话 18718672256

扫一扫
二维码