我有一个返回文件的控制器方法,并且在所有浏览器中,这与IE11不同.在IE11中,我得到500服务器异常.在我的dotnet运行控制台命令中,我收到此消息.
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id “0HLAA8HNC511P”, Request id “0HLAA8HNC511P:00000007”: An unhandled exception was thrown by the
application. System.InvalidOperationException: Response Content-Length
mismatch: too few bytes written (0 of 9283).
即使通过添加app.UseDeveloperExceptionPage(),我似乎也无法捕获异常.在我的Startup.cs文件中调用.
我的控制器方法非常简单,看起来像这样
public async Task<IActionResult> GetAsync([FromRoute] long id, [FromRoute] long fileId, [FromQuery] FilePreviewModel previewOptions)
{
var entity = await _fileService.GetAsync(Module, id, fileId);
var fileName = "MEARS 2000 LOGO";
var contentType = "image/gif";
// this is a byte array
var data = entity.Data.Content;
// return file content
return File(data, contentType, fileName);
}
在IE11中,请求和响应标头看起来像这样.
在chrome中,我的标题看起来像这样.
我已将我的dotnet SDK更新到2.1.3版.
任何人都知道可能会发生什么?
最佳答案 我也有这个问题,相反,它不会总是抛出错误.
管理通过删除操作中的标头来解决它,如下所示:
[HttpGet("{container}/{id}")]
public async Task<IActionResult> Get(string container, string id)
{
/* remove both of these headers... put a warning here to apply the fix after dotnet team distributes the fix. */
HttpContext.Request.Headers.Remove("If-Modified-Since");
HttpContext.Request.Headers.Remove("If-None-Match");
var _fileInfo = provider.GetFileInfo($"{container}/{id}");
if (!_fileInfo.Exists || string.IsNullOrEmpty(id))
/* return a default file here */
var last = _fileInfo.LastModified;
/* ... some code removed for brevity */
return base.File(_fileInfo.CreateReadStream(), MimeTypeMap.GetMimeType(id.Substring(id.LastIndexOf("."))), id, lastModified: _lastModified, entityTag: _etag);
}
dotnet –info显示版本:2.0.4