Auth Controller Structure & Error Handling
I am trying to replace the old login/register controllers in my asp.net web api portfolio project because most of it consists of "if" statements with very little separation of concerns.
I'm asking claude/chatgpt for some ideas for me to learn from to make a professional looking controller, and it's showing me this ExecuteAuthFlow method in a base controller (top) for error handling, and an AuthService for things such as user validation in the login controller (bottom) that inherits from the base controller.
I'm wondering if this is the right direction for my portfolio project with how an auth system should be made?

5 Replies
That seems way overcomplicated
Middleware should be handling exceptions
I'm just at situation where I don't know what I don't know, so just trying to figure out how I should build my controllers for error handling and structure. 😄
You should not, when it comes to exceptions
Let them bubble up and be caught by the middleware
Handle them there, if needed
If not, just return
ProblemDetails
from that middlewareI have never worked on middleware as far as I am aware, I don't know how I would do this with middlewhere, got any advice or know where I can figure it out?
Many of the ways of handling errors are described here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-9.0
When it comes to APIs, check this: https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-9.0
And this post talks specifically about using a middleware: https://dev.to/andytechdev/aspnet-core-middleware-working-with-global-exception-handling-3hi0