I'm making error handling (403, 404, 500). I made ErrorController and Views:
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();
}
}
Then I wrote the following code in Global.asax.cs:
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));
}
It works when url with controller name (example: http://localhost:62693/News/123), then I got error's 404 view.
But when I send url without controller name in it or with invalid controller name (example: http://localhost:62693/123 or http://localhost:62693/new/k) I got only HTML code of error's 404 view.
How can I get error's 404 view with any urls?