I migrated an API method from a standard MVC action to the new asp.net Web-API beta and suddenly a read only property is no longer serialized (both returning JSON). Is this expected behaviour?
我将API方法从标准MVC操作迁移到新的asp.net Web-API beta,突然只读取属性不再序列化(都返回JSON)。这是预期的行为吗?
Edit: Added code sample
编辑:添加代码示例
I have both Newtonsoft.Json 4.0.8 and System.Json 4.0 referenced through nuget packages
我有通过nuget包引用的Newtonsoft.Json 4.0.8和System.Json 4.0
public IQueryable<Car> Gets()
{
return _carRepository.GetCars();
}
public class Car
{
public IEnumerable<Photo> Photos
{
get { return _photos; }
}
public string PreviewImageUrl // No longer serialized
{
get
{
var mainImage = Photos.FirstOrDefault(o => o.IsMainPreview) Photos.FirstOrDefault();
return mainImage != null ? mainImage.Url : (string.Empty);
}
}
}
}
2 个解决方案
#1
10
The JsonMediaTypeFormatter
that ships with the Beta uses a serializer that does not support read-only properties (since they would not round-trip correctly). We are planning on addressing this for the next realese.
Beta附带的JsonMediaTypeFormatter使用不支持只读属性的序列化程序(因为它们不能正确往返)。我们正在计划为下一个问题解决这个问题。
In the mean-time you could use a custom JSON MediaTypeFormatter
implementation that uses Json.NET (there's one available here) instead of the built-in formatter.
在平均时间内,您可以使用自定义JSON MediaTypeFormatter实现,该实现使用Json.NET(这里有一个可用)而不是内置格式化程序。
Update: Also check out Henrik's blog about hooking up a JSON.NET formatter: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
更新:另请参阅Henrik的博客,了解如何连接JSON.NET格式化程序:http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-网络api.aspx
#2
4
I don't know if this is an 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. So I would say this is a bug for an output parameter. And here's an example illustrating the issue:
我不知道这是否是预期的行为。我会说这是输入参数(因为你不能设置它们的值)而不是输出参数。所以我想说这是输出参数的错误。这是一个说明问题的例子:
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();
}
}
Request:
api/products/5
Result:
{"Prop1":"prop1 value","Prop2":"prop2 value"}
So if the property doesn't have a public setter it is not serialized which doesn't seem normal as the Product
class is used as output in this case.
因此,如果属性没有公共setter,那么它不是序列化的,这似乎不正常,因为在这种情况下Product类被用作输出。
I would suggest opening a connect ticket so that Microsoft can fix this before the release or at least tell that this is by design.
我建议打开连接票,以便Microsoft可以在发布之前修复此问题,或者至少告诉它是设计的。
#1
10
The JsonMediaTypeFormatter
that ships with the Beta uses a serializer that does not support read-only properties (since they would not round-trip correctly). We are planning on addressing this for the next realese.
Beta附带的JsonMediaTypeFormatter使用不支持只读属性的序列化程序(因为它们不能正确往返)。我们正在计划为下一个问题解决这个问题。
In the mean-time you could use a custom JSON MediaTypeFormatter
implementation that uses Json.NET (there's one available here) instead of the built-in formatter.
在平均时间内,您可以使用自定义JSON MediaTypeFormatter实现,该实现使用Json.NET(这里有一个可用)而不是内置格式化程序。
Update: Also check out Henrik's blog about hooking up a JSON.NET formatter: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
更新:另请参阅Henrik的博客,了解如何连接JSON.NET格式化程序:http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-网络api.aspx
#2
4
I don't know if this is an 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. So I would say this is a bug for an output parameter. And here's an example illustrating the issue:
我不知道这是否是预期的行为。我会说这是输入参数(因为你不能设置它们的值)而不是输出参数。所以我想说这是输出参数的错误。这是一个说明问题的例子:
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();
}
}
Request:
api/products/5
Result:
{"Prop1":"prop1 value","Prop2":"prop2 value"}
So if the property doesn't have a public setter it is not serialized which doesn't seem normal as the Product
class is used as output in this case.
因此,如果属性没有公共setter,那么它不是序列化的,这似乎不正常,因为在这种情况下Product类被用作输出。
I would suggest opening a connect ticket so that Microsoft can fix this before the release or at least tell that this is by design.
我建议打开连接票,以便Microsoft可以在发布之前修复此问题,或者至少告诉它是设计的。