0

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:

enter image description here

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?

pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • Take a look at [this link](https://benfoster.io/blog/aspnet-mvc-custom-error-pages) otherwise there is an MVC 5 guide [here](https://dzone.com/articles/make-custom-error-pages-work-in-aspnet-mvc-5). – Matt Stannett May 29 '19 at 01:24

1 Answers1

0

Everything was set up perfect. Only issue was that I was modifying Web.Debug.config instead of the Web.config file. Once I moved my config changes to the parent Web.config file, my error pages started showing up.

Also, this: defaultRedirect="~/Views/Error/Error" did not work for me as a default redirect. Had to be a controller action instead of a path. Why path works for others, Im yet to know as this whole space is new to me.

Hope this helps someone someday.

pnizzle
  • 6,243
  • 4
  • 52
  • 81