Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 如何在mvc4中将参数从控制器(Content.ReadAsAsync)传递到api控制器_Asp.net Mvc 4_Asp.net Web Api_Parameter Passing - Fatal编程技术网

Asp.net mvc 4 如何在mvc4中将参数从控制器(Content.ReadAsAsync)传递到api控制器

Asp.net mvc 4 如何在mvc4中将参数从控制器(Content.ReadAsAsync)传递到api控制器,asp.net-mvc-4,asp.net-web-api,parameter-passing,Asp.net Mvc 4,Asp.net Web Api,Parameter Passing,这是我的控制器 public ActionResult Index(string Searchby, string SearchValue) { var client = new HttpClient(); var productDetailUrl = Url.RouteUrl( "DefaultApi", new { httproute = "", controller = "Assets" },

这是我的控制器

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参数正在形成

<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 },
        })
    });
});

我应该如何从ReadAsAsync传递Searchby、SearchValue,只要将您的uri设计为字符串即可。从您的代码中不清楚您想要请求什么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);
}

我在我的webapiconfig config.Routes.MapHttpRoute名称中添加了这个:DefaultApi,routeTemplate:api/{controller}/{Searchby}/{SearchValue};