C
C#4w ago
Swyftey

✅ How can I filter for only JSON ModelState errors?

Hi, I want to use Model State to return a custom failure response when invalid JSON is provided (for the request body, for example). The issue is, I am not sure what the best way to do this is because the errors messages do not have an associated exception with them. What can I do so that my filter can successfully only return early on JSON errors? If it helps, I am using the Newtonsoft JSON package. Thanks for any help!
No description
No description
No description
5 Replies
jcotton42
jcotton424w ago
What does asp.net do by default for malformed JSON?
Swyftey
SwyfteyOP4w ago
I'm not quite sure if this is the correct answer, but I think it uses the ModelState and automatically returns the ModelState response if SuppressModelStateInvalidFilter is false. I have SuppressModelStateInvalidFilter set to true so I can use a custom response format with an action filter.
jcotton42
jcotton424w ago
Why do you want a custom message though? What about the default is unsuitable?
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
Swyftey
SwyfteyOP4w ago
I just wanted to stick with my custom message format which I am using for all API responses I see I’ll just leave it then, thanks

Did you find this page helpful?