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
|
{
|
/// <summary>
|
/// 委托
|
/// </summary>
|
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);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 静态类
|
/// </summary>
|
public static class ExceptionMiddlewareExtension
|
{
|
/// <summary>
|
/// 静态方法
|
/// </summary>
|
/// <param name="app">要进行扩展的类型</param>
|
public static void UseExceptionMiddleware(this IApplicationBuilder app)
|
{
|
app.UseMiddleware(typeof(CustomerExceptionMiddleware));
|
}
|
}
|
}
|