Catch all 404 errors in an ASP.NET MVC application with the following code. Solution found via Stack Overflow http://stackoverflow.com/questions/717628/asp-net-mvc-404-error-handling
Last active
January 2, 2016 18:09
-
-
Save jcreamer898/8341711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult NotFound() | |
{ | |
ActionResult result; | |
object model = Request.Url.PathAndQuery; | |
if (!Request.IsAjaxRequest()) | |
{ | |
result = View("NotFound", model); | |
} | |
else | |
{ | |
result = PartialView("NotFound", model); | |
} | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void Application_EndRequest() | |
{ | |
if (Context.Response.StatusCode == 404) | |
{ | |
Response.Clear(); | |
var rd = new RouteData(); | |
rd.Values["controller"] = "Errors"; | |
rd.Values["action"] = "NotFound"; | |
IController c = new ErrorController(); | |
c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment