zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    快速编辑里指定默认值
    Odoo domain 中的 like, ilike, =like, =ilike 举例说明【转】
    odoo报表条码无法显示解决[转]
    ubuntu 安装 wkhtmltopdf 的方法
    解决Odoo日期(时间)无效的问题 [转]
    ShareSDK演示
    黑客帝国数字矩阵特效做法
    lua中实现倒计时
    Lua中用Split函数分割字符串
    lua封装的位运算
  • 原文地址:https://www.cnblogs.com/kfsmqoo/p/4551530.html
Copyright © 2011-2022 走看看