ASP.net Web API 2 ModelState always valid [duplicate]











up vote
1
down vote

favorite
1













This question already has an answer here:




  • ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

    2 answers



  • Model state validation in unit tests

    3 answers



  • ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

    3 answers




After reviewing several articles and SO questions, I still cannot understand why my validation attributes are not triggering.



For simplicity, I have reduced my model and added the string testing to debug.



using System;
using System.ComponentModel.DataAnnotations;

namespace PublicApi
{
public class CostStandardRequest
{
[DateLessThan("EndDate", ErrorMessage = "StartDate must be less than EndDate")]
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

[Required]
public string testing { get; set; }
}
}


In my controller, I test my method by passing a null value to the testing attribute in my CostStandardRequest class. However, ModelState.IsValid returns true despite null values.:



[HttpPost]
public async Task<ActionResult<string>> PostCostingStandard(CostStandardRequest request)
{
if (ModelState.IsValid)
{
// always valid
}
else
{
// never runs
}
}


I've reached this point by following the guide in the documentation



Note: ModelState.IsValid is true even when my custom validation attribute [DateLessThan] should be returning a ValidationResult(ErrorMessage). I have not included this code, but I can if it's deemed relevant.










share|improve this question













marked as duplicate by Igor c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 5 at 20:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • It's unclear to me. Are you testing the running code (ie. running it but with a test value you expect to fail)? Or are you executing a Unit Test and have created an instance of this controller and passed in a null value to the method?
    – Igor
    Nov 5 at 19:50












  • How did you implemented DateLessThan? That is a custom attribute. Can you post the code for that class? Did read this page msdn.microsoft.com/en-us/library/cc668224.aspx?
    – Alfredo A.
    Nov 5 at 19:54






  • 2




    @BenGraham - Then this is expected because the model validates as a part of the pipeline which you are bypassing in the unit test. This is a good thing because of SoC (separation of concerns) so you are not required to test your model inside the controller (the 2 can be isolated).
    – Igor
    Nov 5 at 20:01








  • 1




    You are mixing two concepts here. The controller is not responsible for the validation. The pipeline is, this answer will explain it in much more detail stackoverflow.com/a/22563585/3973463
    – Alfredo A.
    Nov 5 at 20:06








  • 1




    @BenGraham - Good deal. I found some existing questions (you are/were not the only one with this question) and marked yours as a duplicate of those. They have much better write ups than I could think of at the moment so it is more helpful that way (IMO).
    – Igor
    Nov 5 at 20:08















up vote
1
down vote

favorite
1













This question already has an answer here:




  • ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

    2 answers



  • Model state validation in unit tests

    3 answers



  • ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

    3 answers




After reviewing several articles and SO questions, I still cannot understand why my validation attributes are not triggering.



For simplicity, I have reduced my model and added the string testing to debug.



using System;
using System.ComponentModel.DataAnnotations;

namespace PublicApi
{
public class CostStandardRequest
{
[DateLessThan("EndDate", ErrorMessage = "StartDate must be less than EndDate")]
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

[Required]
public string testing { get; set; }
}
}


In my controller, I test my method by passing a null value to the testing attribute in my CostStandardRequest class. However, ModelState.IsValid returns true despite null values.:



[HttpPost]
public async Task<ActionResult<string>> PostCostingStandard(CostStandardRequest request)
{
if (ModelState.IsValid)
{
// always valid
}
else
{
// never runs
}
}


I've reached this point by following the guide in the documentation



Note: ModelState.IsValid is true even when my custom validation attribute [DateLessThan] should be returning a ValidationResult(ErrorMessage). I have not included this code, but I can if it's deemed relevant.










share|improve this question













