I use Metadata and JsonIgnore to remove special field from being serializing.
[Authorize(Roles = "admin")]
public class UserController : ApiController
{
public IEnumerable<user> Get()
{
using (var mydb = new ModelContainer())
{
return mydb.userSet.ToList();
}
}
}
[MetadataType(typeof(user_Metadata))]
public partial class user
{
private class user_Metadata
{
[JsonIgnore]
public virtual password { get; set; }
public virtual adminFile { get; set; }
}
}
How can I dynamic control which field should be serialized or not. For some thing like
public partial class user
{
private class user_Metadata
{
[JsonIgnore]
public virtual password { get; set; }
[Roes == admin?JsonIgnore:JsonNotIgnore] //some thing like this
public virtual adminFile { get; set; }
}
}
Conditional property serialization
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