using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System;
using System.Text.Json;
using Microsoft.AspNetCore.Builder;
namespace Wms_09.Filter
{
public class CustomerExceptionMiddleware
{
///
/// 委托
///
private readonly RequestDelegate _next;
public CustomerExceptionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
context.Response.ContentType = "application/json";
var result = new { code = 1, msg = "系统异常" };
var stream = context.Response.Body;
await JsonSerializer.SerializeAsync(stream, result);
}
}
}
///
/// 静态类
///
public static class ExceptionMiddlewareExtension
{
///
/// 静态方法
///
/// 要进行扩展的类型
public static void UseExceptionMiddleware(this IApplicationBuilder app)
{
app.UseMiddleware(typeof(CustomerExceptionMiddleware));
}
}
}