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

  • 相关阅读:
    SQL反模式学习笔记16 使用随机数排序
    SQL反模式学习笔记21 SQL注入
    SQL反模式学习笔记22 伪键洁癖,整理数据
    SQL反模式学习笔记18 减少SQL查询数据,避免使用一条SQL语句解决复杂问题
    SQL反模式学习笔记19 使用*号,隐式的列
    SQL反模式学习笔记17 全文搜索
    SQL反模式学习笔记20 明文密码
    (可发送)亿级流量APP,是怎么做前端性能测试自动化的?
    测试窝 高薪测试必备技能和 20+ 项目实战精华,好书免费领(限前 1000 名)!
    同样是断言,为何 Hamcrest 如此优秀?测试灵魂三问,该如何回答?
  • 原文地址:https://www.cnblogs.com/kfsmqoo/p/4551530.html
Copyright © 2011-2022 走看看