I do not know if this is the expected behavior or not. I would say that this is expected for input parameters (because you cannot set their values), but not for output parameters. Therefore, I would say that this is an error for the output parameter. And here is an example illustrating the problem:
Model:
public class Product { public Product() { Prop1 = "prop1 value"; Prop2 = "prop2 value"; Prop3 = "prop3 value"; } public string Prop1 { get; set; } [ReadOnly(true)] public string Prop2 { get; set; } public string Prop3 { get; protected set; } }
Controller:
public class ProductsController : ApiController { public Product Get(int id) { return new Product(); } }
Inquiry:
api/products/5
Result:
{"Prop1":"prop1 value","Prop2":"prop2 value"}
So, if the property does not have a public setter, it does not serialize, which does not seem normal, since the Product
class is used as output in this case.
I would suggest opening a connection ticket so that Microsoft can fix it before the release, or at least say that it is by design.
source share