Last post May 10, 2018 05:47 PM by mgebhard
Member
6 Points
8 Posts
May 10, 2018 05:37 PM|Sachinsboa|LINK
Is there any way to catch this type of request using a route or something else in the MVC framework itself?
433 Points
31 Posts
May 10, 2018 05:39 PM|Satyaprakash Samantaray|LINK
protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); 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"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception); // Clear the error on server. Server.ClearError(); // 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)); }
All-Star
53041 Points
23614 Posts
May 10, 2018 05:47 PM|mgebhard|LINK
Sachinsboa Is there any way to catch this type of request using a route or something else in the MVC framework itself?
It is already caught and the reason MVC returns a 404. Are you trying to return a custom error page?
http://benfoster.io/blog/aspnet-mvc-custom-error-pages
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs
Something else?
Member
6 Points
8 Posts
404 in ASP.NET MVC how to solve it
May 10, 2018 05:37 PM|Sachinsboa|LINK
Is there any way to catch this type of request using a route or something else in the MVC framework itself?
Member
433 Points
31 Posts
Re: 404 in ASP.NET MVC how to solve it
May 10, 2018 05:39 PM|Satyaprakash Samantaray|LINK
All-Star
53041 Points
23614 Posts
Re: 404 in ASP.NET MVC how to solve it
May 10, 2018 05:47 PM|mgebhard|LINK
It is already caught and the reason MVC returns a 404. Are you trying to return a custom error page?
http://benfoster.io/blog/aspnet-mvc-custom-error-pages
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs
Something else?