![](/img/trans.png)
[英]Response Compression Middleware for ASP.NET Core ignored on Azure
[英]Set response status in ASP.NET core middleware
我有一個中間件,它隱藏客戶端的異常並在出現任何異常時返回 500 錯誤:
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
public ExceptionHandlingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next.Invoke(context);
}
catch (Exception exception)
{
var message = "Exception during processing request";
using (var writer = new StreamWriter(context.Response.Body))
{
context.Response.StatusCode = 500; //works as it should, response status 500
await writer.WriteAsync(message);
context.Response.StatusCode = 500; //response status 200
}
}
}
}
我的問題是,如果我在寫入正文之前設置響應狀態,客戶端將看到此狀態,但是如果我在將消息寫入正文后設置狀態,客戶端將收到狀態為 200 的響應。
有人可以解釋我為什么會這樣嗎?
PS 我使用的是 ASP.NET Core 1.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.