in Education by
I have implemented exception handling as mentioned in below link How to pass error message to error view in MVC 5? It is working fine. But I have requirement to handle 404 Error. How can I do that? if I use below code, it works well when any 404 error occurs. But in case any other exception occurs then my error.cshtml call twice and show same exception two times. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
web.config Turn off custom errors in system.web configure http errors in system.webServer Create simple error controller to handle those requests ErrorContoller.cs [AllowAnonymous] public class ErrorController : Controller { // GET: Error public ActionResult NotFound() { var statusCode = (int)System.Net.HttpStatusCode.NotFound; Response.StatusCode = statusCode; Response.TrySkipIisCustomErrors = true; HttpContext.Response.StatusCode = statusCode; HttpContext.Response.TrySkipIisCustomErrors = true; return View(); } public ActionResult Error() { Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError; Response.TrySkipIisCustomErrors = true; return View(); } } configure routes RouteConfig.cs public static void RegisterRoutes(RouteCollection routes) { //...other routes routes.MapRoute( name: "404-NotFound", url: "NotFound", defaults: new { controller = "Error", action = "NotFound" } ); routes.MapRoute( name: "500-Error", url: "Error", defaults: new { controller = "Error", action = "Error" } ); //..other routes //I also put a catch all mapping as last route //Catch All InValid (NotFound) Routes routes.MapRoute( name: "NotFound", url: "{*url}", defaults: new { controller = "Error", action = "NotFound" } ); } And finally make sure you have views for the controller actions Views/Shared/NotFound.cshtml Views/Shared/Error.cshtml If there are any additional error you want to handle you can follow that pattern and add as needed. This will avoid redirects and maintain the original http error status that was raised.

Related questions

0 votes
    I'd like to use two view engines in my asp.net mvc web application. The first one is the Brail ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I've started to work a bit with master pages for an ASP.net mvc site and I've come across a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I've started to work a bit with master pages for an ASP.net mvc site and I've come across a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    Say I have a page that display search results. I search for stackoverflow and it returns 5000 results, 10 per page. Now I ... was posted previously etc.. What I'd like to do is...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    Has anybody established a good naming convention for action in MVC? I was specifically looking at ASP.net ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    I'm looking to build an reusable control or custom helper for my MVC project. I'm sure there is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm looking to build an reusable control or custom helper for my MVC project. I'm sure there is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm looking to build an reusable control or custom helper for my MVC project. I'm sure there is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Say I have a page that display search results. I search for stackoverflow and it returns 5000 results, 10 per page. Now I ... was posted previously etc.. What I'd like to do is...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    how I can use Elmah logger only when I want? I have ErrorsController and I need log it only when ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I have a viewmodel with an Id property [Required] public int Id { get; set; } But I think this ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I have a viewmodel with an Id property [Required] public int Id { get; set; } But I think this ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I have already tried to make a login form and I also want to display the user login profile that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have already tried to make a login form and I also want to display the user login profile that ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
...