.net core web api - 控制器可以将参数传递给中间件吗?
.net core web api - Can controller pass parameters to middleware?
您好,我需要捕获 http 请求的异常,例如:
[HttpPost("Test")]
public async Task<ActionResult<TestResponse>> Test(TestRequest request)
{
TestResponse result;
try
{
// call 3rd party service
}
catch(exception ex)
{
result.Errorcode = "Mock" // This Errorcode will be used by client side
}
return Ok(result);
}
现在http请求比较多,想用一个中间件来全局处理异常,而不是
如上所述在每个http请求中编写try-catch语句。
public class Middleware
{
readonly RequestDelegate next;
public Middleware(RequestDelegate next)
{
this.next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await next(httpContext);
}
catch (Exception ex)
{
// is there a way to pass TestResponse here so I can do result.Errorcode = "Mock"?
}
}
}
我不知道如何使用上面注释的中间件方法分配错误代码。可能吗?谢谢
如果我很了解您的要求,我建议这样做:
您不需要访问 TestResponse,您可以在中间件中配置您的响应。
public class FailResponseModel
{
public FailResponseModel(string errorCode, object errorDetails)
{
ErrorCode = errorCode;
ErrorDetails = errorDetails;
}
public string ErrorCode { get; set; }
public object ErrorDetails { get; set; }
}
public class ExceptionHandlerMiddleware
{
readonly RequestDelegate next;
public Middleware(RequestDelegate next)
{
this.next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await next(httpContext);
}
catch (Exception ex)
{
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
httpContext.Response.ContentType = "application/json";
var response =
JsonConvert.SerializeObject(new FailResponseModel("your-error-code", "your-error-details"));
await httpContext.Response.WriteAsync(response);
}
}
}
您好,我需要捕获 http 请求的异常,例如:
[HttpPost("Test")]
public async Task<ActionResult<TestResponse>> Test(TestRequest request)
{
TestResponse result;
try
{
// call 3rd party service
}
catch(exception ex)
{
result.Errorcode = "Mock" // This Errorcode will be used by client side
}
return Ok(result);
}
现在http请求比较多,想用一个中间件来全局处理异常,而不是
如上所述在每个http请求中编写try-catch语句。
public class Middleware
{
readonly RequestDelegate next;
public Middleware(RequestDelegate next)
{
this.next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await next(httpContext);
}
catch (Exception ex)
{
// is there a way to pass TestResponse here so I can do result.Errorcode = "Mock"?
}
}
}
我不知道如何使用上面注释的中间件方法分配错误代码。可能吗?谢谢
如果我很了解您的要求,我建议这样做:
您不需要访问 TestResponse,您可以在中间件中配置您的响应。
public class FailResponseModel
{
public FailResponseModel(string errorCode, object errorDetails)
{
ErrorCode = errorCode;
ErrorDetails = errorDetails;
}
public string ErrorCode { get; set; }
public object ErrorDetails { get; set; }
}
public class ExceptionHandlerMiddleware
{
readonly RequestDelegate next;
public Middleware(RequestDelegate next)
{
this.next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await next(httpContext);
}
catch (Exception ex)
{
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
httpContext.Response.ContentType = "application/json";
var response =
JsonConvert.SerializeObject(new FailResponseModel("your-error-code", "your-error-details"));
await httpContext.Response.WriteAsync(response);
}
}
}