我已经实现了一个WebApi2控制器,用于将数据返回到jQuery DataTable。我在从URI解析查询时遇到错误。 VS正在抛出Null Exception错误,因为RequestUri.Query为null。
我已经在Fiddler中检查了这个请求,这是从DataTable代码发送到控制器的内容:
GET /api/GetMyStuff?sEcho=1&...
这是控制器:
[Route(Name ="GetMyStuff")]
public SysDataTablePager Get()
{
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
string sEcho = nvc["sEcho"].ToString();
int iDisplayStart = Convert.ToInt32(nvc["iDisplayStart"]);
...LINQ QUERIES HERE
var MyStuffPaged = new SysDataTablePager();
MyStuffPaged.iTotalRecords = count;
MyStuffPaged.iTotalDisplayRecords = count;
MyStuffPaged.aaData = materials.ToList();
return MyStuffPaged ;
}
我的JS代码如下:
$(document).ready(function () {
$('table.grid').dataTable({
"bServerSide": true,
"sAjaxSource": "api/GetMyStuff",
"sAjaxDataProp": "aaData",
"bLengthChange": false,
"sPaginationType": "full_numbers",
"aoColumns": [
{ "sName": "Name",
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj) {
return '<a href=\"Details/' +
oObj.aData[0] + '\">View</a>';
}
},
{ "sName": "Description" }
]
});
});
为了完整性,这里是我的返回模型
public class SysDataTablePager
{
public string sEcho { get; set; }
public int iTotalRecords { get; set; }
public int iTotalDisplayRecords { get; set; }
public List<StuffModel> aaData { get; set; }
}
答案 0 :(得分:1)
解决上述关于使用MVC框架为您解析查询的评论:
[Route(Name ="GetMyStuff")]
public SysDataTablePager Get(string sEcho, int iDisplayStart)
{
}