title: Returning Status Codes from a class outside my Controller
body: <p>I am trying to move logic that I do on all of my controllers into a class to follow the "Do Not Repeat Yourself" principle. What I am struggling with is how to elegantly return an error code.</p>
  
  <p>Here are some examples of what I currently do within each controller:</p>
  
  <pre><code>public class SomethingRequest
  {
      public SomethingModel Something { get; set; }
  
  
      public string Token { get; set; }
  }
  
  public ActionResult GetSomething(SomethingRequest request)
  {
  
      var something = request.Something;
  
      var token = request.Token;
  
      if (something == null)
      {        
          return BadRequest("Something object is null. You may have sent data incorrectly");
      }
  
      if (token == null || token != "1234")
      {
          return Unauthorized("Token object is null");
      }
  }
  
  </code></pre>
  
  <p>Now what I want to do is move the last two parts of that into their own class:</p>
  
  <pre><code>public class RequestValidation
  {
  
      public void TokenCheck(string token)
      {
          if (token == null || token != "1234")
          {
              // doesn't work
              return Unauthorized("Token object is null");
          }
      }
  
      public void DataCheck(object someObject)
      {
          if (someObject == null)
          {
              // doesn't work
              return BadRequest("Object is null. You may have sent data incorrectly");
          }
      }       
  }
  </code></pre>
  
  <p>And then I want to call them from SomethingController like so</p>
  
  <pre><code>RequestValidation.TokenCheck(token);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>RequestValidation.DataCheck(something);
  </code></pre>
  
  <p>and then have them return the bad request or an exception.</p>
  
  <p>How should I accomplish this?</p>