Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a C# solution, I hope it helps. I plagiarized some of this code, though for the life of me, I cannot find where I got it from. If anyone know, please let me know so I can add it to my comments. </p> <p>This solution does not use reflection, but it looks at all the application errors (exceptions) and checks to see if it's a 404 error. If it is, then it just routes the current request to a different controller. Though I am not an expert in any way, I think this solution might be faster than reflection. Anyway, here's the solution and it goes into your Global.asax.cs,</p> <pre><code> protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // A good location for any error logging, otherwise, do it inside of the error controller. Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "YourErrorController"); if (httpException != null) { if (httpException.GetHttpCode() == 404) { routeData.Values.Add("action", "YourErrorAction"); // We can pass the exception to the Action as well, something like // routeData.Values.Add("error", exception); // Clear the error, otherwise, we will always get the default error page. Server.ClearError(); // Call the controller with the route IController errorController = new ApplicationName.Controllers.YourErrorController(); errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); } } } </code></pre> <p>So the controller would be,</p> <pre><code>public class YourErrorController : Controller { public ActionResult YourErrorAction() { return View(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload