[英]ASMX JSON WebService, changing property types and having old __type - error
我有一個.NET JSON Web服務
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
[GenerateScriptType(typeof(Item))]
[GenerateScriptType(typeof(Info))]
[GenerateScriptType(typeof(Details))]
public class API : System.Web.Services.WebService
{
[WebMethod]
public Item[] GetItems()
{
...
}
[WebMethod]
public bool SaveItem(Item item)
{
...
}
}
類的定義在消息的底部。
在Item
類中有一個Info
類型的More
屬性。
在客戶端(HTML5)上,我正在調用GetItems()
,並將其存儲在本地存儲中,並定期調用SaveItem
。
我從客戶端獲取的JSON數據如下所示:
[{"__type":"MyApp.API.Item","More":{"__type":"MyApp.API.Info","ID":1}}]
到目前為止一切都很好。
當我更改屬性時發生的問題 More
從類型Info
輸入Details
的Item
類,即:
[Serializable]
public class Item
{
public Details More { get; set; } // <----- type changed from Info to Details
}
由於我的客戶端緩存了數據,因此當我嘗試調用SaveItem
ASP.NET SaveItem
錯誤:
Cannot convert object of type 'MyApp.API.Info' to 'MyApp.API.Details'
問題 :是否有解決方案(最好保留ASMX Web服務,而不更改客戶端上的JSON數據)?
這是這些類:
[Serializable]
public class Item
{
public Info More { get; set; }
}
[Serializable]
public class Details: Info, IDetails
{
public string Notes { get; set; }
}
[Serializable]
public class Info: IInfo
{
public int ID { get; set; }
}
public interface IDetails: IInfo
{
string Notes { get; }
}
public interface IInfo
{
int ID { get; }
}
如果不想看到“ _type”:“ object”,則可以將方法作為object
類型。 所有這些文字都會消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.