title: How to return data from Web Api controller?
body: <p>Hi I am developing restfull web api application. After inserting data into database i want to return 0 for success,1 for error and data as unique id assigned to the user. I want to return above data in json format. My requirement is i have to send data to controller in json format and receive data in json format.  I have following code and it is working but i want to ask few points here.
  Below is my User_Creation controller code.</p>
  
  <pre><code>public result Post(Noor_Users users)
          {
              result obj = new result();
                if (ModelState.IsValid)
                  {
                      entityObject.Noor_Users.Add(users);
                      int result = entityObject.SaveChanges();
                      if(result==1)
                      {
                          obj.success = 0;
                          obj.id = 5;
                          return obj;
                      }
                      else
                      {
                          obj.error = 1;
                          return obj;
                      }
                  }
                  else
                  {
                      obj.error = 1;
                      return obj;
                  }
              }
  }
  </code></pre>
  
  <p>My service.js file contains below code.</p>
  
  <pre><code>app.service("UserCreation", function ($http) {
      this.saveSubscriber = function (sub) {
          return $http({
              method: 'post',
              data: JSON.stringify(sub),
              url: 'api/User_Creation',
              contentType: "application/json"
          });
      }
  });
  </code></pre>
  
  <p>This is my controller.js code.</p>
  
  <pre><code>app.controller('UserCreation', function ($scope, UserCreation) {
  
      $scope.saveSubs = function () {
          var sub = {
              user_email: $scope.user_email,
              user_password: $scope.user_password,
          };
          var saveSubs = UserCreation.saveSubscriber(sub);
          saveSubs.then(function (data) {
              alert(JSON.stringify(data.data));
          }, function (error) {
              console.log('Oops! Something went wrong while saving the data.')
          })
      };
  });
  </code></pre>
  
  <p>I am expecting response in json format as below.</p>
  
  <p>●   status - 0 for success, 1 for failure.
  ●   data
      ○   id - unique id assigned to the user
  ●   error - error message if failed</p>
  
  <p>This is working absolutelt fine. I have below line of code in webapiconfig.cs file</p>
  
  <pre><code>config.Formatters.JsonFormatter.SupportedMediaTypes
      .Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
  </code></pre>
  
  <p>With this line of code always i can receive data in json format. But in angularjs success call i need to stringify recieved data. If all my data is returning in json then again why i should convert it to json? Also someone can tell me is above logic is a good practice to return data? Thank you.  </p>