1

I have action method defined as

public HttpResponseMessage Get(SomeEnum? param)
{
   ...
}

If I pass some invalid value for param which is not convertible to Some Enum type I get this message:

The value 'xxx' is not valid for Nullable`1.

It is default message which I can get from ModelState. I would like to customize this message. I have found plenty of tips how to do it in ASP.NET MVC (like here) but nothing for WebAPI. Changing DefaultModelBinder.ResourceClassKey does not work in WebAPI. I have also tried to solve problem by customizing ParameterBindingRule:

config.ParameterBindingRules.Insert(0, parameter =>
{
   if (!typeof (EnumType?).IsAssignableFrom(parameter.ParameterType))
      return parameter.BindAsError("Error message");

    return null;
});

Unfortunately this also doesn't work.

3
  • You can create a custom ExceptionFilter to achieve your porpose. Commented Nov 6, 2014 at 12:58
  • @AnkushJain I have defined global exception filter but it is not reached in this case. I think I have to create some kind of custom binder or validator. Commented Nov 6, 2014 at 13:49
  • it will work. May be you have not followed proper rules to implement it. Read this carefully asp.net/web-api/overview/error-handling/… Commented Nov 6, 2014 at 18:50

1 Answer 1

1

Here is the solution:

// in Application_Start 
ModelBinderConfig.TypeConversionErrorMessageProvider = (context, metadata, value) =>
{
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.