marked as duplicate by Igor c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 5 at 20:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • It's unclear to me. Are you testing the running code (ie. running it but with a test value you expect to fail)? Or are you executing a Unit Test and have created an instance of this controller and passed in a null value to the method?
    – Igor
    Nov 5 at 19:50












  • How did you implemented DateLessThan? That is a custom attribute. Can you post the code for that class? Did read this page msdn.microsoft.com/en-us/library/cc668224.aspx?
    – Alfredo A.
    Nov 5 at 19:54






  • 2




    @BenGraham - Then this is expected because the model validates as a part of the pipeline which you are bypassing in the unit test. This is a good thing because of SoC (separation of concerns) so you are not required to test your model inside the controller (the 2 can be isolated).
    – Igor
    Nov 5 at 20:01








  • 1




    You are mixing two concepts here. The controller is not responsible for the validation. The pipeline is, this answer will explain it in much more detail stackoverflow.com/a/22563585/3973463
    – Alfredo A.
    Nov 5 at 20:06








  • 1




    @BenGraham - Good deal. I found some existing questions (you are/were not the only one with this question) and marked yours as a duplicate of those. They have much better write ups than I could think of at the moment so it is more helpful that way (IMO).
    – Igor
    Nov 5 at 20:08













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1






This question already has an answer here:




  • ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

    2 answers



  • Model state validation in unit tests

    3 answers



  • ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

    3 answers




After reviewing several articles and SO questions, I still cannot understand why my validation attributes are not triggering.



For simplicity, I have reduced my model and added the string testing to debug.



using System;
using System.ComponentModel.DataAnnotations;

namespace PublicApi
{
public class CostStandardRequest
{
[DateLessThan("EndDate", ErrorMessage = "StartDate must be less than EndDate")]
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

[Required]
public string testing { get; set; }
}
}


In my controller, I test my method by passing a null value to the testing attribute in my CostStandardRequest class. However, ModelState.IsValid returns true despite null values.:



[HttpPost]
public async Task<ActionResult<string>> PostCostingStandard(CostStandardRequest request)
{
if (ModelState.IsValid)
{
// always valid
}
else
{
// never runs
}
}


I've reached this point by following the guide in the documentation



Note: ModelState.IsValid is true even when my custom validation attribute [DateLessThan] should be returning a ValidationResult(ErrorMessage). I have not included this code, but I can if it's deemed relevant.










share|improve this question














This question already has an answer here:




  • ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

    2 answers



  • Model state validation in unit tests

    3 answers



  • ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

    3 answers




After reviewing several articles and SO questions, I still cannot understand why my validation attributes are not triggering.



For simplicity, I have reduced my model and added the string testing to debug.



using System;
using System.ComponentModel.DataAnnotations;

namespace PublicApi
{
public class CostStandardRequest
{
[DateLessThan("EndDate", ErrorMessage = "StartDate must be less than EndDate")]
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }

[Required]
public string testing { get; set; }
}
}


In my controller, I test my method by passing a null value to the testing attribute in my CostStandardRequest class. However, ModelState.IsValid returns true despite null values.:



[HttpPost]
public async Task<ActionResult<string>> PostCostingStandard(CostStandardRequest request)
{
if (ModelState.IsValid)
{
// always valid
}
else
{
// never runs
}
}


I've reached this point by following the guide in the documentation



Note: ModelState.IsValid is true even when my custom validation attribute [DateLessThan] should be returning a ValidationResult(ErrorMessage). I have not included this code, but I can if it's deemed relevant.





This question already has an answer here:




  • ASP.NET MVC Controller post method unit test: ModelState.IsValid always true

    2 answers



  • Model state validation in unit tests

    3 answers



  • ModelState.IsValid always true when testing Controller in Asp.Net MVC Web Api

    3 answers








c# asp.net-mvc validation asp.net-web-api2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 5 at 19:39









Ben

1291111




1291111




