question

dkhetwal-5929 avatar image
0 Votes"
dkhetwal-5929 asked PaoloFornari-6312 commented

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-aspnet-core-general
5 |1600 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
1 Vote"
miwan2-msft answered PaoloFornari-6312 commented

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





· 1
5 |1600 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.

Hi @miwan2-msft ,
I'm on .net 5 and your solution is not working.

The type of the action filter is not ModelStateInvalidFilter but it's ModelStateInvalidFilterFactory.

I've slightly modified your code (I don't know if your solution works on .net core < 5):


 public class SuppressModelStateInvalidFilterAttribute : Attribute, IActionModelConvention
     {
    
         private const string FilterTypeName = "ModelStateInvalidFilterFactory";
    
            
         public void Apply(ActionModel action)
         {
    
             for (var i = 0; i < action.Filters.Count; i++)
             {
                 //if (action.Filters[i] is ModelStateInvalidFilter)
                 if (action.Filters[i].GetType().Name == FilterTypeName)
                 {
                     action.Filters.RemoveAt(i);
                     break;
                 }
             }
        }
     }


Since ModelStateInvalidFilterFactory is internal I cannot use 'is' to test the filter.
Do you have a more 'strong' solution ?

One more question... is it possible to pass an ILogger to this class to log something if the filter is not found ?


Thanks
Paolo

0 Votes 0 ·