2014-05-01 41 views
0

嘗試使用Request.Form獲取已發佈的有效內容,但Request.Form始終{}爲空?Request.Form空郵件數據

這裏是我的有效載荷:

chromepayload

但是當我嘗試和使用:

var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url); 

我載荷的Request.Form是空的。

整個C#代碼:

public ActionResult Index(string pathInfo) 
{ 
    var url = Settings.GetValue<string>("QualService") + "/" + pathInfo + "?" + Request.QueryString; 

    //Get stuff from the back end 
    using (var client = new WebClient()) 
    { 
     client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
     client.Headers[HttpRequestHeader.Cookie] = Request.Headers["Cookie"]; 
     client.Headers[HttpRequestHeader.Authorization] = "Basic " + 
                  Convert.ToBase64String(
                   Encoding.UTF8.GetBytes(
                    "x:{0}".Fmt(UserSession.ApiKey))); 
     try 
     { 
      var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url); 

      var result = new ContentResult(); 
      result.Content = Encoding.UTF8.GetString(responseBytes); 
      result.ContentEncoding = Encoding.UTF8; 
      result.ContentType = "application/json"; 
      return result; 
     } 
     catch(Exception e) 
     { 
      Logger.Error("Error while proxying to the API: ", e); 
     } 
    } 

    return Json(false); 
} 

回答

0

我將讓事情變得簡單。更改操作方法的簽名:

[HttpPost] 
public ActionResult Index(string pathInfo, string Id, string Name, int ProjectId) 
{ 
    // ID, Name and ProjectId will be populated with the values in the payload. 
    // ... Rest of code. 

} 

指定[HttpPost]將幫助你在這裏,因爲MVC模型綁定(類負責從,例如,獲得的Request.Form數據)將通過匹配填充的參數值他們的名字到Request.Form中的數據。

請注意,我不知道Id是數字還是字符串值。如果它是一個號碼,那麼你需要

[HttpPost] 
public ActionResult Index(string pathInfo, int? Id, string Name, int ProjectId) 
{ 

在這種情況下,編號是空整型。

+0

我想我發現了這個問題。 Application/json是帖子。 – allencoded

+0

你是如何通過$ .ajax()發出POST請求的?您可能需要更改'contentType'值以滿足您的需求。 –

+0

$ http角度它會自動給你contentType應用程序/ json – allencoded