Reputation: 81
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.
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
Upvotes: 3
Views: 2563
Reputation: 199
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>
Upvotes: 1
Reputation:
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>
Upvotes: 1