下面的异常筛选器将异常重定向到我的特定错误页面。
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class FrontofficeControllerExceptionFilterAttribute : ExceptionFilterAttribute
{
protected readonly ILogger<FrontofficeController> _logger;
protected readonly IHostingEnvironment _hostingEnvironment;
protected readonly IModelMetadataProvider _modelMetadataProvider;
public FrontofficeControllerExceptionFilterAttribute(ILogger<FrontofficeController> logger, IHostingEnvironment hostingEnvironment, IModelMetadataProvider modelMetadataProvider)
{
_logger = logger;
_hostingEnvironment = hostingEnvironment;
_modelMetadataProvider = modelMetadataProvider;
}
public override void OnException(ExceptionContext context)
{
if (context.ExceptionHandled) return;
Exception ex = context.Exception;
var result = new ViewResult { ViewName = "ApplicationError" };
context.ExceptionHandled = true; // mark exception as handled
context.Result = result;
}
}将我的web应用程序迁移到asp.net Core1.1后,结果是一个空白页面:响应的正文为空,Content-Length为零。这是在客户端上收到的响应
HTTP/1.1 200 OK
Server: Kestrel
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcaW50ZXJhaC52aXN1YWxzdHVkaW8uY29tXEludmVudGFyaW9cTWFpblxTb3VyY2VcRnJvbnRvZmZpY2Vcc3JjXEZyb250b2ZmaWNl?=
X-Powered-By: ASP.NET
Date: Wed, 30 Nov 2016 14:49:53 GMT
Content-Length: 0有没有人遇到过类似的问题?为什么?感谢您的评论
发布于 2016-12-14 20:29:33
该行为得到了github aspnet/mvc团队的确认。https://github.com/aspnet/Mvc/issues/5594#issuecomment-265866653
目前,唯一的解决方案是直接呈现HTML响应。在这种情况下,将ExceptionHandled = true标记为true的效果与预期一致。
public override void OnException(ExceptionContext context)
{
context.ExceptionHandled = true; // mark exception as handled
context.HttpContext.Response.Clear();
context.HttpContext.Response.StatusCode = 400;
context.HttpContext.Response.ContentType = new MediaTypeHeaderValue("text/html").ToString();
var home = string.Format("{0}://{1}", context.HttpContext.Request.Scheme,context.HttpContext.Request.Host);
var htmlString ="<h2>SI E' VERIFICATO UN ERRORE INATTESO</h><br/><br/>";
context.HttpContext.Response.WriteAsync(htmlString, Encoding.UTF8);
}https://stackoverflow.com/questions/40892538
复制相似问题