2

Trying to exclude properties from a model from being included during serialization.

I am using the following syntax:

JsConfig<MyTestClass>.ExcludePropertyNames = new[] { "ShortDescription" };

Just after that I have the following:

            return     (from o in __someProvider.GetAll() select (new
                      {
                          o.Name,
                          o.ShortDescription
                          o.InsertDate
                      }).TranslateTo<MyTestClass>()).ToList()

However once result is returned from the method, it still contains "ShortDescription" field in the Json. Am I doing something wrong?

4

1 回答 1

7

JsConfig<T>.ExcludePropertyNames appears to be checked only once for each type, in a static constructor for TypeConfig<T>. Thus, if you are configuring ExcludePropertyNames in your service class, just before returning your response, it might be too late -- the TypeConfig properties may already be set up and cached for MyTestClass. I was able to reproduce this.

A more reliable alternative is to move all of your JsConfig<T> configuration to your AppHost setup code.

If you really do need to do this in your service class, e.g. if you are only conditionally excluding property names, then an alternative approach would be to ensure that JsConfig.IncludeNullValues is false (I believe it is by default) and in your service code set ShortDescription to null when appropriate.

于 2013-08-19T14:02:58.940 回答