今天我有一个像这样的Web Api:
public HttpResponseMessage Get()
{
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query, Encoding.UTF8);
string Report = nvc["Report"].ToString();
Int64 Id = Convert.ToInt64(nvc["Id"]);
var response = new HttpResponseMessage(HttpStatusCode.OK);
switch (Report)
{
case "PackLista":
response.Content = new StreamContent(this.PackLista(Id));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
break;
}
return response;
}
对于测试,我使用javascript通过按钮和硬编码参数调用此API,如下所示:
@using (@Html.BeginForm("Index", "Meels", FormMethod.Get))
{
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-3">
<label class="control-label ">Choose:</label>
@Html.DropDownList("SearchString", (IList<SelectListItem>)ViewBag.Htos, "-- All --", new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input value="Report" class="btn btn-primary" onclick="cmdPrint_onclick()"/>
</div>
</div>
</div>
}
<script>
function cmdPrint_onclick()
{
//
window.location.href = '/api/Report/Report?report=PackLista&Id=2' ;
}
</script>
现在我想在javascript中使用下拉列表中的值作为参数Id
。或者也许jQuery是正确的方法?我怎样才能做到这一点?
如您所见,API返回的是pdf格式的流,因此结果会打开一个pdf文档。