webapi路由问题
相关配置和代码如下:
1 WebApiConfig:使用默认路由,暂未增加{action}参数
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
2 StaffsController:
// GET: api/Staffs
public IEnumerable<Staff> GetAllStaffs()
{
return repository.GetAll();
}
// GET: api/Staffs/5
public Staff GetStaff(string staffCD)
{
Staff item = repository.Get(staffCD);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
3 前端调用处代码:
var sStaffCD = 'S1';
$.ajax({
url: "/api/Staffs/" + sStaffCD,
type: "GET",
contentType: "application/json; charset=urf-8",
success: function (data) {
...
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
...
}
});
问题: 每次都路由到GetAllStaffs而非GetStaff,为什么?