2013-07-22 35 views
0

我想在ASP.Net上實現一個REST風格的Web API。 爲了測試這個Web API,我創建了一個使用HttpClient.PostAsync的小型客戶端應用程序。 我在HttpContent對象中添加了一些參數,但無論我嘗試什麼,我都無法在Web API中的服務器端找到這些張貼的參數。如何檢索服務器端發佈的參數?

代碼在客戶端:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
var customer = new Customer() { FirstName = "test", LastName = "test" }; 

MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); 
HttpContent content = new ObjectContent<Customer>(customer, jsonFormatter); 

HttpResponseMessage response = await client.PostAsync(base_url, content); 

在服務器端代碼:

string httpMethod = Request.HttpMethod; 

if (httpMethod == "POST") 
{ 
    string firstName = Request.QueryString["FirstName"]; 
    string lastName = Request.QueryString["LastName"]; 
} 

如果我設置在服務器端斷點我看到Request.AcceptTypes等於「應用/ JSON」 ,所以可能在服務器端收到格式類型。 但是,Request.QueryString始終爲空,我不知道如何檢索張貼的參數...

任何人都可以幫我嗎? 在此先感謝!

回答

相關問題