Why does ModelState.IsValid fail for my ApiController method that has nullable parameters?

淺唱寂寞╮ 提交于 2019-12-18 03:59:09

问题


I have an ApiController method that accepts several parameters, like so:

    // POST api/files
    public HttpResponseMessage UploadFile
    (
        FileDto fileDto,
        int? existingFileId,
        bool linkFromExistingFile,
        Guid? previousTrackingId
    )
    {
        if (!ModelState.IsValid)
            return Request.CreateResponse(HttpStatusCode.BadRequest);

        ...
    }

When I POST to this I'm putting the FileDto object in the body of the request, and the other parameters on the query string.

I've already discovered that I cannot simply omit the nullable parameters - I need to put them on the query string with an empty value. So, my query looks like this when I don't want to specify a value for the nullable parameters:

http://myserver/api/files?existingFileId=&linkFromExistingFile=true&previousTrackingId=

This does match my controller method, and when the method is executed, the nullable parameters are indeed null (as you'd expect).

However, the call to ModelState.IsValid returns false, and when I examine the erorrs it's complaining about both the nullable parameters. (The other bits of the model have no errors). The message is:

A value is required but was not present in the request.

Why does it think that a value was required / not present? Surely (a) a value is not required for a nullable, and (b) a value was (sort of) present - in a null-ish sort of a way?


回答1:


In addtion to the first answer you should be able to get your code working allow the omitting of the prefix's on the url if you move all the optional's to the end of the method declaration and I always set them to NULL for good measure:

FileDto fileDto,
bool linkFromExistingFile,
Guid? previousTrackingId = null,
int? existingFileId = null

But

Good point re: an empty URL value with a prefix... is it the same as a NULL... Thinking about strings, Is ?q= an empty string or a null??

I have attempted to find the exact logic in the framework (and continue to look) that raises these errors but during my experimentation I did find that specifying a binder directly on a URL parameter seems to bypass the logic and allow an empty value after a prefix without a model binding error.

Like so:

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get(
        [FromUri(BinderType = typeof(TypeConverterModelBinder))] string q = null,
        [FromUri(BinderType = typeof(TypeConverterModelBinder))] int? value = null)
    {
        if (!ModelState.IsValid)
        {
            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }

        return new string[] { value.HasValue ? value.Value.ToString() : "", q };
    }     
}



回答2:


I solved this by moving all the parameters to a single class.

public class UploadFileModel {
   public FileDto FileDto { get; set; }
   public int? ExistingFileId { get; set; }
   public bool LinkFromExistingFile { get; set; }
   public Guid? PreviousTrackingId { get; set; }
}

public HttpResponseMessage UploadFile([FromUri]UploadFileModel model)
{
   // ...
}


来源:https://stackoverflow.com/questions/12006524/why-does-modelstate-isvalid-fail-for-my-apicontroller-method-that-has-nullable-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!