diff --git a/src/Shentun.Peis.Application.Contracts/CustomerOrgs/UpdateCustomerOrgDto.cs b/src/Shentun.Peis.Application.Contracts/CustomerOrgs/UpdateCustomerOrgDto.cs index b8e6583..10bab42 100644 --- a/src/Shentun.Peis.Application.Contracts/CustomerOrgs/UpdateCustomerOrgDto.cs +++ b/src/Shentun.Peis.Application.Contracts/CustomerOrgs/UpdateCustomerOrgDto.cs @@ -71,7 +71,7 @@ namespace Shentun.Peis.CustomerOrgs /// /// 单位性质 /// - public Guid OrgTypeId { get; set; } + public Guid? OrgTypeId { get; set; } diff --git a/src/Shentun.Peis.HttpApi.Host/ErrorHandlerMiddleWare.cs b/src/Shentun.Peis.HttpApi.Host/ErrorHandlerMiddleWare.cs deleted file mode 100644 index 7e268bf..0000000 --- a/src/Shentun.Peis.HttpApi.Host/ErrorHandlerMiddleWare.cs +++ /dev/null @@ -1,102 +0,0 @@ -using Microsoft.AspNetCore.Http.Features; -using Microsoft.AspNetCore.Http; -using Microsoft.Net.Http.Headers; -using System.Net; -using System.Text.Encodings.Web; -using System.Text.Json; -using System.Text.Unicode; -using System; -using System.Threading.Tasks; -using Microsoft.IdentityModel.Logging; - -namespace Shentun.Peis -{ - public class ErrorHandlerMiddleWare - { - //请求委托 - private readonly RequestDelegate _next; - - // private readonly LogHelper _nlogger; - - public ErrorHandlerMiddleWare(RequestDelegate next - //, LogHelper nlogger - ) - { - this._next = next; - // this._nlogger = nlogger; - } - - public async Task Invoke(HttpContext context) - { - try - { - await this._next.Invoke(context); - } - catch (Exception ex) - { - await HandleExceptionAsync(context, ex); - } - } - - private async Task HandleExceptionAsync(HttpContext context, Exception exception) - { - if (exception == null) return; - await HandleErrorResultAsync(context, exception).ConfigureAwait(false); - } - - private async Task HandleErrorResultAsync(HttpContext context, Exception exception) - { - //记录日志 - // _nlogger.LogError(exception); - //返回友好的提示 - var response = context.Response; - //清除原有HTTP上下文信息,为了明确指定程序出现异常,防止异常未被处理而后续当做正常操作执行 - ClearHttpContext(context); - //状态码 - if (exception is UnauthorizedAccessException) - response.StatusCode = (int)HttpStatusCode.Unauthorized; - else if (exception is BadHttpRequestException) - response.StatusCode = (int)HttpStatusCode.BadRequest; - else if (response.StatusCode == 403 || response.StatusCode == 404) { } - else response.StatusCode = (int)HttpStatusCode.InternalServerError; - - response.ContentType = context.Request.Headers["Accept"]; - var result = new ApiResult - { - Code = 500, - Message = exception.GetBaseException().Message - }; - if (response.ContentType.ToLower() != "application/x-www-form-urlencoded") - { - response.ContentType = "application/json"; - } - await response.WriteAsync(JsonSerializer.Serialize(result - , new JsonSerializerOptions() - { - Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), - PropertyNameCaseInsensitive = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase - })).ConfigureAwait(false); - } - - private static void ClearHttpContext(HttpContext context) - { - context.Response.Clear(); - //因为可能创建了新的终结点,所以执行管道会有所调整,所以需要重新计算 - context.SetEndpoint(endpoint: null); - var routeValuesFeature = context.Features.Get(); - routeValuesFeature?.RouteValues?.Clear(); - } - - private static Task ClearCacheHeaders(object state) - { - //清除输出缓存相关 - var headers = ((HttpResponse)state).Headers; - headers[HeaderNames.CacheControl] = "no-cache"; - headers[HeaderNames.Pragma] = "no-cache"; - headers[HeaderNames.Expires] = "-1"; - headers.Remove(HeaderNames.ETag); - return Task.CompletedTask; - } - } -} diff --git a/src/Shentun.Peis.HttpApi.Host/ApplicationDescription.cs b/src/Shentun.Peis.HttpApi.Host/Filter/ApplicationDescription.cs similarity index 96% rename from src/Shentun.Peis.HttpApi.Host/ApplicationDescription.cs rename to src/Shentun.Peis.HttpApi.Host/Filter/ApplicationDescription.cs index 348f779..4ae4cf7 100644 --- a/src/Shentun.Peis.HttpApi.Host/ApplicationDescription.cs +++ b/src/Shentun.Peis.HttpApi.Host/Filter/ApplicationDescription.cs @@ -13,6 +13,9 @@ using Volo.Abp.TenantManagement; namespace Shentun.Peis { + /// + /// 移除基础模板的控制器 + /// public class ApplicationDescription : IApplicationModelConvention { public ApplicationDescription() diff --git a/src/Shentun.Peis.HttpApi.Host/AuthorizationMiddlewareResultHandler.cs b/src/Shentun.Peis.HttpApi.Host/Filter/AuthorizationMiddlewareResultHandler.cs similarity index 100% rename from src/Shentun.Peis.HttpApi.Host/AuthorizationMiddlewareResultHandler.cs rename to src/Shentun.Peis.HttpApi.Host/Filter/AuthorizationMiddlewareResultHandler.cs diff --git a/src/Shentun.Peis.HttpApi.Host/WrapResultAttribute.cs b/src/Shentun.Peis.HttpApi.Host/Filter/CustomerActionFilterAttribute.cs similarity index 98% rename from src/Shentun.Peis.HttpApi.Host/WrapResultAttribute.cs rename to src/Shentun.Peis.HttpApi.Host/Filter/CustomerActionFilterAttribute.cs index 02ec5be..98d0325 100644 --- a/src/Shentun.Peis.HttpApi.Host/WrapResultAttribute.cs +++ b/src/Shentun.Peis.HttpApi.Host/Filter/CustomerActionFilterAttribute.cs @@ -12,8 +12,11 @@ namespace Shentun.Peis /// /// 用于判断Web API需要包装返回结果. /// - public class WrapResultAttribute : ActionFilterAttribute + public class CustomerActionFilterAttribute : ActionFilterAttribute { + + + public override void OnActionExecuted(ActionExecutedContext context) { //if (context.Result is ObjectResult objRst) diff --git a/src/Shentun.Peis.HttpApi.Host/ABCExceptionFilterAttribute.cs b/src/Shentun.Peis.HttpApi.Host/Filter/CustomerExceptionFilterAttribute.cs similarity index 91% rename from src/Shentun.Peis.HttpApi.Host/ABCExceptionFilterAttribute.cs rename to src/Shentun.Peis.HttpApi.Host/Filter/CustomerExceptionFilterAttribute.cs index 822e1b1..887ec6c 100644 --- a/src/Shentun.Peis.HttpApi.Host/ABCExceptionFilterAttribute.cs +++ b/src/Shentun.Peis.HttpApi.Host/Filter/CustomerExceptionFilterAttribute.cs @@ -11,10 +11,13 @@ using Volo.Abp; namespace Shentun.Peis { - public class ABCExceptionFilterAttribute : ExceptionFilterAttribute + /// + /// 异常Filter处理 + /// + public class CustomerExceptionFilterAttribute : ExceptionFilterAttribute { - private readonly ILogger _logger; - public ABCExceptionFilterAttribute(ILogger logger) + private readonly ILogger _logger; + public CustomerExceptionFilterAttribute(ILogger logger) { _logger = logger; } diff --git a/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs b/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs index 5759ef7..4b008e0 100644 --- a/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs +++ b/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs @@ -125,8 +125,8 @@ public class PeisHttpApiHostModule : AbpModule - options.Filters.Add(typeof(ABCExceptionFilterAttribute)); - options.Filters.Add(typeof(WrapResultAttribute)); + options.Filters.Add(typeof(CustomerExceptionFilterAttribute)); + options.Filters.Add(typeof(CustomerActionFilterAttribute)); diff --git a/src/Shentun.Peis.HttpApi.Host/PlanExceptionAttribute.cs b/src/Shentun.Peis.HttpApi.Host/PlanExceptionAttribute.cs deleted file mode 100644 index 2084f78..0000000 --- a/src/Shentun.Peis.HttpApi.Host/PlanExceptionAttribute.cs +++ /dev/null @@ -1,111 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Logging; - -namespace Shentun.Peis -{ - public class PlanExceptionAttribute - { - //private readonly ILogger _logger; - - - ///// - ///// - ///// - ///// - //public override void OnException(ExceptionContext context) - //{ - - // // 如果异常没有被处理则进行处理 - // if (context.ExceptionHandled == false) - // { - // var userExpect = context.Exception as OnlineContractException; - // var info = new ExceptionModel(); - // info.code = "500"; - // info.success = false; - // info.message = "服务器遇到一个错误,无法继续为您服务!"; - - // if (userExpect != null) - // { - // info.code = $"{userExpect.Code}"; - // info.message = context.Exception.Message; - // } - // else - // { - // var volexception = context.Exception as OnlineContractException; - // if (volexception != null) - // { - // info.code = $"{volexception.Code}"; - // info.message = volexception.Message; - // } - // } - // if (context.ModelState != null) - // { - // if (context.ModelState.Count > 0) - // { - // foreach (var key in context.ModelState.Keys) - // { - // context.ModelState.TryGetValue(key, out var val); - // if (val.Errors.Count > 0) - // { - // info.message = key + "::" + val.Errors[0].ErrorMessage; - // break; - // } - // } - // } - // } - - // if (info.code == "500") - // { - // //写入日志 - // _logger.LogError(context.Exception, context.HttpContext.Request.Path, null); - - // } - - - // context.Result = new ContentResult - // { - // // 返回状态码设置为200,表示成功 - // StatusCode = int.Parse(info.code), - // // 设置返回格式 - // ContentType = "application/json;charset=utf-8", - // Content = Newtonsoft.Json.JsonConvert.SerializeObject(info) - // }; - // } - // // 设置为true,表示异常已经被处理了 - // context.ExceptionHandled = true; - - //} - - - ///// - ///// - ///// - ///// - ///// - //public override System.Threading.Tasks.Task OnExceptionAsync(ExceptionContext context) - //{ - // return base.OnExceptionAsync(context); - //} - - } - - /// - /// - /// - public class ExceptionModel - { - /// - /// - /// - public string code { get; set; } - /// - /// - /// - public bool success { get; set; } - /// - /// - /// - public string message { get; set; } - } -} diff --git a/src/Shentun.Peis.HttpApi.Host/WebApiExceptionFilterAttribute.cs b/src/Shentun.Peis.HttpApi.Host/WebApiExceptionFilterAttribute.cs deleted file mode 100644 index 48ec957..0000000 --- a/src/Shentun.Peis.HttpApi.Host/WebApiExceptionFilterAttribute.cs +++ /dev/null @@ -1,76 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; -using Shentun.Utilities; -using Shentun.WebApi.Service; -using System; -using System.Data.SqlClient; -using System.Net; -using System.Net.Http; -using Volo.Abp; -using Volo.Abp.AspNetCore.ExceptionHandling; - -namespace Shentun.Peis -{ - public class WebApiExceptionFilterAttribute : Microsoft.AspNetCore.Mvc.Filters.ExceptionFilterAttribute - { - private readonly ILogger _logger; - public WebApiExceptionFilterAttribute(ILogger logger) - { - _logger = logger; - } - //重写基类的异常处理方法 - public override void OnException(ExceptionContext exceptionContext) - { - Console.WriteLine("WebApiExceptionFilter.OnException"); - var objectResult = exceptionContext.Result as ObjectResult; - //异常日志记录 - string error = string.Empty; - ReadException(exceptionContext.Exception, ref error); - //LogHelper.Logger.LogError(error); - _logger.LogError("异常:" + error+ Environment.NewLine + "异常堆栈:" +exceptionContext.Exception.StackTrace - ); - //处理异常信息 - - string errorMessage; - - if (exceptionContext.Exception is DbUpdateException) - { - errorMessage = "数据库更新错误:" + exceptionContext.Exception.Message; - } - else if (exceptionContext.Exception is BusinessException) - { - - errorMessage = "业务错误:" + exceptionContext.Exception.Message; - } - else if (exceptionContext.Exception is ArgumentNullException) - { - - errorMessage = exceptionContext.Exception.Message; - } - else if(exceptionContext.Exception is ArgumentException) - { - - errorMessage = exceptionContext.Exception.Message; - } - else - { - errorMessage = exceptionContext.Exception.Message; - } - - exceptionContext.Result = new JsonResult(ReturnValue.CreateErrorInstance(errorMessage)); - exceptionContext.Exception = null; - //base.OnException(exceptionContext); - } - - private void ReadException(Exception ex,ref string error) - { - error += string.Format("{0} | {1} | {2}", ex.Message, ex.StackTrace, ex.InnerException); - if (ex.InnerException != null) - { - ReadException(ex.InnerException,ref error); - } - } - } -}