HTTP Error 404.0 - Not Found in MVC 5

12.2k views Asked by At

My project in MVC 5.i wanted to handel 404. i did it but problem is that

When I access the view using the following URL, everything works fine and as expected:

http://localhost/Hotels/Index30701000000

But when I access the view using following URL, I get 404.0 error message (error shown below)

http://localhost/Hotels/Edit/09099999dfdfb                       
http://localhost/Hotels/Edit/090/20130701000000

Error: HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

My code is

Controller

public class ErrorController : Controller
{
    // GET: Error
    public ActionResult Unauthorized()
    {
        Response.StatusCode = 404;
        Response.TrySkipIisCustomErrors = true;
        return View();
    }
}

RouteConfig

    routes.MapRoute(
        name: "Unauthorized",
        url: "Unauthorized/{action}/{id}",
        defaults: new
        {
            controller = "Error",
            action = "Unauthorized",
            id = UrlParameter.Optional
        }
    );

Global.asax

    protected void Application_Error()
    {
        Exception exception = Server.GetLastError();
        HttpException httpException = exception as HttpException;
    }

webconfig

  <system.web>
    <customErrors mode="On" defaultRedirect="Error">
      <error statusCode="404" redirect="Unauthorized" />
    </customErrors>
  </system.web>

View //Unauthorized.cshtml

@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = "Unauthorized";
}
<h1>Page Not Found!</h1>

Reference Link

2

There are 2 answers

0
AudioBubble On BEST ANSWER

just Write these lines of code in global.asax file of your application.

protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
            if ((!Request.RawUrl.Contains("style")) && (!Request.RawUrl.Contains("images")))
            {
                Response.Clear();
                if (Response.StatusCode == 404)
                {
                    Response.Redirect("/ControllerName/ActionName");
                }
            }
        }
    }
0
Eric On

Another approach would be to define your error pages in an ErrorController, then use httpError section under system.WebServer in your web.config.

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
  <remove statusCode="500" />
  <error statusCode="500" responseMode="ExecuteURL" path="/Error/Error" />
</httpErrors>

The code above is so you can test your custom page. But in your development environment, you could replace Custom with errorMode="DetailedLocalOnly" and existingResponse="Auto" so that you can still see the details errors.

I wrote a post about it recently, and it comes with a sample demo project available on GitHub you can play with. http://www.neptunecentury.com/Blogs/ASP-NET/MVC5/MVC-5-How-To-Show-Custom-Error-Pages