Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Exception Filters returns an empty body in asp.net-core 1.1 #5594

@robertovanoli

Description

@robertovanoli

I have a very simple controller with an ExceptionFilterAttribute that throws an exception

   [TypeFilter(typeof(MyControllerExceptionFilterAttribute))]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            throw new Exception("Hello Exception");
            return View();
        }
   }
}

this is the ExceptionFilterAttribute

   [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
    public class MyControllerExceptionFilterAttribute : ExceptionFilterAttribute
    {
        protected readonly IHostingEnvironment _hostingEnvironment;
        protected readonly IModelMetadataProvider _modelMetadataProvider;

        public MyControllerExceptionFilterAttribute(IHostingEnvironment hostingEnvironment, IModelMetadataProvider modelMetadataProvider)
        {
            _hostingEnvironment = hostingEnvironment;
            _modelMetadataProvider = modelMetadataProvider;
        }

        public override void OnException(ExceptionContext context)
        {
            if (context.ExceptionHandled) return;
            var result = new ViewResult { ViewName = "ApplicationError" };
            context.ExceptionHandled = true; // mark exception as handled
            context.HttpContext.Response.Clear();
            context.Result = result;
        }
    }

this works fine in asp.net core 1.0, while in asp.net core 1.1 the result is not rendered anymore and the returned http response is like this:

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

You can reproduce very easily in this way

  1. create a new ASP.NET Core Web Application (.NET Framework). by default asp.net core 1.0 is installed. then add the controller the ExceptionFilterAttribute above and a very simple ApplicationError view: the view is rendered
  2. update to asp.net core 1.1 then excecute: the view is not rendered and the returned page is blank
  3. comment the row 'context.ExceptionHandled = true;' in ExceptionFilterAttribute: at this point the view is rendered as in step 1.

my conclusion is that 'context.ExceptionHandled = true' is not working as expected

I've made this small project to demostrate the behavior https://www.dropbox.com/s/dl1xa7d2nzy48o8/WebApplication3.zip?dl=0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions