【问题标题】:ASMX JSON WebService, changing property types and having old __type - errorASMX JSON WebService,更改属性类型并具有旧的 __type - 错误
【发布时间】:2011-08-29 20:36:55
【问题描述】:

我有一个 .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 类中有 More 类型的属性 Info

在客户端 (HTML5) 我调用GetItems(),并将其存储在本地存储中,并定期调用SaveItem

我在客户端获得的 JSON 数据类似于:

[{"__type":"MyApp.API.Item","More":{"__type":"MyApp.API.Info","ID":1}}]

到目前为止一切都很好。

当我在Item 类中将属性More 从类型Info 更改为类型Details 时,会出现问题,即:

[Serializable]
public class Item 
{
    public Details More { get; set; } // <----- type changed from Info to Details
}

由于我的客户端缓存了数据,当我尝试调用SaveItem ASP.NET 时抛出错误:

 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; }
}

【问题讨论】:

    标签: c# web-services json asmx


    【解决方案1】:

    如果您不想看到“_type”:“object”,您可以将您的方法设置为object 类型。 所有这些文字都会消失。

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多