how to handle exception with customized error message in json format in webapi in c#

Pradyut Sinha 21 Reputation points
2023-01-28T12:49:01.5266667+00:00

how to handle exception with customized error message in json format in webapi in c# also my webapi calling different one drive api like delete file , download file etc and my webapi return type like IActionResult and jsonstring

Any body can help

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
294 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,201 Reputation points
    2023-01-28T19:12:47.83+00:00

    customized error format will be Json format.

    Of course the response is JSON. JSON is standard in Web API these days.

    Your original post asked for a "customized error message" but you did not provide an example of the JSON message. Is your expectation that the community will come up with a custom design? Then explain how the design works? That's a large ask for a support forum.

    Moreover, REST error responses follow a standard and Web API provides these response out of the box. Why can't you use the responses that come with Web API? I get the feeling that you did not go through the linked tutorial which covers these basics.

    At this point, it is not clear exactly what you want.

    The following snippet is a ASP.NET Web API bad request response. BadRequest() has three overloads and one of the overloads is a string. You get to add what ever string you like. Is this what you're looking for???

    public IHttpActionResult Get()
    {
        return BadRequest("Houston, we have a problem!");
    }
    
    0 comments No comments

  2. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2023-01-29T10:10:07.7566667+00:00

    Hi @Pradyut Sinha , You can create a custom Exception Filter like below:

    public class CustomExceptionFilterAttribute : ExceptionFilterAttribute { 
      public override void OnException(ExceptionContext context) 
       { 
           var exception = context.Exception; 
           context.Result = new JsonResult(exception.Message); 
       } }
    

    Then apply the above attribute to your controller.

    [Route("api/[controller]")] 
    [CustomExceptionFilter] 
    public class ValuesController : Controller 
    { // GET: api/values 
     [HttpGet]
     public IEnumerable<string> Get() 
    { throw new Exception("Suckers"); 
    return new string[] { "value1", "value2" }; } }
    

    For details, you can view the following documents.

    Exception Handling in ASP.NET Web API

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments