在我们基于 ASP.NET Core 的 Web 应用程序中,我们需要以下内容:某些请求的文件类型应获得自定义 ContentType 的响应。例如.map
应该映射到application/json
. 在“完整”的 ASP.NET 4.x 中并结合 IIS,可以利用 web.config<staticContent>/<mimeMap>
来实现这一点,我想用自定义的 ASP.NET Core 中间件替换这种行为。
所以我尝试了以下方法(为简洁起见):
public async Task Invoke(HttpContext context)
{
await nextMiddleware.Invoke(context);
if (context.Response.StatusCode == (int)HttpStatusCode.OK)
{
if (context.Request.Path.Value.EndsWith(".map"))
{
context.Response.ContentType = "application/json";
}
}
}
context.Response.ContentType
不幸的是,在调用中间件链的其余部分后尝试设置会导致以下异常:
System.InvalidOperationException: "Headers are read-only, response has already started."
如何创建解决此要求的中间件?