I do error handling (403, 404, 500). To do this, made a separate controller and presentation.
public class ErrorController : Controller { // GET: Error public ActionResult General(Exception exception) { return View("Exception", exception); } public ActionResult Http404() { return View(); } public ActionResult Http403() { return View(); } } In Global.asax.cs registered the following method:
protected void Application_Error() { var exception = Server.GetLastError(); log.Error(exception); Response.Clear(); var httpException = exception as HttpException; var routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values.Add("action", "Http403"); break; case 404: routeData.Values.Add("action", "Http404"); break; default: routeData.Values.Add("action", "Error"); break; } } else { routeData.Values.Add("action", "General"); routeData.Values.Add("exception", exception); } Server.ClearError(); Response.TrySkipIisCustomErrors = true; IController errorsController = new ErrorController(); errorsController.Execute(new RequestContext( new HttpContextWrapper(Context), routeData)); } If I enter a non-existent controller into the Url (example: http://localhost:62693/123 ), my browser displays the HTML code of my "erroneous" view (instead of the view itself).
If the entered path as a whole does not exist, but the controller is specified correctly in it (example: http://localhost:62693/News/123 ), then the view itself is displayed correctly in the browser.