2014-02-12 150 views
0

這是我的控制器如何從控制器(Content.ReadAsAsync)通過參數API控制器mvc4

public ActionResult Index(string Searchby, string SearchValue) 
    { 
     var client = new HttpClient(); 
     var productDetailUrl = Url.RouteUrl(
      "DefaultApi", 
      new { httproute = "", controller = "Assets" }, 
      Request.Url.Scheme 
     ); 
     var model = client 
        .GetAsync(productDetailUrl) 
        .Result 
        .Content.ReadAsAsync<AssetDetails[]>().Result; 

     return View(model);} 

searchby和searchvalue PARAMS來了形式

<script type="text/javascript"> 

$(function() { 
    $('#txtSearch').blur(function() { 
     var Searchby = $('select[name="Searchby"]').val(); 
     var SearchValue = $("txtSearch").val(); 
     $.ajax({ 
      type: "POST", 
      url: '/Asset/Index', 
      data: { Searchby: searchby, SearchValue: SearchValue }, 
     }) 
    }); 
}); 

我應該如何通過Searchby,來自ReadAsAsync的SearchValue

+0

我在webapiconfig config.Routes.MapHttpRoute將此添加( 名: 「DefaultApi」, routeTemplate: 「API/{控制器}/{Searchby}/{SearchValue}」 ); – Karthik

回答

0

只需將您的uri製作爲一個字符串即可。從你的代碼中不清楚你想請求什麼,所以我建議它看起來如何。此外,我將代碼轉換爲使用異步模式以避免死鎖。

public async Task<ActionResult> Index(string Searchby, string SearchValue) 
{ 
    var productDetailUrl = string.Format("http://<hostname>/you/uri/here?searchBy={0}&searchValue={1}", SearchBy, SearchValue); 

    var client = new HttpClient(); 
    var response = await client.GetAsync(productDetailUrl); 
    var model = await response.Content.ReadAsAsync<AssetDetails[]>(); 

    return View(model); 
}