Add Json response in C#.net API for all scenarios

Add Json response in C#.net API for all scenarios

How can I return a Json response ALWAYS, it doesn't matter if the HTTP Code needs to be 200, 401, or another one.

How can I return a Json response ALWAYS, it doesn't matter if the HTTP Code needs to be 200, 401, or another one.

For the HTTP 200 the Json is sent with return Ok(response); //where response is a model. For HTTP 401 (Unauthorized) I have the following:

throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized)
                {
                    Content = new StringContent(msg),
                    ReasonPhrase = msg
                });

But msg needs to be an String... I cannot make it work with a model to return a JSON.

The same happens with BadRequest or other HTTP status.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Add asp.net Identity in empty project

LIKE | COMMENT | SHARE | SUBSCRIBE Today, in this tutorial you will see the process of adding an ASP.NET Identity to the ASP.NET Web Application developed in...

List all users from asp net core identity

LIKE | COMMENT | SHARE | SUBSCRIBE In this video, i will show you How to retrieve and display all the registered application users in asp.net core using the ...

Extend IdentityUser in ASP NET Core

LIKE | COMMENT | SHARE | SUBSCRIBE In this tutorials, I will show you how to extend IdentityUser in asp.net core.. Subscribe & Stay Connected! Thank You! ♥ #...