locked
How to create a custom 403, 404 error page RRS feed

  • Question

  • User-501297529 posted

    I need to create a custom 404 or any error code page in my solution and I'm not sure how to do it. I read that you need to use statuscodes in the startup but not sure if that is correct and if it is where to go from there.

     private static void SetErrorHandling(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    // TODO REsearch on why this is a problem for upgrading to .net core 3.0 app.UseDatabaseErrorPage();
                }
                else
                {
                    //app.UseExceptionHandler("/Home/Error");
                    app.UseStatusCodePagesWithReExecute("/StatusCode", "?code={0}");
                    app.UseHsts();
                }
            }

    Friday, January 24, 2020 2:28 PM

Answers

  • User475983607 posted

    Works for me... Is your debug/dev environment setup correctly?

    if (env.IsDevelopment())
    {
        app.UseStatusCodePagesWithReExecute("/StatusCode", "?code={0}");
    }
    else
    {
        app.UseStatusCodePagesWithReExecute("/StatusCode", "?code={0}");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 24, 2020 6:48 PM

All replies

  • User475983607 posted

    All you have to do is create a StatusCode controller and in the Index method read the code and return the appropriately named View.  You'll need to create Views named 404.cshtml and 403.cshtml  with your custom HTML message.

        public class StatusCodeController : Controller
        {
            public IActionResult Index(string code)
            {
                if (string.IsNullOrEmpty(code))
                {
                    code = "Unknown.cshtml";
                }
                return View($"/Views/Errors/{code}.cshtml");
            }
        }

     
     

    Friday, January 24, 2020 3:00 PM
  • User-501297529 posted

    All you have to do is create a StatusCode controller and in the Index method read the code and return the appropriately named View.  You'll need to create Views named 404.cshtml and 403.cshtml  with your custom HTML message.

        public class StatusCodeController : Controller
        {
            public IActionResult Index(string code)
            {
                if (string.IsNullOrEmpty(code))
                {
                    code = "Unknown.cshtml";
                }
                return View($"/Views/Errors/{code}.cshtml");
            }
        }

     
     

    OK. I'm assuming I don't need the code I posted that is in the startup class file?

    Friday, January 24, 2020 3:45 PM
  • User475983607 posted

    OK. I'm assuming I don't need the code I posted that is in the startup class file?

    Of course you need the middleware (startup).  The middleware is responsible for invoking the Action.  You just need to add the Controller and Views to complete the task.

    Friday, January 24, 2020 3:55 PM
  • User-501297529 posted

    bootzilla

    OK. I'm assuming I don't need the code I posted that is in the startup class file?

    Of course you need the middleware (startup).  The middleware is responsible for invoking the Action.  You just need to add the Controller and Views to complete the task.

    Is what I have in the startup correct?

    Friday, January 24, 2020 4:14 PM
  • User475983607 posted

    Is what I have in the startup correct?

    Yes, are you having a problem?

    Friday, January 24, 2020 4:21 PM
  • User-501297529 posted
    Haven't tried it yet but I'll keep you posted.
    Friday, January 24, 2020 5:39 PM
  • User-501297529 posted

    bootzilla

    Is what I have in the startup correct?

    Yes, are you having a problem?

    I'm still not able to get the custom view to show. I created a StatusCodeController:

     public class StatusCodeController : Controller
        {
            public IActionResult Index(string code)
            {
                if (string.IsNullOrEmpty(code))
                {
                    code = "Unknown.cshtml";
                }
                return View($"/Views/Shared/{code}.cshtml");
            }
        }

    The 404.cshtml is in the Views/Shared folder but when I debug from the start of the Index action and then type in a bad URL it doesn't hit the Index action at all and I still get the the generic 404 message instead of my 404.cshtml view. Does the code in the startup need to be updated. I'm not sure if the path format is off in that it is going to the Views/Shared folder in the controller? If not what am I doing wrong?

    Friday, January 24, 2020 6:39 PM
  • User475983607 posted

    Works for me... Is your debug/dev environment setup correctly?

    if (env.IsDevelopment())
    {
        app.UseStatusCodePagesWithReExecute("/StatusCode", "?code={0}");
    }
    else
    {
        app.UseStatusCodePagesWithReExecute("/StatusCode", "?code={0}");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 24, 2020 6:48 PM
  • User-501297529 posted

    I got it to work!!

    Friday, January 24, 2020 7:35 PM