GET动词没有调用正确的动作

时间:2018-04-27 20:44:55

标签: c# asp.net

所以我直接从MS .NET教程中删除了这个控制器代码,它运行正常:

public class ProductsController : ApiController
{
    Product[] products = new Product[]
    {
        new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
    };

    public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }

    public IHttpActionResult GetProduct(int id)
    {
        Console.WriteLine("id = " + id);
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }
}

在同一个命名空间中,我创建了两个自己的控制器,并且都显示相同的问题 - 只有第一个动作(GetAll *())响应GET,第二个动作(GetVehicle())没有,并且设置时单独使用[HttpGet]错误装饰:

{
    "Message": "The requested resource does not support http method 'GET'."
}

此控制器似乎与上述产品控制器完全相同:

public class VehiclesController : ApiController
{
    Vehicle[] vehicles = new Vehicle[]
    {
        new Vehicle { Code = 1, Type = "type1", BumperID = "H0002" },
        new Vehicle { Code = 2, Type = "type2", BumperID = "T0016" }
    };

    public IEnumerable<Vehicle> GetAllVehicles()
    {
        Console.WriteLine("GetAllVehicles()");
        return vehicles;
    }

    public IHttpActionResult GetVehicle(int code)
    {
        Console.WriteLine("GetVehicle() code = " + code);
        var vehicle = vehicles.FirstOrDefault((v) => v.Code == code);
        if (vehicle == null)
        {
            return NotFound();
        }

        return Ok(vehicle);
    }
}

但只调用第一个动作。我错过了什么?与单独的SecurityController相同的问题。 .NET的新手......

添加了自托管路线图:

    public void Configuration(IAppBuilder app)
    {
        // Configure Web API for self-host. 
        var config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        app.UseWebApi(config);
    }

使用上面的代码,

http://localhost:8080/api/vehicles

有效,但

http://localhost:8080/api/vehicles/1

没有。

任何通过搜索Jeffrey Rennie来到这里的人都是正确的 - id中的{id}是字面的。

3 个答案:

答案 0 :(得分:2)

尝试将GetVehicle()的参数从code重命名为id。这与Anthony Liriano的回答有关。默认路由模式需要名为id的参数。

答案 1 :(得分:1)

您错过了路线和动词属性。这要求您使用System.Web.Http库。您可以在Attribute Routing in ASP.NET Web API 2

上找到更多信息
[HttpGet, Route("api/your/route/here")]
public IHttpActionResult GetVehicle(int code)
{
    Console.WriteLine("GetVehicle() code = " + code);
    var vehicle = vehicles.FirstOrDefault((v) => v.Code == code);
    if (vehicle == null)
    {
        return NotFound();
    }

    return Ok(vehicle);
}

答案 2 :(得分:1)

或者装饰你的api端点,如

   [HttpGet("{code}/GetVehicleById")]
   public IHttpActionResult GetVehicle(int code)
    {
        Console.WriteLine("GetVehicle() code = " + code);
        var vehicle = vehicles.FirstOrDefault((v) => v.Code == code);
        if (vehicle == null)
        {
            return NotFound();
        }

        return Ok(vehicle);
    }

您的api端点呼叫将是

api/vehicles/123/GetVehicleById