乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      mvc、webapi雜記

       印度阿三17 2019-08-10
      原文鏈接:http://www.cnblogs.com/ToughGuy/p/5157113.html


      //1、JsonSerializerSettings
      var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
      json.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
      {
      DateFormatString = "yyyy-MM-dd HH:mm:ss",//用于WebAPI日期序列化
      };

      //2、禁用格式化器
          // Remove the JSON formatter

          // 刪除JSON格式化器
          config.Formatters.Remove(config.Formatters.JsonFormatter);
      // or(或者)
      // Remove the XML formatter // 刪除XML格式化器 config.Formatters.Remove(config.Formatters.XmlFormatter);


      //3、解決MvcJsonResult返回的Date格式化(/Date(1359522345000)/),以繼承折方式重寫即可。
      //http://www.cnblogs.com/JerryWang1991/archive/2013/03/08/2950457.html

      //4、WebApi全局異常處理

      config.Filters.Add(new WebApiExceptionFilter());

      public partial class WebApiExceptionFilter : ExceptionFilterAttribute
          {
              public override void OnException(HttpActionExecutedContext context)
              {
                  var Ex = context.Exception;
      
                  //增加二行 Trace 代碼
                  Trace.TraceError("異常: {0}", Ex.Message);
                  Trace.TraceError("請(qǐng)求 URI: {0}", context.Request.RequestUri);
      
                  var message = Ex.Message;
                  if (Ex.InnerException != null)
                      message = Ex.InnerException.Message;
      
                  //注意:ResponseException不會(huì)來
                  if (Ex is NotImplementedException)
                  {
                      context.Response = context.Request.CreateResponse(HttpStatusCode.NotImplemented, new HttpError("此方法暫未實(shí)現(xiàn)"));
                  }
                  else if (Ex is InvalidOperationException)
                  {
                      context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, new HttpError("未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例,可能存在無效參數(shù)"));
                  }
                  else if (Ex is NotFondException)
                  {
                      context.Response = context.Request.CreateResponse(HttpStatusCode.NotFound, new HttpError("所需實(shí)例不存在"));
                  }
                  else if (Ex is PageSizeException)
                  {
                      context.Response = context.Request.CreateResponse(HttpStatusCode.NotFound, new HttpError(Ex.Message));
                  }
                  else if (Ex is ExException)
                  {
                      var ExEx = Ex as ExException;
                      context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, new JsonRet(ExEx.Code, ExEx.Message,ExEx.Object));
                  }
                  else
                  {
      
                      if (Ex.Message.Contains("String or binary data would be truncated."))
                      {
                          context.Response = context.Request.CreateResponse(HttpStatusCode.NotFound, new HttpError("可能存在字段的字符超長(zhǎng)!"));
                      }
                      else
                      {
      
                          context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, new HttpError(Ex.Message));
      
                      }
                  }
                  base.OnException(context);
              }
          }
      View Code

      //5、數(shù)據(jù)注解-模型驗(yàn)證
      GlobalConfiguration.Configuration.Filters.Add(new ModelValidationFilterAttribute());

          // Model驗(yàn)證
          public class ModelValidationFilterAttribute : ActionFilterAttribute
          {
              public override void OnActionExecuting(HttpActionContext context)
              {
                  var modelState = context.ModelState;
                  if (modelState.IsValid == false)
                  {
      
                      //返回多個(gè)
      
                      var errors = new List<object>();
                      foreach (var state in modelState)
                      {
      
      
                          if (state.Value.Errors.Count > 0)
                          {
      
                              var ErrorMessage = state.Value.Errors[0].ErrorMessage;
                              if (string.IsNullOrEmpty(ErrorMessage))
                              {
                                  var Message = state.Value.Errors[0].Exception.Message;
      
                                  if (Message.Contains("Could not convert "))
                                  {
                                      Message = "參數(shù)格式錯(cuò)誤,請(qǐng)重新錄入!";
                                  }
      
                                  errors.Add(new { Success = false, Code = -1, Key = state.Key, Message = Message });
                              }
                              else
                              {
                                  errors.Add(new { Success = false, Code = -1, Key = state.Key, Message = ErrorMessage });
                              }
                          }
                          else
                          {
      
                          }
                      }
      
                      context.Response = context.Request.CreateResponse(HttpStatusCode.OK, errors.Count > 0 ? errors[0] : null);
                  }
              }
          }
      View Code

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多