ASP.NET 网站 API 2 - POST / jQuery
ASP.NET Web API 2 - POST / jQuery
我正在尝试post一个对象到我使用Web的服务器API 2.代码如下:
$.ajax({
cache: false,
type: "POST",
url: "/api/Employees",
data: { EmployeeId: 1 },
success: function(result) {
console.log("employees saved successfully");
},
error: function(result) { }
});
至于网络 API:
public class EmployeesController : ApiController
{
// POST api/<controller>
public void Post([FromBody]Employee value)
{
}
}
public class Employee
{
public Int32 EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
public ReferenceData MaritalStatus { get; set; }
public Int32 VacationDays { get; set; }
public Int32 SickLeaveDays { get; set; }
public decimal Salary { get; set; }
public string Cid { get; set; }
}
我将收到来自服务器的此响应
The requested resource does not support http method 'POST'
您需要在控制器上添加注解[httppost]
public class EmployeesController : ApiController
{
// POST api/<controller>
[HttpPost]
public void Post([FromBody]Employee value)
{
}
}
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
页面的一半,在 HTTP 方法下
您的 WebApiConfig.Register 方法中是否存在与路由相关的非典型或自定义配置?当 ApiController 对操作名称使用 RESTful 约定时,无需添加 [HttpPost] 元数据属性。同样,当参数是复杂类型时,[FromBody] 属性不是必需的。默认情况下,WebApi 将查看请求的主体并使用正确的 MediaTypeFormatter (XML,JSON,FormUrlEncoded)。下面是默认值 WebApiConfig.Register。如果您进行了更改,能否恢复为默认值 WebApiConfig.Register,删除元数据属性,然后重试?
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
这是因为 API 路由的工作原理。是 /api/NameOfController/NameOfMethod。您的控制器名称是 Employee,您的方法名称是 Post。如果您想使用该方法,则需要执行 /api/Employee/Post。这就是为什么 /api/Employees/SaveEmployee 在您更改方法名称时工作正常(根据您昨天的评论)。
我正在尝试post一个对象到我使用Web的服务器API 2.代码如下:
$.ajax({
cache: false,
type: "POST",
url: "/api/Employees",
data: { EmployeeId: 1 },
success: function(result) {
console.log("employees saved successfully");
},
error: function(result) { }
});
至于网络 API:
public class EmployeesController : ApiController
{
// POST api/<controller>
public void Post([FromBody]Employee value)
{
}
}
public class Employee
{
public Int32 EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
public ReferenceData MaritalStatus { get; set; }
public Int32 VacationDays { get; set; }
public Int32 SickLeaveDays { get; set; }
public decimal Salary { get; set; }
public string Cid { get; set; }
}
我将收到来自服务器的此响应
The requested resource does not support http method 'POST'
您需要在控制器上添加注解[httppost]
public class EmployeesController : ApiController
{
// POST api/<controller>
[HttpPost]
public void Post([FromBody]Employee value)
{
}
}
http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
页面的一半,在 HTTP 方法下
您的 WebApiConfig.Register 方法中是否存在与路由相关的非典型或自定义配置?当 ApiController 对操作名称使用 RESTful 约定时,无需添加 [HttpPost] 元数据属性。同样,当参数是复杂类型时,[FromBody] 属性不是必需的。默认情况下,WebApi 将查看请求的主体并使用正确的 MediaTypeFormatter (XML,JSON,FormUrlEncoded)。下面是默认值 WebApiConfig.Register。如果您进行了更改,能否恢复为默认值 WebApiConfig.Register,删除元数据属性,然后重试?
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
这是因为 API 路由的工作原理。是 /api/NameOfController/NameOfMethod。您的控制器名称是 Employee,您的方法名称是 Post。如果您想使用该方法,则需要执行 /api/Employee/Post。这就是为什么 /api/Employees/SaveEmployee 在您更改方法名称时工作正常(根据您昨天的评论)。