question

dkhetwal-5929 avatar image
0 Votes"
dkhetwal-5929 asked ·

How to suppress - SuppressModelStateInvalidFilter at action level, asp.net core webspi

This is regarding asp.net core web api. How to suppress model state validation at action level. we have [apicontroller] attribute defined at controller level, we want to keep auto model state validation for all the actions except one action method. How we can ignore auto model state validation for a specific action method?

dotnet-aspnetcore-webapi
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

miwan2-msft avatar image
0 Votes"
miwan2-msft answered ·

Hi, @dkhetwal-5929 ,

You could implement FilterAttribute to remove ModelStateInvalidFilter.

 [ApiController]
 public class PersonController
 {
     [SuppressModelStateInvalidFilter]
     public ActionResult<Person> Get() => new Person();
 }
    
 public class SuppressModelStateInvalidFilterAttribute : Attribute, IActionModelConvention
 {
     public void Apply(ActionModel action)
     {
         for (var i = 0; i < action.Filters.Count; i++)
         {
             if (action.Filters[i] is ModelStateInvalidFilter)
             {
                 action.Filters.RemoveAt(i);
                 break;
             }
         }
     }
 }


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,
Michael Wang





·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.