title: Web Api 2: Custom parameter validation (typeconverter)
body: <p>In Web Api 2, a method on a controller can have a number of parameters supplied from the URI, e.g. string converted to int and automatic web error message returned if string could not be converted to int.</p>
  
  <p>I would like to do the same with a custom type. I have created a class that holds just a DateTime object and I want to make a custom conversion of the web URI string argument to this DateTime object. For this I implemented a TypeConverter. It works. My problem is that if I give it a bad date string, I get no web error and my method gets a null pointer. This is my model with the typeconverter:</p>
  
  <pre><code>[TypeConverter(typeof(DkDateTimeConverter))]
  public class DkDateTime
  {
      private static readonly ILog log4 = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  
      public DateTime dkDateTime;
  
      public static bool TryParse(string s, out DkDateTime result)
      {
          log4.Debug("TryParse");
          result = null;
  
          var culture = new CultureInfo("da");
          try
          {
              result = new DkDateTime()
              {
                  dkDateTime = DateTime.ParseExact(s, "dd-MM-yyyy HH:mm:ss", culture)
              };
              return true;
          }
          catch
          {
              return false;
          }
      }
  }
  
  class DkDateTimeConverter : TypeConverter
  {
      private static readonly ILog log4 = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  
      public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
      {
          log4.Debug("CanConvertFrom");
          if (sourceType == typeof(string))
          {
              return true;
          }
          return base.CanConvertFrom(context, sourceType);
      }
  
      public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
      {
          log4.Debug("ConvertFrom");
          if (value is string)
          {
              DkDateTime dkDateTime;
              if (DkDateTime.TryParse((string)value, out dkDateTime))
              {
                  return dkDateTime;
              }
              else
              {
                  log4.Debug("Exception");
                  throw new NotSupportedException();
              }
          }
          return base.ConvertFrom(context, culture, value);
      }
  }
  </code></pre>
  
  <p>My Controller has this method as entry point:</p>
  
  <pre><code>public IHttpActionResult Get(Guid messageid, int statuscode, DkDateTime sentdate, DkDateTime donedate)
  </code></pre>
  
  <p>I was under the impression that if ConvertFrom caused an exception, this would be reflected by web api and an web error message would be sent back to the caller. If I give "2a" as statuscode, I do get a web error like this:</p>
  
  <pre><code>&lt;Error&gt;
  &lt;Message&gt;The request is invalid.&lt;/Message&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>How do I trigger the same error message with my typeconverter? Perhaps a typeconverter cannot help, but which way should I then look?</p>