Asp.net mvc 如何使用URL将param设置为FromURI?
因此,我有以下控制:Asp.net mvc 如何使用URL将param设置为FromURI?,asp.net-mvc,asp.net-web-api,routing,Asp.net Mvc,Asp.net Web Api,Routing,因此,我有以下控制: public class ItemQuery { public int storeID { get; set; } public int companyID { get; set; } public string itemName { get; set; } public string itemDescription { get; set; } public string itemPLU { get; set; } publi
public class ItemQuery {
public int storeID { get; set; }
public int companyID { get; set; }
public string itemName { get; set; }
public string itemDescription { get; set; }
public string itemPLU { get; set; }
public string itemUPC { get; set; }
public int supplierID { get; set; }
public string partNumber { get; set; }
}
public class ItemController : ApiController {
public List<Item> FindItem([FromUri]ItemQuery query) {
return new List<Item>();
}
}
它不工作,但给了我这个错误:
The requested resource does not support http method 'GET'.
我该怎么办?这是我的路线信息,我还没有更改任何内容:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
您尚未定义WebApi的路由
您拥有的
routes.MapRoute
是用于MVC的。要定义WebApi的路由,您需要使用routes.MapHttpRoute
我认为您应该确保为Web Api使用System.Web.Http命名空间
然后将方法名称更改为GetFindItem
或添加HttpGet
属性,如下所示:
[HttpGet]
public List<Item> FindItem([FromUri]ItemQuery query){ // }
http://localhost:43751/api/Item/?storeId=1&companyID=2&itemName=ABC&itemDescription=good&itemPLU=aa&itemUPC=dd&&supplierID=1&partNumber=number
如果您使用Ajax调用Web API,下面是一个示例
Js文件
var data = {
storeID: 1,
companyID: 1,
itemName: 'Test',
itemDescription: 'Description',
itemPLU: 'Test',
itemUPC: 'Test',
supplierID: 1,
partNumber: 'Description',
};
$.getJSON('/api/Item', { query:data }, function() {
alert("success");
});
var data = {
storeID: 1,
companyID: 1,
itemName: 'Test',
itemDescription: 'Description',
itemPLU: 'Test',
itemUPC: 'Test',
supplierID: 1,
partNumber: 'Description',
};
$.getJSON('/api/Item', { query:data }, function() {
alert("success");
});