I have an Asp.Net core 3.1 Web Api project in witch I have a lot of controllers.
almost in every controller I have a template like this:
public class SomeController:ControllerBase
{
//injecting my service
[HttpGet]
[Route("")]
public async Task<ActionResult> SomeMethod()
{
try
{
//Do some work
return Ok(somevalue);
}
catch(ServiceException ex)
{
return BadRequest(ex.Message);
}
catch
{
return BadRequest();
}
}
}
this template is implemented in almost all the controllers.
I want to delete try...catch statements and write a base class or something to handle this try...catch statements outside my controller.
something like this I reckon:
public async Task<ActionResult> SomeMethod()
{
//Do some work
return Ok(somevalue);
//if any exception of ServiceException type occured i want to
//return BadRequest with message and if not a BadRequest without message
}
I have heard something about Cross-Cutting Concerns but I don't know how to use them.
Do you have any Idea how can I do it?