Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi I'm using umbraco web api. I have a model
public class MyModel { public string Name1 { get; set; } [Required] [Range(1,12, ErrorMessage ="Invalid Month")] public int myMonth { get; set; } [Required] [Range(00, 99, ErrorMessage = "Invalid Year")] public int myYear { get; set; } }
and in the controller
[HttpPost] public HttpResponseMessage TestMyModel(HttpRequestMessage request) { var msg = new HttpResponseMessage(); XmlSerializer deserializer = new XmlSerializer(typeof(MyModel)); try { var reader = new StringReader(request.Content.ReadAsStringAsync().Result); var mymodel = (MyModel)deserializer.Deserialize(reader); if (ModelState.IsValid) { msg.StatusCode = HttpStatusCode.OK; } else { msg.StatusCode = HttpStatusCode.BadRequest; } } catch (Exception exc) { msg.StatusCode = HttpStatusCode.BadRequest; } return msg; }
And none of the validation is working. Please advice
Hi Sonja
The validation works only if you pass this param to the method, instead of "HttpRequestMessage request" use "MyModel model" then validation will work
Alex
Thanks but when I was passing MyModel instead of HttpRequestMessage I was getting null in the request.
I've managed to solve the problem, I put
Validate<MyModel>(myModel); if (ModelState.IsValid) { msg.StatusCode = HttpStatusCode.OK; } else { msg.StatusCode = HttpStatusCode.BadRequest; }
Hello Sonja
Thanks for sharing! Have a great day!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Umbraco web api validation doesn't work
Hi I'm using umbraco web api. I have a model
and in the controller
And none of the validation is working. Please advice
Hi Sonja
The validation works only if you pass this param to the method, instead of "HttpRequestMessage request" use "MyModel model" then validation will work
Alex
Thanks but when I was passing MyModel instead of HttpRequestMessage I was getting null in the request.
I've managed to solve the problem, I put
Hello Sonja
Thanks for sharing! Have a great day!
Alex
is working on a reply...