I have a web-API project and a simple class with a few properties, some are marked <JsonIgnore>
.
In my MVC-controller I put Return Json(instanceOfMyClass, JsonRequestBehavior.AllowGet)
. All members are serialized.
I put Return Json(Of MyClass)(instanceOfMyClass)
in my WEBAPI-controller. Only the members I intend to serialize are present.
How can I ignore these properties independent of the controller that's going to serialize.
The JsonResult
in MVC does not actually use JSON.NET which is why [JsonIgnore]
is not working. Instead it uses the JavaScriptSerializer
class.
To make the JavaScriptSerializer
skip a property, you can you the [ScriptIgnore]
attribute on your model property.
An alternative would be to make a custom ActionResult
that uses JSON.NET's JsonConvert
to serialize the object which would then honor the [JsonIgnore]
attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With