webapi $ type customization

时间:2014-05-11 18:02:02

标签: asp.net-web-api json.net

我有一个用aspnet webapi编写的web api。主要思想是与继承合作。你在这里看到的是一个测试项目,在将其添加到真实项目之前玩arround。为了在webconfig文件上执行此操作,我添加了:

config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;

这是模型

 public abstract class Person
 {
   public string Name { get; set; }
   public string DocumentNumber { get; set; }
 }

 public class Teacher : Person
 {
    public string School { get; set; }
 }
 public class Employee : Person
 {
   public string PersonnelNumber { get; set; }
 }

结果,这是来自服务器的响应:

[
    {
        "$type": "JsonConverterTest.Models.Employee, JsonConverterTest",
        "PersonnelNumber": "1001",
        "Name": "Emp1",
        "DocumentNumber": "01"
    },
    {
        "$type": "JsonConverterTest.Models.Employee, JsonConverterTest",
        "PersonnelNumber": "1002",
        "Name": "Emp2",
        "DocumentNumber": "02"
    },
    {
        "$type": "JsonConverterTest.Models.Teacher, JsonConverterTest",
        "Name": "Teacher1",
        "DocumentNumber": "10"
    }
]

我需要$ type for web api deserization进程来为person创建正确的实例。

如何将$ type值更改为更简单的值:" Models.Employee"或" Models.Teacher"并摆脱集会名称?

提前致谢。

0 个答案:

没有答案