marked as duplicate by Igor c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 5 at 20:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Igor c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 5 at 20:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • It's unclear to me. Are you testing the running code (ie. running it but with a test value you expect to fail)? Or are you executing a Unit Test and have created an instance of this controller and passed in a null value to the method?
    – Igor
    Nov 5 at 19:50












  • How did you implemented DateLessThan? That is a custom attribute. Can you post the code for that class? Did read this page msdn.microsoft.com/en-us/library/cc668224.aspx?
    – Alfredo A.
    Nov 5 at 19:54






  • 2




    @BenGraham - Then this is expected because the model validates as a part of the pipeline which you are bypassing in the unit test. This is a good thing because of SoC (separation of concerns) so you are not required to test your model inside the controller (the 2 can be isolated).
    – Igor
    Nov 5 at 20:01








  • 1




    You are mixing two concepts here. The controller is not responsible for the validation. The pipeline is, this answer will explain it in much more detail stackoverflow.com/a/22563585/3973463
    – Alfredo A.
    Nov 5 at 20:06








  • 1




    @BenGraham - Good deal. I found some existing questions (you are/were not the only one with this question) and marked yours as a duplicate of those. They have much better write ups than I could think of at the moment so it is more helpful that way (IMO).
    – Igor
    Nov 5 at 20:08


















  • It's unclear to me. Are you testing the running code (ie. running it but with a test value you expect to fail)? Or are you executing a Unit Test and have created an instance of this controller and passed in a null value to the method?
    – Igor
    Nov 5 at 19:50












  • How did you implemented DateLessThan? That is a custom attribute. Can you post the code for that class? Did read this page msdn.microsoft.com/en-us/library/cc668224.aspx?
    – Alfredo A.
    Nov 5 at 19:54






  • 2




    @BenGraham - Then this is expected because the model validates as a part of the pipeline which you are bypassing in the unit test. This is a good thing because of SoC (separation of concerns) so you are not required to test your model inside the controller (the 2 can be isolated).
    – Igor
    Nov 5 at 20:01








  • 1




    You are mixing two concepts here. The controller is not responsible for the validation. The pipeline is, this answer will explain it in much more detail stackoverflow.com/a/22563585/3973463
    – Alfredo A.
    Nov 5 at 20:06








  • 1




    @BenGraham - Good deal. I found some existing questions (you are/were not the only one with this question) and marked yours as a duplicate of those. They have much better write ups than I could think of at the moment so it is more helpful that way (IMO).
    – Igor
    Nov 5 at 20:08
















It's unclear to me. Are you testing the running code (ie. running it but with a test value you expect to fail)? Or are you executing a Unit Test and have created an instance of this controller and passed in a null value to the method?
– Igor
Nov 5 at 19:50






It's unclear to me. Are you testing the running code (ie. running it but with a test value you expect to fail)? Or are you executing a Unit Test and have created an instance of this controller and passed in a null value to the method?
– Igor
Nov 5 at 19:50














How did you implemented DateLessThan? That is a custom attribute. Can you post the code for that class? Did read this page msdn.microsoft.com/en-us/library/cc668224.aspx?
– Alfredo A.
Nov 5 at 19:54




How did you implemented DateLessThan? That is a custom attribute. Can you post the code for that class? Did read this page msdn.microsoft.com/en-us/library/cc668224.aspx?
– Alfredo A.
Nov 5 at 19:54




2




2




@BenGraham - Then this is expected because the model validates as a part of the pipeline which you are bypassing in the unit test. This is a good thing because of SoC (separation of concerns) so you are not required to test your model inside the controller (the 2 can be isolated).
– Igor
Nov 5 at 20:01






@BenGraham - Then this is expected because the model validates as a part of the pipeline which you are bypassing in the unit test. This is a good thing because of SoC (separation of concerns) so you are not required to test your model inside the controller (the 2 can be isolated).
– Igor
Nov 5 at 20:01






1




1




You are mixing two concepts here. The controller is not responsible for the validation. The pipeline is, this answer will explain it in much more detail stackoverflow.com/a/22563585/3973463
– Alfredo A.
Nov 5 at 20:06






You are mixing two concepts here. The controller is not responsible for the validation. The pipeline is, this answer will explain it in much more detail stackoverflow.com/a/22563585/3973463
– Alfredo A.
Nov 5 at 20:06






1




1




@BenGraham - Good deal. I found some existing questions (you are/were not the only one with this question) and marked yours as a duplicate of those. They have much better write ups than I could think of at the moment so it is more helpful that way (IMO).
– Igor
Nov 5 at 20:08




@BenGraham - Good deal. I found some existing questions (you are/were not the only one with this question) and marked yours as a duplicate of those. They have much better write ups than I could think of at the moment so it is more helpful that way (IMO).
– Igor
Nov 5 at 20:08

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong