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.
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.
24 Replies
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.Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
UseStatusCodePagesWithReExecute
is almost there, but it doesn't give me much controller in directing it to completely different routes
Ah, we thought the same thingUnknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
MVC/Razor
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
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•8mo ago
Message Not Public
Sign In & Join Server To View
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
This works well and as I need it to, so thanks.
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Ah, I see what you mean. That's not a bad idea either.
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
I don't use the normal identity system, but I get what you're saying.
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
I don't use that system in my app
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
Because two users can be logged in at one time
I use a custom user system
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View