MVC Api 控制器方法 HttpPost 不工作

MVC Api Controller Method HttpPost not working

我正在使用 Visual Studio 2013...

我在使用 Api 控制器方法时遇到一些问题。

我了解 HTTPGETHTTPPUT[FromUri][FromBody].

之间的区别

我有一个简单的方法,我正在测试所有组合。

当我运行

http://localhost:62536/api/Controller/Method/10

我收到 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],但参数为空。

我的网站Api配置如下

 public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
           
        }

为什么它不起作用?

您必须在控制器方法中命名您的参数,因为它们在路由配置中被命名。因此,要么将其命名为 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)";
}

GET、PUT、POST、DELETE HTTP 动词的使用

本质上,RESTful API 设计围绕建模 business/application 域的资源、寻址它们的 URI 以及四种基本操作 GET、PUT、POST , DELETE(有时是第 5 个,PATCH)。例如,在人力资源应用程序域中,我们有雇员。我们可以将员工集合表示为由 URI http://example.com/api/Employees and each single employee in the collection as a resource addressed by the 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] 等。它只是按照约定工作。

当然,这是一个过于简单的例子,只是为了表达这一点。

希望对您有所帮助。