1

I'd like to show a different 404 page for a specific controller then using the default I have now. How can I do this? Here is my setup now:

 protected void Application_Error()
        {
            if (!Request.IsLocal)
            {
                var context = new HttpContextWrapper(HttpContext.Current);

                if (!context.Request.IsAjaxRequest())
                {
                    context.Response.ContentType = "text/html";

                    var unhandledException = Server.GetLastError();
                    var httpException = unhandledException as HttpException;
                    if (httpException == null)
                    {
                        var innerException = unhandledException.InnerException;
                        httpException = innerException as HttpException;
                    }

                    Server.ClearError();
                    var routeData = new RouteData();

                    routeData.Values.Add("controller", MVC.Errors.Name);

                    if (httpException != null)
                    {
                        var httpCode = httpException.GetHttpCode();
                        switch (httpCode)
                        {
                            case (int) HttpStatusCode.NotFound:
                                routeData.Values.Add("action", "PageNotFound");

                                IController pageNotFoundController = new ErrorsController();
                                pageNotFoundController.Execute(new RequestContext(context, routeData));
                                break;
                        }
                    }
                    else
                    {
                        routeData.Values.Add("exception", unhandledException);
                        routeData.Values.Add("action", "Error");
                        IController errorController = new ErrorsController();
                        errorController.Execute(new RequestContext(context, routeData));
                    }
                }
            }
        }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • Check out these existing [http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc](http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc) answers – TugboatCaptain Feb 03 '14 at 23:57

1 Answers1

0
     public class ErrorController : Controller {
         publc ActionResult NotFound()
         {
           Return View();
          }
      }

      <customErrors mode="On">
         <error statusCode="404" redirect="~/Error/NotFound"/>
      </customErrors>

In the NotFound Action you can specify your logic. I hope this will help you.