I'm refactoring an MVC controller into smaller pieces in different classes, and I've come to a part where I'm returning a "return StatusCode(500,"Some message")" item.
It seems as though I'm not able to do this outside of a controller as far as I can tell.
Is this possible, or must I find an alternative way to return a response, and convert it to the appropriate type back in the calling code (The MVC controller)?
A problem I can see myself having, is that the original type of the MVC action is IActionResult, so within the action I've been returning whatever is the appropriate response, sometimes view(,model), sometimes Json() and other times StatusCode(<500>,), i.e. my responses aren't consistent.
Is there a natural way for me to just pickup and move this code into another class to make it work, or will I have to hand craft my own object to represent what I would otherwise and returned in the controller, and if so, what is recommended?
In short, ideally I want to be able to return an action result such as "return View("my view name", myModel) in a class outside of a controller. How can I achieve this?
It feels quite dirty, but perhaps a single object containing a statuscode, message, object (to serve as the model), view name and a new enum called ActionResultType, with a value for "StatusCode", "Json" and "View", so I can reasonably easily map it back to it's appropriate natural state when the controller receives the response.
Perhaps I'm going a bit far and instead should just focus on getting the main parts of the logic out of the controller, rather than trying to build a more fleshed out response somewhere else?
If possible, I'd love to get each action result to about 10 lines, but unless I can figure out a nice way to return the appropriate response from somewhere else, it will probably be a struggle.