小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

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("請求 URI: {0}", context.Request.RequestUri);

            var message = Ex.Message;
            if (Ex.InnerException != null)
                message = Ex.InnerException.Message;

            //注意:ResponseException不會來
            if (Ex is NotImplementedException)
            {
                context.Response = context.Request.CreateResponse(HttpStatusCode.NotImplemented, new HttpError("此方法暫未實現(xiàn)"));
            }
            else if (Ex is InvalidOperationException)
            {
                context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, new HttpError("未將對象引用設置到對象的實例,可能存在無效參數(shù)"));
            }
            else if (Ex is NotFondException)
            {
                context.Response = context.Request.CreateResponse(HttpStatusCode.NotFound, new HttpError("所需實例不存在"));
            }
            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("可能存在字段的字符超長!"));
                }
                else
                {

                    context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, new HttpError(Ex.Message));

                }
            }
            base.OnException(context);
        }
    }
View Code

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

    // Model驗證
    public class ModelValidationFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext context)
        {
            var modelState = context.ModelState;
            if (modelState.IsValid == false)
            {

                //返回多個

                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ù)格式錯誤,請重新錄入!";
                            }

                            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

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多