using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Newtonsoft.Json;

namespace Cmdjy.Controllers
{
    public class ControllerBase:Controller
    {
        protected override JsonResult Json(object data,string contentType,Encoding contentEncoding) {
            return Json(data,contentType,contentEncoding,JsonRequestBehavior.DenyGet);
        }

        protected override JsonResult Json(object data,string contentType,Encoding contentEncoding,JsonRequestBehavior behavior) {
            return new MyJsonResult {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior
            };
        }
    }

    public class MyJsonResult:JsonResult
    {
        public override void ExecuteResult(ControllerContext context) {
            if(context == null) {
                throw new ArgumentNullException("context");
            }
            if(JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
                String.Equals(context.HttpContext.Request.HttpMethod,"GET",StringComparison.OrdinalIgnoreCase)) {
                throw new InvalidOperationException("GetNotAllowed");
            }

            HttpResponseBase response = context.HttpContext.Response;

            if(!String.IsNullOrEmpty(ContentType)) {
                response.ContentType = ContentType;
            }
            else {
                response.ContentType = "application/json";
            }
            if(ContentEncoding != null) {
                response.ContentEncoding = ContentEncoding;
            }
            if(Data != null) {
                response.Write(JsonConvert.SerializeObject(Data));
            }
        }
    }
}