创建接受从参数类型派生的任何类型的参数的动作(多态参数)

时间:2015-05-12 02:05:30

标签: c# asp.net asp.net-mvc asp.net-web-api parametric-polymorphism

我想为客户端构建一个动态结构,以便在Web API中询问服务器。我曾尝试使用以下代码来处理我的问题,但是,它不起作用。

  
      
  1. 如何向服务
  2. 发送<travel>等通用类型   
  3. 如何更改服务器代码(或者所有需要更改客户端/服务器)?
  4.   

PS:如果您已将问题读到最后,请感谢您的耐心等待。

客户代码

var serializer = new JavaScriptSerializer();    
var product = new travel() { travel_desc = "select * from travel" };    
var jsonText = serializer.Serialize(product);    
var client = new HttpClient();    
client.BaseAddress = new Uri("http://localhost:65370/");     
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));    
StringContent content = new StringContent(jsonText, Encoding.UTF8, "application/json");     

var z = client.PostAsync<travel>("api/bb", product, new JsonMediaTypeFormatter()).Result;

服务器代码,无效

public IHttpActionResult Post< T > (Object x) where T : new()    
{    
                ........................    
}

顺便说一句,但我不知道如何发送&lt; T>到服务器

public IHttpActionResult Post(Object x)    
{    
                ........................    
}     

错误讯息
Client call server, server will be getting an error message " StatusCode: 404, ReasonPhrase: 'Not Found' "

 var z = client.PostAsync < travel > ("api/dd", product, new JsonMediaTypeFormatter()).Result; <--client

    public class ddController< T > : ApiController {public virtual void Post() {   ... }  } <---server    





  // sorry all , my English isn't very well , so I will try to use code to tell everyone how i want 
// in format situations,I will create 2 controller when I have 2 models(ex: users/product) , as following (client)
                var a = client.PostAsync("api/users", users, new JsonMediaTypeFormatter()).Result;
                var b = client.PostAsync("api/product", product, new JsonMediaTypeFormatter()).Result;

//and then when the users and product controllers was created the post code should be like as following (server)
                public IHttpActionResult Postusers(users travel) {}
                public IHttpActionResult Postproduct(product travel) {}

   //now i just want to create 1 controller for above  like as follwing 
                 var b = client.PostAsync<users/product>("api/all", product, new JsonMediaTypeFormatter()).Result;(client)

                 public IHttpActionResult Post<T>(Object ForAll) where T : new() {} (server)

1 个答案:

答案 0 :(得分:3)

JSON.NET,即Web API JSON序列化程序,能够在序列化对象时发送类型信息,并使用相同的信息对其进行反序列化。

它使用的技巧是将$type属性作为JSON对象的第一个属性。

如果要使用此技术,则需要具有基类或接口(例如ITravel),从中继承所有可能的类,并使用基类或接口作为参数类型,像这样:

public interface ITravel
{
  public int TravelId { get; set; }
}

public class TravelTypeA : ITravel
{
  public int TravelId { get; set; }
  public string Destination { get; set; }
}

public class TravelTypeB : ITravel
{
  ...
}

[HttpPost]
public object PostMeATravel(ITravel travel)
{
    // check what type is travel with "is" or ".GetType()"
}

当(de)序列化ITravel个对象时,您还需要指示JSON包含类型信息。 (JSON TypeName Handling):

JsonSerializerSettings serializerSettings 
  = GlobalConfiguration.Configuration.Formatters
    .JsonFormatter.SerializerSettings;

serializerSettings.TypeNameHandling = TypeNameHandling.Auto;

然后你必须发布一个带有typeInformation的JSON,如下所示:

{
  $type: 'SampleApp.TravelTypeA, SampleApp',
  TravelId: 22,
  Destination: 'La Almunia de Doña Godina'
}

当您这样做时,JSON.NET将使用类型信息来创建TravelTypeA对象,并将其作为参数传递给操作,该操作需要ITravel。如果需要,您可以在操作中检查所接收参数的类型,如下所示:if (travel.GetType().Name == "TravelTypeA") { ... }

请查看此Q&amp; A,了解有关如何执行此操作,工作原理以及此方法的优缺点的更多信息,以及另一种方法:Deserialising Json to derived types in Asp.Net Web API

注意:您可以使用优秀的Postman补充Chrome来测试Web API方法