1

I am working in Asp.net web api, my model class is follwoing

[Serializable]    
public class Workspace
{
    private string strID;
    public string strName;

    public string ID
    {
        get { return strID; }
        set { strID = value; }
    }

    public string Name
    {
        get { return strName; }
        set { strName = value; }
    }

}

I have Following Get Method in controller

public Workspace GetWorkSpace()
    {
        if (objWorkspace == null)
        {             
            objWorkspace = new Workspace();
            objWorkspace.ID = Guid.NewGuid().ToString();
            objWorkspace.Name = "Workspace 1";

        }

        return objWorkspace;
    }

I am trying to get this object using Jquery as follows

var uri = 'api/workspace/GetWorkspace';
    $(document).ready(function () {
        $.getJSON(uri)
        .done(function (data) {

            $('#workspaceId').append(data.ID);
            $('#workspaceName').append(data.Name);

        });

    });

I get the response object in data, but it exclude my public properties "ID" and "Name" as shown below

strID: "a0a523c6-f657-40df-b957-114b87de02f8"
strName: "Workspace 1"
__proto__: Object

As you see response object get "strID, and strName" , which are private variables in model class, rather it should be ID and Name in reponse data, Moreover, if i remove [Serializable] attribute from my model class, i will get the desired result. Can any one explain me it, and what i do wrong with [Serializable] attribute

your help will be appreciatable

Ahmad
  • 13
  • 1
  • 5
  • you have **public string strName;** not private. – Matas Vaitkevicius May 22 '14 at 11:33
  • Is it your public method in your Controller? I'd suppose you have a method with `ActionResult` return type (even if not strictly required). "Proper" one should be `JsonResult` with `return Json(objWorkspace, JsonRequestBehavior.AllowGet);` – Adriano Repetti May 22 '14 at 11:36
  • I don't do much with .NET anymore, but could you try using the `ISerializable` interface and implementing the `GetObjectData` method to choose what will be serialized and how? – Pudge601 May 22 '14 at 11:40
  • ooops, sory, that actually private variable strName. – Ahmad May 26 '14 at 12:59

2 Answers2

0

I use a custom ContractResolver, with setting:

public MyResolver()
{
    IgnoreSerializableAttribute = true;
}

And as documented here DefaultContractResolver.IgnoreSerializableAttribute Property:

true if the SerializableAttribute attribute will be ignored when serializing and deserializing types; otherwise, false.

If you do not plan to implement custom Resolver you can do it like here:

The snippet cited from that source:

((DefaultContractResolver)config
     .Formatters
     .JsonFormatter
     .SerializerSettings
     .ContractResolver
).IgnoreSerializableAttribute = true;
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
0

Alternatively, you can resolve this problem by decorating the Workspace type with [JsonObject].

user2818985
  • 380
  • 3
  • 10