添加项目文件。

This commit is contained in:
dell
2023-03-03 16:07:50 +08:00
parent 2c462551b6
commit 011039960e
585 changed files with 362460 additions and 0 deletions

View File

@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Threading.Tasks;
namespace HT.Cloud.Code
{
/// <summary>
/// 全局异常过滤器,MVC使用
/// </summary>
public class GlobalExceptionFilter : IExceptionFilter, IAsyncExceptionFilter
{
public void OnException(ExceptionContext context)
{
LogHelper.WriteWithTime(context);
if (context.HttpContext.Request.IsAjaxRequest())
{
AlwaysResult obj = new AlwaysResult();
obj.state = ResultType.error.ToString();
obj.message = context.Exception.GetOriginalException().Message;
if (string.IsNullOrEmpty(obj.message))
{
obj.message = "抱歉,系统错误,请联系管理员!";
}
context.Result = new JsonResult(obj);
context.ExceptionHandled = true;
}
else
{
//context.HttpContext.Response.WriteAsync("<script>top.location.href ='" + context.HttpContext.Request.PathBase + "/Home/Error?msg=500" + "';if(document.all) window.event.returnValue = false;</script>");
context.Result = new RedirectResult(context.HttpContext.Request.PathBase + "/Home/Error?msg=500");
context.ExceptionHandled = true;
}
}
public Task OnExceptionAsync(ExceptionContext context)
{
OnException(context);
return Task.CompletedTask;
}
}
}

View File

@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Threading.Tasks;
namespace HT.Cloud.Code
{
/// <summary>
/// 全局异常中间件api使用
/// </summary>
public class GlobalExceptionMiddleware
{
private readonly RequestDelegate next;
public GlobalExceptionMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context /* other dependencies */)
{
try
{
await next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = HttpStatusCode.OK;
LogHelper.WriteWithTime(exception);
var result = JsonConvert.SerializeObject(new AlwaysResult
{
state = ResultType.error.ToString(),
message = exception.Message
});
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)code;
return context.Response.WriteAsync(result);
}
}
}

View File

@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace HT.Cloud.Code
{
/// <summary>
/// 模型验证过滤器
/// </summary>
public class ModelActionFilter : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext context)
{
string message = "";
if (!context.ModelState.IsValid)
{
foreach (var item in context.ModelState.Values)
{
foreach (var error in item.Errors)
{
message = message += error.ErrorMessage + "|";
}
}
if (message.Length > 0)
{
message = message.Substring(0, message.Length - 1);
}
context.Result = new JsonResult(new AlwaysResult { state = ResultType.error.ToString(), message = message });
}
}
}
}