2

Web API でクライアントがサーバーに要求するための動的構造を構築したいと考えています。次のコードを使用して質問に対処しようとしましたが、機能しません。

  1. <travel>ジェネリック型のようなサービスを送信するにはどうすればよいですか
  2. サーバーコードを変更するにはどうすればよいですか (またはクライアント/サーバーをすべて変更する必要があります)?

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()    
{    
                ........................    
}

ところでそれは大丈夫ですが、サーバーに < 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)
4

1 に答える 1