Skip to content

app.UseExceptionHandler("/Error"); doesn't work on POST methods. #3555

Closed
@TanvirArjel

Description

@TanvirArjel

I have configured ASP.NET Core default Exception Handler as follows:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                //app.UseHsts();
            }
}

Whenever any error occurs in the GET methods, it is working as expected that is it is being caught and redirected to the Error() method.

Problem is that whenever any error occurs in the POST methods, it is not working as expected that is it is not being caught and redirected to the Error() method.

Here is sample code:

public class HomeController : Controller
{
       // GET: ContactInfo/Create
        public async Task<IActionResult> Create()
        {
           Convert.ToInt32("Hello");  // Intentional error
            return View();
        }

        // POST: ContactInfo/Create
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(ContactInfo contactInfo)
        {
            Convert.ToInt32("Hello"); // Intentional error
            if (ModelState.IsValid)
            {
                return RedirectToAction(nameof(Index));
            }
            
            return View(contactInfo);
        }

         [HttpGet]
        [Route("/Error/{id?}")]
        public IActionResult Error(int? id)
        {
            if (id == 404)
            {
                return View("PageNotFound");
            }

            var feature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
           // Necessary staffs here

            return View();
        }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions