Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Created Unassigned: WebApi validation ignores ErrorMessage in Data Annotation attributes [1471]

$
0
0
WebApi validation ignores custom Error Message in Data Annotation attributes, f.e. Required.

Given the code:

View Model:
```
public class InputVM : IValidatableObject
{
#region Properties

/// <summary>
/// Duration period
/// </summary>
[Required(ErrorMessage = "Custom duration required message")]
public int? Duration { get; set; }

(...)
}
```

Controller:
```
public class SampleApiController : ApiController
{
// GET /api/dummyoutput/
public HttpResponseMessage GetDummOutput(InputVM vm)
{

if(ModelState.IsValid == false)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
}

var service = new DummyDataService();
var vm = service.GetDummyOutput(vm);
return Request.CreateResponse<CalculatorVM>(HttpStatusCode.OK, vm);
}
```

When i miss the Duration field in query string, it will give me back JSON / XML with:
"The Duration property is required." instead of my custom error.

It's really hard to live with it with a localization requirement. :)

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>