// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // // DB Context // services.AddDbContext <DbCtx>(options => options.UseSqlite("Data Source=blog.db", b => b.MigrationsAssembly("rest-api"))); // // DAL // services.AddScoped <IEntrepriseDAL, EntrepriseDAL>(); services.AddScoped <IContactDAL, ContactDAL>(); // // BL // services.AddScoped <IEntrepriseBL, EntrepriseBL>(); services.AddScoped <IContactBL, ContactBL>(); // //MVC // services.AddMvc( config => { config.Filters.Add(typeof(GlobalExceptionFilter)); config.Filters.Add(typeof(FormatResultFilter)); } ); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.Configure <ApiBehaviorOptions>(options => { options.InvalidModelStateResponseFactory = (context) => { var message = String.Join(", ", context.ModelState.Values.SelectMany(x => x.Errors.Select(p => p.ErrorMessage)).ToList()); var result = new MsgException { status = System.Net.HttpStatusCode.BadRequest, message = message, code = 00009, type = "Validation errors", }; return(new ObjectResult(new MsgResult { exception = result })); }; }); }
public void OnException(ExceptionContext context) { HttpStatusCode status = HttpStatusCode.InternalServerError; String message = String.Empty; int code = 0; if (context.HttpContext.Request.Path.Value.StartsWith("/api")) { var exceptionType = context.Exception.GetType(); String type = exceptionType.ToString(); if (exceptionType == typeof(UnauthorizedAccessException)) { message = "Unauthorized Access"; status = HttpStatusCode.Unauthorized; } else if (exceptionType == typeof(NotImplementedException)) { message = "A server error occurred."; status = HttpStatusCode.NotImplemented; } else { if (context.Exception.Message != null && context.Exception.Message.Length > 300) { message = context.Exception.Message.Substring(0, 299); } else { message = context.Exception.Message; } status = HttpStatusCode.BadRequest; } var result = new MsgException { status = status, message = message, code = code, type = type }; context.Result = new ObjectResult(new MsgResult { exception = result }); } }
/// <summary> /// 获取OUInfo的详细信息 /// </summary> /// <param name="actionContext"></param> /// <param name="httpContext"></param> /// <returns>null</returns> public Forward GetDetail(ActionContext actionContext, HttpContext httpContext) { AjaxForwardUtils.InitResponse(httpContext.Response); JavaScriptObject json = new JavaScriptObject(); try { string unid = RequestUtils.GetStringParameter(httpContext, "unid", null); logger.Debug("unid:" + unid); if (string.IsNullOrEmpty(unid)) { MsgException e = new MsgException("OUInfo的Unid不能为空,需要在请求参数中包含有效的OUInfo的Unid值!"); logger.Error(e.Message, e); return(AjaxForwardUtils.ExceptionForward(httpContext, json, e)); } OUInfo ouInfo = this.ouInfoService.Load(unid); if (ouInfo == null) { MsgException e = new MsgException("指定Unid的OUInfo在系统中不存在:unid=" + unid); logger.Error(e.Message, e); return(AjaxForwardUtils.ExceptionForward(httpContext, json, e)); } string jsonStr = JavaScriptConvert.SerializeObject(ouInfo); if (logger.IsDebugEnabled) { logger.Debug("json=" + jsonStr); } httpContext.Response.Write(jsonStr); return(null); } catch (Exception e) { logger.Error(e.Message, e); return(AjaxForwardUtils.ExceptionForward(httpContext, json, e)); } }