C
C#3mo ago
nox7

ASP Core 8 - Custom 404 Handling

I have a few scenarios where I need to show a different kind of view and a bit of different logic depending on what type of authenticated user in my app experiences a "404" However, I don't understand how to capture the 404. I am using attribute routes and have the following middleware.
public class NotFoundMiddleware
{
private readonly RequestDelegate RequestDelegate;

public NotFoundMiddleware(RequestDelegate RequestDelegate)
{
this.RequestDelegate = RequestDelegate;
}

public async Task InvokeAsync(HttpContext context)
{
if (context.Response.StatusCode == StatusCodes.Status404NotFound)
{
context.Request.Path = "/client/errors/404";
}

await RequestDelegate(context);
}
}
public class NotFoundMiddleware
{
private readonly RequestDelegate RequestDelegate;

public NotFoundMiddleware(RequestDelegate RequestDelegate)
{
this.RequestDelegate = RequestDelegate;
}

public async Task InvokeAsync(HttpContext context)
{
if (context.Response.StatusCode == StatusCodes.Status404NotFound)
{
context.Request.Path = "/client/errors/404";
}

await RequestDelegate(context);
}
}
Setting a breakpoint with the debugger shows that providing a gibberish URL still is giving a status code of 200 - so my middleware doesn't handle it. How do I capture the a situation where there is no matching route or static file? I am trying to run code only when no route or static file was matched.
No description
24 Replies
nox7
nox73mo ago
Upon further investigation, it looks like HttpContext has an Endpoint property that is null if no matching route occurred. If I put my middleware at the end of a chain, unless anyone else has any further information - it seems this is how to tell if the request has no matching route and I can use this to assume it will 404.
No description
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
Yeah, I've read through that - unfortunately it doesn't do what I need here. It forces me to use their internal error handling. Or in the case of a custom IExceptionHandler - only when an exception is thrown. I need it to go to custom paths based on specific HttpContext data.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
UseStatusCodePagesWithReExecute is almost there, but it doesn't give me much controller in directing it to completely different routes Ah, we thought the same thing
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
I guess I could use it and just handle the logic I need within the Controller I redirect to (showing different views). So I'll go down that path
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
MVC/Razor
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
E.g., I am trying to show 3 different 404 views. One for authenticated clients, one for authenticated staff, and one for non-authenticated users
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
There are other situations I need the 404 page to be shown. E.g., when an object is missing from a request Url that has an Id in the parameter. So I wanted to have it all (all 404 scenarios) propagate up to the top and then the top handles it based on the currently authenticated user
nox7
nox73mo ago
app.UseStatusCodePagesWithReExecute("/errors/{0}");
app.UseStatusCodePagesWithReExecute("/errors/{0}");
No description
nox7
nox73mo ago
This works well and as I need it to, so thanks.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
Ah, I see what you mean. That's not a bad idea either.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
I don't use the normal identity system, but I get what you're saying.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
I don't use that system in my app
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nox7
nox73mo ago
Because two users can be logged in at one time I use a custom user system
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View