2

I am a newcomer in ASP.Net Web API world. Sorry if this is a foolish question.

I have the following api method -

[Route("api/v1/Location/Create")]
[HttpPost]
public IHttpActionResult Create(Location location)
{
    if (!ModelState.IsValid)
    {
       return StatusCode(HttpStatusCode.BadRequest);
    }

    return Ok();
}


public class Location
{
    public int MCC { get; set; }
    public int MNC { get; set; }
    public int LAC{ get; set; }
    public int CellId { get; set; }
}

If I send a string value from client, it still returns StatusCode 200.

What I am missing here?

3 Answers 3

2

You haven't put any data annotations on your location class. Try it adding [Required] data annotation one of property.

Sign up to request clarification or add additional context in comments.

Comments

1

Modify your class as follows-

using System.ComponentModel.DataAnnotations;

public class Location
{
    [Required()]
    public int MCC { get; set; }

    [Required()]
    public int MNC { get; set; }

    [Required()]
    public int LAC{ get; set; }

    [Required()]
    public int CellId { get; set; }
}

Comments

1

ModelState.IsValid is checking the data model validation when each filed is annotated by [Required].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.