3

I have created one demo for show our custom error page in MVC 5. I am having success when entering two slashes, but when I am entering more than two slashes, I am not getting my custom error page, but rather a MVC error as shown below.

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

This is how my Web.config file looks like.

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

This is my ErrorController.

public ViewResult NotFound()
{
    return View();
}

My current setup works fine when I enter the URL below.

http://localhost:14360/search/fdsfdf

But when enter one of the URLs below, my custom error page is not displayed.

http://localhost:14360/search/fdsfdf/sdsdsd/asdsadasd/dasdsad
http://localhost:14360/search/fdsfdf/dsads/fe

Coder
  • 71
  • 10
  • A 404 is appropriate here. An error page is displayed when there's an error in the application. If the end user uses a invalid URL the web server itself will respond with a 404. If the application doesn't recognize a URL it won't even try to process it. – Panagiotis Kanavos Jul 23 '18 at 09:23
  • This has nothing to do with slashes. What is this URL supposed to represent? A subsite calles `search` with an area called `fdsfdf`, a `dsads` controller that accepts an ID parameter with the value `fe`? Or did you intend to create a `SearchController` with a `Get` method that accepts 3 parameters specified in the route? And another with 5 parameters? – Panagiotis Kanavos Jul 23 '18 at 09:25
  • @PanagiotisKanavos In my search controller have one name of method detail method and I am type search/fdsfdf/dsads like this then In my search controller can't have any name of the method fdsfdf so conceptually my url is wrong so why my custom page is not show there? – Coder Jul 23 '18 at 09:31
  • You'll find the explanation and workarounds in [this duplicate question](https://stackoverflow.com/questions/717628/asp-net-mvc-404-error-handling/9026907), especially the most voted answer – Panagiotis Kanavos Jul 23 '18 at 09:42
  • 1
    Possible duplicate of [ASP.NET MVC 404 Error Handling](https://stackoverflow.com/questions/717628/asp-net-mvc-404-error-handling) – Panagiotis Kanavos Jul 23 '18 at 09:46
  • @PanagiotisKanavos I have tried all way but still getting the same issue so can you please help me on that how can I fix this issue?? – Coder Jul 23 '18 at 12:15

2 Answers2

1

The problem you are facing comes down to the fact that some errors are handled by ASP.NET and others by IIS. You can read more on that topic here.

I would suggest you create an ErrorsController, which handles the most common application errors and returns the respective views. Additionally, you have to configure your Web.config (or Web.Release.config) file to use the newly created controller and you also might have to replace the default error pages of your IIS (for me, they are located within the InetPub\custerr\en-US\ directory).

Controller (ErrorsController.cs)

public class ErrorsController : Controller
{
    // GET: Errors/Unauthorized
    public ActionResult Unauthorized()
    {
        return new HttpStatusCodeResult(401);
    }

    // GET: Errors/Forbidden
    public ActionResult Forbidden()
    {
        return new HttpStatusCodeResult(403);
    }

    // Additional Errors
}

Configuration File (Web.Release.config)

<customErrors defaultRedirect="~/" mode="RemoteOnly" xdt:Transform="Replace">
  <error statusCode="401" redirect="~/Errors/Unauthorized"/>
  <error statusCode="403" redirect="~/Errors/Forbidden"/>
  <!-- Additional Errors -->
</customErrors>
1

Default /App_Start/RouteConfig.cs have

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

so you enter http://localhost:14360/search/fdsfdf or http://localhost:14360/search/fdsfdf/1234 will get httpStatusCode 200,but http://localhost:14360/search/fdsfdf/dsads/fe is undefine url, asp.net will return 404 to browser if you need custom error page, try add httpErrors and customErrors handle httpStatusCode 404

<system.webServer>
    <httpErrors existingResponse="Auto" errorMode="Custom" defaultResponseMode="File">
        <remove statusCode="404" subStatusCode="-1"/>
        <error statusCode="404" path="/Error/404" responseMode="Redirect" />
    </httpErrors>
</system.webServer>

<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/Error/Index">
        <error redirect="~/Error/404" statusCode="404" />
    </customErrors>
</system.web>
lex.xu
  • 199
  • 3