2013-07-31 16 views
0

我有以下的看法,在這裏所有的字段應該作爲API調用的一部分被髮送: -如何填充我的API調用URL與模型綁定參數

@using (Html.BeginForm(
     "CreateProcess","Rack", FormMethod.Post 
    )) 
    { 
    <table width="650" bgcolor="#FFFFFF" cellpadding="5" cellspacing="0" style="border:1px #ccc solid" align="center"> 
     <tr> 
      <th colspan="4">Save Asset Form</th> 
     </tr> 
     <tr> 
      <td align="right" width="142">Name</td> 
      <td><input type="text" class="txt" NAME="assetName" style="width:160px;" /></td> 
      <td width="142" align="right">Asset Type</td> 
      <td><input type="text" class="txt" NAME="assetType" value="" style="width:160px;" /></td> 
     </tr> 
     <tr> 
      <td align="right" width="142">Product Name</td> 
      <td><input type="text" class="txt" NAME="productName" style="width:160px;" /></td> 
      <td align="right" width="142">Asset Tag</td> 
      <td><input type="text" class="txt" NAME="resTag" style="width:160px;" /></td> 
     </tr> 
     <tr> 
      <td colspan="4"> 
      <table class="innertable" cellpadding="5" cellspacing="0" width="100%"> 
      <tr> 
       <td class="thd" colspan="4">Resource Info</td> 
      </tr> 
      <tr> 
       <td align="right" width="142">Asset Serial Number</td> 
       <td><input type="text" class="txt" NAME="resSlNo" style="width:160px;" /></td> 
       <td width="142" align="right">BarCode</td> 
       <td><input type="text" class="txt" NAME="barCode" value="" style="width:160px;" /></td> 
      </tr> 
      <tr> 
       <td align="right" width="142">Location</td> 
       <td><input type="text" class="txt" NAME="location" style="width:160px;" /></td> 
       <td width="142" align="right">Purchase Cost</td> 
       <td><input type="text" class="txt" NAME="purchaseCost" value="" style="width:160px;" /></td> 
      </tr> 
      <tr> 

       <td class="thd" colspan="4">Authentication details</td> 
      </tr> 
      <tr> 
       <td align="right" width="142">username</td> 
       <td><input type="text" class="txt" NAME="username" style="width:160px;" /></td> 
       <td width="142" align="right">password</td> 
       <td><input type="password" class="txt" NAME="password" value="" style="width:160px;" /></td> 
      </tr> 
      <tr> 
       <td align="right" width="142">domain</td> 
       <td><input type="text" class="txt" NAME="DOMAIN_NAME" style="width:160px;" /></td> 
       <td align="right" width="142">Authentication Mode</td> 
       <td><INPUT TYPE=radio NAME="logonDomainName" value="Local Authentication" >Local Authentication&nbsp; 
       <INPUT TYPE=radio NAME="logonDomainName" value="AD_AUTH" >AD Authentication</td> 
      </tr> 
      </table> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="4" height="10"></td> 
     </tr> 
     <tr> 
      <td align="center" colspan="4" class="footer"><input type="submit" name="operation" value="SaveAsset" class="btnsubmitact"></td> 
     </tr> 
     </table> 
    } 

我的行動方法如下: -

[HttpPost] 
     public ActionResult CreateProcess(Rack rack) 
     { 

      using (var client = new WebClient()) 
      { 

       try 
       { 
        var query = HttpUtility.ParseQueryString(string.Empty); 

var url = new UriBuilder("http://localhost:8080/jw/web/json/Process/create/" //notsure what to write here….); 
        url.Query = query.ToString(); 
        string json = client.DownloadString(url.ToString()); 

但我怎麼能構建我的操作方法在我的API URL,因此它將擁有所有的模型綁定值作爲參數,如

http://localhost:8080/jw/web/json/Process/create?parm1=&parm2=&etc.... 

回答

1

你可以從Form收集閱讀:

[HttpPost] 
public ActionResult CreateProcess(Rack rack) 
{ 
    using (var client = new WebClient()) 
    { 
     client.Headers[HttpRequestHeader.Accept] = "application/json"; 
     var query = HttpUtility.ParseQueryString(string.Empty); 
     foreach (string key in this.Request.Form) 
     { 
      query[key] = this.Request.Form[key]; 
     } 
     var url = new UriBuilder("http://localhost:8080/jw/web/json/Process/create"); 
     url.Query = query.ToString(); 
     try 
     { 
      string json = client.DownloadString(url.ToString()); 
     } 
     catch (WebException ex) 
     { 
      ... 
     } 
    } 

    ... 
} 
+1

:)我正要添加評論並鏈接到這一點:http://stackoverflow.com/questions/17096201/build-query-string -for-system-net-httpclient-get –

+0

@Darin Dimitriv。它運作良好,非常感謝。 –

相關問題