I use asp.net core
but principle is the same. Also, not saying this is the best way, but for me this is the setup that works pretty nice.
I always use IActionResult
as its most flexible for me. I can then have action methods completely unaware of DTO
s that my services (which action methods call) return. All i do is, more or less, something like this:
[CustomExceptionsFilter]
[CustomValidateModelFilter]
[Authorize(Roles="whatever")]
public IActionResult ActionMethod(params){
return Ok(this._myService.whatever());
}
This way, if you change DTO
that your service returns (which happens alot, especially in early development phases), i don' thave to touch my controller at all.
Also, i have pretty much unified way of returning things where my custom exception error filter catches all service layer custom exceptions like validation exception, operation exception, etc...
[UPDATE]
in filters you don't actually return in a traditional sense. You rather set your context.result
(which is of type IActionResult
). So, lets say my service throws my custom MyServiceOperationException("You cannot do that")
exception.
What i do in my exception filter is:
public override void OnException(ExceptionContext context)
{
if (context.Exception is MyServiceOperationException)
{
context.Result = new BadRequestObjectResult(new MyErrorResult(){
Message = context.Exception.Message,
Code=context.Exception.MyErrorCode }
);
}
}