我正在使用Visual Studio 2013 ...
我在使用Api控制器方法时遇到了一些问题。
我理解HTTPGET
,HTTPPUT
,[FromUri]
和[FromBody].
我有一个简单的方法,我正在测试所有组合。
当我跑步时
我得到HTTP 404错误就是这个例子
[HttpPut]
public string Method([FromUri] int a)
{
return "";
}
[HttpPut]
public string Method( int a)
{
return "";
}
[HttpGet]
public string Method([FromUri] int a)
{
return "";
}
[HttpGet]
public string Method( int a)
{
return "";
}
获得HTTP 405错误就是这个例子
[HttpPut]
public string Method([FromBody] int a)
{
}
无错误,但参数a
为0 ..
[HttpGet]
public string Method([FromBody] int a)
{
return a.ToString();
}
换句话说..使用[HttpPut]或使用未定义或定义为[FromUri]的参数不起作用。
它仅适用于[HttpGet]和[FromBody],但参数为空。
我的WebApiConfig看起来像这样
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
为什么它不起作用?
答案 0 :(得分:1)
您必须在控制器方法中为参数命名,因为它们在路由配置中命名。因此,要么将其命名为id
:
[HttpPut]
public string Method(int id)
{
return "";
}
[HttpGet]
public string Method(int id)
{
return "";
}
...或者,在路线配置中更改它:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{a}",
defaults: new { a = RouteParameter.Optional }
);
}
请注意,它现已配置为a
而非id
<强>更新强>
以下是适用于上述配置路线的控制器方法:
// Example URI: http://localhost:62536/api/Controller/PutMethod/10
[HttpPut]
public string PutMethod(int a)
{
return a.ToString();
}
// Example URI: http://localhost:62536/api/Controller/GetMethod/10
[HttpGet]
public string GetMethod(int a)
{
return a.ToString();
}
// Example URI: http://localhost:62536/api/Controller/PutMethod/
[HttpPut]
public string PutMethod()
{
return "(none)";
}
// Example URI: http://localhost:62536/api/Controller/GetMethod/
[HttpGet]
public string GetMethod()
{
return "(none)";
}
本质上,RESTful API设计围绕着为业务/应用程序域建模的资源,解决它们的URI以及四个基本操作GET,PUT,POST,DELETE(有时是第五个,PATCH)。例如,在人力资源应用程序域中,我们有员工。我们可以将URI http://example.com/api/Employees所代表的资源和集合中的每个员工作为URI http://example.com/api/Employee/id寻址的资源来代表员工集合,其中 id 是员工的ID。所以,我们可以:
如果路线配置如下:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",i
defaults: new { id = RouteParameter.Optional }
);
}
员工的控制器可以是:
public class EmployeesController : ApiController
{
// GET api/Employees
public IEnumerable<Employee> Get()
{
List<Employee> employees = null;
// Get here the list of employees
return employees;
}
// POST api/Employees
public int Post(Employee employee)
{
// Persist here the new employee and determine its ID (for example, identity in SQL Server)
return employee.Id; // Returns the ID of the newly created employee
}
// GET api/Employees/5
public Employee Get(int id)
{
Employee employee;
// Get here the employee with the id
return employee;
}
// PUT api/Employees/5
public Employee Put(int id, Employee employee)
{
var updatedEmployee;
// Find existing employee with the id, update it with the data passed via employee argument and persist it
return updatedEmployee; // It's a good practice to return the updated entity back, especially in the case when updating changes some properties, e.g. summarized values, counts, etc.
}
// DELETE api/Employees/5
// More complicated example showing communicating statuses back to the client regarding successfulness of the operation
public IHttpAction Delete(int id)
{
Employee employee;
// Find the employee with the id
if (employee == null)
return NotFound(); // The employee is not found. Signal 404, i.e. there's no resource at this URI.
// Delete here the employee from the persistence mechanism
return Ok(); // The employee is found and deleted. Signal 200 - OK
}
}
请注意,不需要属性[HttpGet]
,[HttpPut]
等。它只是按惯例运作。
当然,这只是一个过于简单的例子,只是为了传达这一点。
希望它有所帮助。