csharp C#:JsonResult替代ASP.NET MVC。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#:JsonResult替代ASP.NET MVC。相关的知识,希望对你有一定的参考价值。

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace System.Web.Mvc
{
    internal class JsonNetResult : JsonResult
    {
        static IList<JsonConverter> DefaultConverters = new List<JsonConverter>()
        {
            new StringEnumConverter(), //new StringEnumConverter() { CamelCaseText = true },
            new IsoDateTimeConverter(),
        };

        public JsonSerializerSettings SerializerSettings { get; set; }
        public Formatting Formatting { get; set; }

        public JsonNetResult()
        {
            this.SerializerSettings = new JsonSerializerSettings()
            {
                Converters = DefaultConverters,
                //ContractResolver = new CamelCasePropertyNamesContractResolver(),
            };
        }

        public JsonNetResult(JsonResult original)
            : this()
        {
            this.ContentEncoding = original.ContentEncoding;
            this.ContentType = original.ContentType;
            this.JsonRequestBehavior = original.JsonRequestBehavior;
            this.Data = original.Data;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            HttpResponseBase response = context.HttpContext.Response;

            response.ContentType = !string.IsNullOrEmpty(ContentType)
                ? ContentType
                : "application/json";

            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;

            if (Data != null)
            {
                JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting };

                JsonSerializer serializer = JsonSerializer.Create(this.SerializerSettings);
                serializer.Serialize(writer, Data);

                writer.Flush();
            }
        }
    }

    internal static class ControllerExtensions
    {
        public static JsonResult JsonNet(this Controller controller, JsonResult result)
        {
            return new JsonNetResult(result);
        }
    }
}
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace System.Web.Mvc
{
    internal class JsonNetAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            var result = filterContext.Result as JsonResult;
            if (result != null) filterContext.Result = new JsonNetResult(result);
        }
    }
}

以上是关于csharp C#:JsonResult替代ASP.NET MVC。的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 Asp.Net Core 中间件的 JsonResult 响应?

ASP.NET MVC JsonResult 日期格式

加载 ASP.Net MVC JSONResult jQuery DataTables

ASP.NET MVC JsonResult 返回 500

ASP.NET MVC:用JsonResult控制属性名的序列化。

ASP.Net MVC:如何基于原始Json数据创建JsonResult