I am struggling to setup the custom 403 page for Umbraco 10 project. Custom 404 and Error page is working fine, but for 403 it's not working.
I am pasting my code below.
TBH, I didn't manage to get it to work that way :D
What I have done is created a CustomError Document type, using that I created several error pages, error404 (assigned as the umbraco 404), errorNoAcess and errorHandler.
These are just basic page templates with a title and message.
I discovered that depending how the 401/403 is raised/returned, you could get an Unauthorised exception or just a http statuscode, so you have to handle both :)
So, I create my own exceptionhandler midddleware to handle any exceptions and added some code to my startup.cs to handle the status codes
if (env.IsDevelopment())
{
// app.UseDeveloperExceptionPage();
app.CustomExceptionHandler();
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404)
{
context.Request.Path = "/error404/";
await next();
}
if (context.Response.StatusCode is 401 or 403)
{
context.Request.Path = "/errorNoAccess/";
await next();
}
//This is a BadRequest so use the default handler (500)
if (context.Response.StatusCode == 500)
{
context.Request.Path = "/error500/";
await next();
}
});
app.UseUmbraco()
.WithMiddleware(u =>
{
u.UseBackOffice();
u.UseWebsite();
})
.WithEndpoints(u =>
{
u.UseInstallerEndpoints();
u.UseBackOfficeEndpoints();
u.UseWebsiteEndpoints();
});
}
MiddleWareExtensions.cs
public static class MiddleWareExtensions
{
public static IApplicationBuilder CustomExceptionHandler(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ExceptionMiddlewareHandler>();
}
}
ExceptionMiddlewareHandler.cs
public class ExceptionMiddlewareHandler
{
private readonly RequestDelegate _next;
public ExceptionMiddlewareHandler(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var allowedcodes = new[] { 200, 301, 302, 401, 403, 404, 500 };
try
{
await _next(context);
if (!allowedcodes.Contains(context.Response.StatusCode))
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.Redirect("/error500");
}
}
catch (UnauthorizedAccessException)
{
context.Response.Clear();
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
context.Response.Redirect("/errorNoAccess");
}
catch (Exception ex)
{
if (context.Response.StatusCode is 404)
{
//may be from an exception in startup, so lets just let it get handled by the default 404;
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.Redirect("/error500");
}
}
}
}
Once that is done and working, you need to create the other error nodes using the same template as for the 404 page, you can then implement the changes to startup.cs for the custom handling
Yes I had not set up the custom 404 page in appsettings.json as I thought it would be handled by the custom logic you have implemented. I have added it now and 404 error handling seems to work. As I am new to this I have a couple of questions:
What is the need of the below code in Startup.cs if we are setting custom 404 page in appsettings.json?
Why is the below code in place? If I comment this code, the 404 and 500 error handling still seems to work. Can you please provide some more explanation?
if (context.Response.StatusCode == 404)
{
context.Request.Path = "/error404/";
await next();
}
if (context.Response.StatusCode is 401 or 403)
{
context.Request.Path = "/errorNoAccess/";
await next();
}
//This is a BadRequest so use the default handler (500)
if (context.Response.StatusCode == 400)
{
context.Request.Path = "/errorHandler/";
await next();
}
I had to add that because not all 404 errors were caught by Umbraco, IIRC invalid media urls did not get picked up by the umbraco 404 redirect
Similar reason to above, these errors may not get created by your code so don't get trapped by the exception handler it depends how the error/stuscode was raised.
Is there a way to redirect the user to a 404 page by throwing a 404 error and not actually redirecting them to a specific page? Our client has a tendency to rename pages so having them hard coded might lead to an incorrect 404 page
How to setup custom 403 page in Umbraco 10
Hi,
I am struggling to setup the custom 403 page for Umbraco 10 project. Custom 404 and Error page is working fine, but for 403 it's not working. I am pasting my code below.
I am using Backoffice restrictions, that's why I need to use 403 custom page.
Please suggest me to fix this.
In order to use app.UseStatusCodePagesWithReExecute you should create a controller that returns the correct view and use it like
Thanks Huw Reddick ,
I need to try like you mentioned above, will update here.
TBH, I didn't manage to get it to work that way :D
What I have done is created a CustomError Document type, using that I created several error pages, error404 (assigned as the umbraco 404), errorNoAcess and errorHandler.
These are just basic page templates with a title and message.
I discovered that depending how the 401/403 is raised/returned, you could get an Unauthorised exception or just a http statuscode, so you have to handle both :)
So, I create my own exceptionhandler midddleware to handle any exceptions and added some code to my startup.cs to handle the status codes
I'll post some code when I get back home later
This is the approach I am currently using, so far it seems to work for most things :D
Handling Errors in Umbraco 9+
Hi Huw Reddick,
I'm using Umbraco version 10. I want to be able to handle 404 and 500 errors. I followed your implementation from the blog https://umbraco.themediawizards.co.uk/the-grimoire/handling-errors-in-umbraco/, but its not working. This is what I have done so far:
Startup.cs
MiddleWareExtensions.cs
Can you please suggest what I'm doing wrong?
Hi Debasish,
What exactly is happening when you get a 404 or raise an exception?
Hi Huw Reddick,
When I enter this https://localhost:51434/testpage in the browser, I get the following
It is not taking me to my custom error page.
Hi Huw Reddick,
When I try to raise an exception(500 error), I'm presented with the below screen:
That sounds like you haven't setup a custom404 page for umbraco yet, you need to do that first.
https://our.umbraco.com/documentation/tutorials/Custom-Error-Pages/#create-a-404-page-in-the-backoffice
Once that is done and working, you need to create the other error nodes using the same template as for the 404 page, you can then implement the changes to startup.cs for the custom handling
Yes I had not set up the custom 404 page in appsettings.json as I thought it would be handled by the custom logic you have implemented. I have added it now and 404 error handling seems to work. As I am new to this I have a couple of questions:
What is the need of the below code in Startup.cs if we are setting custom 404 page in appsettings.json?
Why is the below code in place? If I comment this code, the 404 and 500 error handling still seems to work. Can you please provide some more explanation?
I had to add that because not all 404 errors were caught by Umbraco, IIRC invalid media urls did not get picked up by the umbraco 404 redirect
Similar reason to above, these errors may not get created by your code so don't get trapped by the exception handler it depends how the error/stuscode was raised.
Thank you Huw Reddick for the explanation. That makes sense :)
Is there a way to redirect the user to a 404 page by throwing a 404 error and not actually redirecting them to a specific page? Our client has a tendency to rename pages so having them hard coded might lead to an incorrect 404 page
is working on a reply...