日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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不會(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("未將對象引用設(shè)置到對象的實(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("可能存在字段的字符超長!"));
                }
                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ò)誤,請重新錄入!";
                            }

                            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)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多