Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For the first part:</p> <blockquote> <blockquote> <p>Also error messages returned to a client shouldn't contain those prefixes</p> </blockquote> </blockquote> <p>I agree having the parameter name as a prefix on all model state errors isn't great behavior. Fortunately, the service that has this behavior is replaceable. You just need to have a custom IBodyModelValidator. Here's what it would look like (using the Decorator pattern to let the default service do most of the work):</p> <pre><code>public class PrefixlessBodyModelValidator : IBodyModelValidator { private readonly IBodyModelValidator _innerValidator; public PrefixlessBodyModelValidator(IBodyModelValidator innerValidator) { if (innerValidator == null) { throw new ArgumentNullException("innerValidator"); } _innerValidator = innerValidator; } public bool Validate(object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, string keyPrefix) { // Remove the keyPrefix but otherwise let innerValidator do what it normally does. return _innerValidator.Validate(model, type, metadataProvider, actionContext, String.Empty); } } </code></pre> <p>Then, wrap the default service with yours:</p> <pre><code>config.Services.Replace(typeof(IBodyModelValidator), new PrefixlessBodyModelValidator(config.Services.GetBodyModelValidator())); </code></pre> <p>For the second part:</p> <blockquote> <blockquote> <p>elso replace "modelState" with "errors"</p> </blockquote> </blockquote> <p>The reason it currently says "modelState" is your current code:</p> <pre><code>return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); </code></pre> <p>Is effectively doing the following:</p> <pre><code>HttpError error = new HttpError(ModelState, false); return Request.CreateResponse(HttpStatusCode.BadRequest, error); </code></pre> <p>Since HttpError is being serialized, and it has a property named "ModelState", that's what you see in the response.</p> <p>If you want a different property name, you can use a custom error class:</p> <pre><code>public class PrettyHttpError { public PrettyHttpError(ModelStateDictionary modelState) { Message = "Your request is invalid."; Errors = new Dictionary&lt;string, IEnumerable&lt;string&gt;&gt;(); foreach (var item in modelState) { var itemErrors = new List&lt;string&gt;(); foreach (var childItem in item.Value.Errors) { itemErrors.Add(childItem.ErrorMessage); } Errors.Add(item.Key, itemErrors); } } public string Message { get; set; } public IDictionary&lt;string, IEnumerable&lt;string&gt;&gt; Errors { get; set; } } </code></pre> <p>And then create your response with this error type instead of HttpError:</p> <pre><code>PrettyHttpError error = new PrettyHttpError(ModelState); return Request.CreateResponse(HttpStatusCode.BadRequest, error); </code></pre> <p>The combination of PrettyHttpError and PrefixlessBodyModelValidator gives the output you requested.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload