How can we handle 404 in ASP.NET MVC?

How can we handle 404 in ASP.NET MVC?


First Install ILogger from nuget package :


protected void Application_Error(object sender, EventArgs e)


        Exception exception = Server.GetLastError();

        // Log the exception.


        ILogger logger = Container.Resolve<ILogger>();





        HttpException httpException = exception as HttpException;


        RouteData routeData = new RouteData();

        routeData.Values.Add("controller", "Error");


        if (httpException == null)


            routeData.Values.Add("action", "Index");


        else //It's an Http Exception, Let's handle it.


            switch (httpException.GetHttpCode())


                case 404:

                    // Page not found.

                    routeData.Values.Add("action", "HttpError404");


                case 500:

                    // Server error.

                    routeData.Values.Add("action", "HttpError500");



                // Here you can handle Views to other error codes.

                // I choose a General error template  


                    routeData.Values.Add("action", "General");





        // Pass exception details to the target error View.

        routeData.Values.Add("error", exception);


        // Clear the error on server.



        // Avoid IIS7 getting in the middle

        Response.TrySkipIisCustomErrors = true;


        // Call target Controller and pass the routeData.

        IController errorController = new ErrorController();

        errorController.Execute(new RequestContext(

             new HttpContextWrapper(Context), routeData));



(Visited 14 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *