我启动了一个Visual Studio WCF Rest服务应用程序项目,我希望有一个服务返回带有序列化对象的Json格式消息,并且我希望对象的某些字段不被序列化。我必须说,我对WCF的知识非常非常基础,并且我使用了Visual Studio中的Rest Service Application项目,因为它是实现我所需要的快速简单方法。ScriptIgnore无法在WCF REST服务应用程序上工作
比方说,这是我的目标:
public class BaseMessage
{
public string errorCode { get; set; }
public string errorMessage { get; set; }
}
这是我的代码
[WebGet(UriTemplate = "/v1/test",
ResponseFormat = WebMessageFormat.Json),OperationContract]
public BaseMessage Test()
{
return (new BaseMessage { errorCode = "000", errorMessage = "test" });
}
我读过到处说什么,我需要做的就是分配[ScriptIgnore]属性到我不想序列化的属性。但如果我这样做,它不起作用。无论如何,我都会将它们序列化。
我已经实现了我想要做的就是宣布我想以这种方式来返回对象的唯一方法:
[Serializable]
public class BaseMessage
{
public string errorCode;
[NonSerialized]
public string errorMessage;
}
这样我得到的序列化的类,因为我想要的。但是它让我感到很困惑,因为我可能不会这样做。我做对了吗?
嗯......如果我声明类是这样的: 'code' [DataContract] 公共类BaseMessage { [数据成员] 公共字符串的errorCode {获得;组; } public string errorMessage {get;组; } } 按预期序列化...但IgnoreDataMember不起作用。只有[DataMember]属性才能将该属性序列化。如果没有分配它不会被序列化...仍然这看起来不太适合我,但也许它是... – user1750251
格式化有点naff;但我认为,看起来确定 –
是肯定的;你也可以用[JsonIgnore] –