Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TanvirArjel opened this issue Sep 25, 2018 · 3 comments
Closed

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

TanvirArjel opened this issue Sep 25, 2018 · 3 comments

Comments

@TanvirArjel
Copy link
Contributor

TanvirArjel commented Sep 25, 2018

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();
        }
}
@TanvirArjel TanvirArjel changed the title app.UseExceptionHandler("/Error"); doesn't work on POST method. app.UseExceptionHandler("/Error"); doesn't work on POST methods. Sep 25, 2018
@Tratcher
Copy link
Member

Only the path is re-written, not the method. You have your Error action marked as handling Get requests, not Post.

@Eilon
Copy link
Contributor

Eilon commented Sep 25, 2018

You should be able to remove the [HttpGet] from the Error method because it is what's causing the unnecessary restriction.

@Eilon Eilon closed this as completed Sep 25, 2018
@TanvirArjel
Copy link
Contributor Author

@Tratcher @Eilon Yes! You are correct! I didn't notice that. Thank you so much.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants