asp.net mvc 4 - How to hide a Model class field based on custom logic in MVC Web Api RC -


I am using ASP.Net Mvc web API RC

I use custom attribute Taxes wanted to hide the field / properties of their model class. Below is my class:

  public class employee {public in EmpId {get; Set; } Public string name {get; Set; } // wanted to hide this feature based on the custom logic. For a certain role, I want to hide the designation public string designation {get; Set; } Public string section {get; Set; }}   

How we can use data annotations. I mean, I want to create a different characteristic to use in this manner:

  [HideForRoles (roles = "admin, author")] Public string designation {get; Set; } Update:   

As I am developing Web API, the answer is either sorted in XML or Jason format. Depending on the formatter, the better question is, do not allow the field to be categorized when writing the response.

However an option IgnoreDataMember attribute can be used such as

  [anonymousDetamel] receive public string designation { Set; }   

But above is a compiled time announcement, where I can not apply any condition.

Question: How can I avoid field / property while playing serial based on certain condition on runtime?

It was completely missed in the first round that you are using web ap, my apology Were.

What you want to do, to create custom formatters

There is a good article here on the flow / differences between MVC and Web API (which I think you already have However, here are some valid points:)

And here's a sample implementation of custom formats:

From that building, you can use reflection to read from the attributes You will write on the custom action filter attribute, The An entitles you evaluate the user's roles and determine which is a sample of the field should be omitted / included here an action filter:

hope that more help.

Cheers.

Comments