MVC Json输出调试信息


当测试环境、生产环境不能够调试的时候,有的时候想通过一些参数跟踪出想要的数据。如:SOA,DALSQL 等。

 
public class DebugJsonResult: JsonResult
    {
        public DebugJsonResult(object Data)
        {
            base.Data = Data;
        }

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

            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)
            {
                string jsonData = JsonNetHelper.ToJson(Data);
                if (ControllerDebug.GetInstance.IsDebug == true)
                {
                    try
                    {
                        if (jsonData.LastIndexOf("}") + 1 == jsonData.Length)
                        {
                            DebugInfoResult debuginfo = new DebugInfoResult();
                            debuginfo.DebugInfo = ControllerDebug.GetInstance.DebugList;

                            string strdebuginfo = jsonData.Insert(jsonData.LastIndexOf("}"), "," + JsonNetHelper.ToJson(ControllerDebug.GetInstance.DebugList));
                            response.Write(strdebuginfo);
                        }
                        else
                        {
                            response.Write(jsonData);
                        }
                    }
                    catch
                    {
                        
                        response.Write(jsonData);
                    }
                }
                else
                {
                     
                    response.Write(jsonData);
                }
            }
        }
    }

    public class DebugInfoResult 
    {
        /// <summary>
        /// Debug 信息
        /// </summary>
        public object DebugInfo
        {
            get;
            set;
        }
    }

 
在 调用Controller 是传入isdebug 参数.

public class IoCControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
if (HttpContext.Current.Request.QueryString["isdebug"]!=null)
            {
                string sisdebug = HttpContext.Current.Request.QueryString["isdebug"].ToString().ToUpper();

                if (sisdebug.IndexOf("T") >= 0)
                {
                    string tempisdebug = sisdebug.Replace("T","").Replace("-","");

                    if (!string.IsNullOrEmpty(tempisdebug))
                    { 
                         ControllerDebug.GetInstance.SearchType= (EnumDebug)Enum.Parse(typeof(EnumDebug), tempisdebug.ToUpper());
                    }

                    //开启调试.
                    ControllerDebug.GetInstance.IsDebug = true;
                    ControllerDebug.GetInstance.DebugList.Clear();
                }
                else
                {
                    ControllerDebug.GetInstance.IsDebug = false;
                    ControllerDebug.GetInstance.DebugList.Clear();
                }
            }
            else
            {
                ControllerDebug.GetInstance.IsDebug = false;
                ControllerDebug.GetInstance.DebugList.Clear();
            }
return base.GetControllerInstance(requestContext, controllerType);
     }
}
public abstract class BaseController : System.Web.Mvc.Controller
    {
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
        {
            return new DebugJsonResult(data);
        }
     }
public class BookingGroupOrderController : BaseController
    {
    [HttpPost]
        public ActionResult CreateOrder(O_BookingCreateOrder obj)
        {
            IOrderBiz GOBiz = Ioc.IoCFactory.Instance.CurrentContainer.Resolve<IOrderBiz>("BookingGroupOrder");

            ICreateOrder createobject = GOBiz.CreateOrder<O_BookingCreateOrder>(obj);

            return Json(createobject, null, null);
        }
}


Q[`8A4X9LR%8GA8B~@_LY)I

原文地址:https://www.cnblogs.com/kfsmqoo/p/4551530.html