Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api 无法将类对象传递给Web API_Asp.net Web Api - Fatal编程技术网

Asp.net web api 无法将类对象传递给Web API

Asp.net web api 无法将类对象传递给Web API,asp.net-web-api,Asp.net Web Api,我是Web API新手,无法使用URL分别调用下面的所有函数,想知道如何确保每次正确的函数都会使用相应的输入参数调用,因为现在它会引发类似这样的错误 找到多个与请求匹配的操作:System.Collections.Generic.IEnumerable`1[ProductsApp.Models.Product]GetAllProducts(),该操作位于ProductsApp.Controllers.ProductsController System.Web.Http.IHttpActionR

我是Web API新手,无法使用URL分别调用下面的所有函数,想知道如何确保每次正确的函数都会使用相应的输入参数调用,因为现在它会引发类似这样的错误

找到多个与请求匹配的操作:System.Collections.Generic.IEnumerable`1[ProductsApp.Models.Product]GetAllProducts(),该操作位于ProductsApp.Controllers.ProductsController System.Web.Http.IHttpActionResult GetProduct123(TestParam),该操作位于ProductsApp.Controllers.ProductsController类型 System.InvalidOperationException异常

公共类产品控制器:ApiController
{
产品[]产品=新产品[]
{ 
新产品{Id=1,Name=“番茄汤”,Category=“杂货”,Price=1},
新产品{Id=2,Name=“Yo”,Category=“Toys”,Price=375万},
新产品{Id=3,Name=“Hammer”,Category=“Hardware”,Price=16.99M}
};
公共IEnumerable GetAllProducts()
{
退货产品;
}
[HttpGet]
公共IHttpActionResult GetProduct(FromUri]TestParam t1)
{
var product=products.FirstOrDefault((p)=>p.Id==t1.Id);
如果(产品==null)
{
返回NotFound();
}
返回Ok(产品);
}
[HttpGet]
公共IHttpActionResult GetProduct123([FromUri]TestParam t2)
{
var product=products.FirstOrDefault((p)=>p.Id==t2.id2);
如果(产品==null)
{
返回NotFound();
}
返回Ok(产品);
}
公共类TestParam
{
公共int id{get;set;}
公共int id2{get;set;}
}
}

您希望Web API如何区分这两种方法?它们都是
HttpGet
,都接受类型为
TestParam
的单个参数。嘿,John,我在RouteConfig上使用{controller}/{action}/{id}解决了这个问题,因为我缺少actiontype名称。。。
 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;
    }

[HttpGet]
   public IHttpActionResult GetProduct(FromUri]TestParam t1)
    {
        var product = products.FirstOrDefault((p) => p.Id == t1.id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }




    [HttpGet]
    public IHttpActionResult GetProduct123([FromUri]TestParam t2)
    {
        var product = products.FirstOrDefault((p) => p.Id == t2.id2);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

    public class TestParam
    {
        public int id { get; set; }
        public int id2 { get; set; }

    }
}