I have followed the answer on this post here, but I am unable to get the app to redirect to the custom page upon encountering a 404 error.
When I visit localhost:12345/error/NotFound
the error page is shown fine. So the controller to view link is all there.
I have setup an <a>
tag with an invalid controller name to trigger the error page. Upon clicking the link i am taken to the browsers default error page and not my custom page.
I have tried adding <httpErrors existingResponse="PassThrough" />
to <system.webServer>
I have tried commenting out filters.Add(new HandleErrorAttribute());
in FilterConfig.cs
I have tried adjusting defaultRedirect
in my web.config file.
Nothing has worked for me, and I can not find any good straight forward documentation on this. And seems people have different approaches and different solutions working for them and not for others. Just makes me wonder...
Here is my code:
Web.Debug.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<customErrors mode="On" defaultRedirect="~/Views/Error/Error">
<error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>
</system.web>
</configuration>
Error Controller:
public class ErrorController : Controller
{
[AllowAnonymous]
public PartialViewResult Error()
{
return PartialView("Error");
}
[AllowAnonymous]
public PartialViewResult NotFound()
{
Response.StatusCode = 404;
return PartialView("Error");
}
}
Error.cshtml:
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="list-header clearfix">
<span>Error</span>
</div>
<div class="list-sfs-holder">
<div class="alert alert-error">
An unexpected error has occurred. Please contact the system administrator.
</div>
@{
if (Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<div>
<p>
<b>Exception:</b> @Model.Exception.Message<br />
<b></b> @Model.ControllerName<br />
<b></b> @Model.ActionName
</p>
<div style="overflow: scroll">
<pre>
@Model.Exception.StackTrace
</pre>
</div>
</div>
}
}
</div>
Location of Error.cshtml:
Is there something I am doing wrong? Is there some other setting I have to handle? Is my redirect path supposed to point to error.cshtml or to its location?