[英]Get and set items in a collection
I have two classes: Group and Item. 我有两个班级:组和项目。
public class Group
{
public string Name{ get; set; }
public List<Item> ItemList { get; set; }
}
And then Item 然后项目
public class Item
{
public int ID{ get; set; }
public string Name{ get; set; }
public string Description{ get; set; }
public Group ItemGroup{ get; set; }
}
Each group show have a set of items. 每个小组表演都有一组项目。
The following code is meant to get the list of items of a particular group. 以下代码旨在获取特定组的项目列表。
public IEnumerable<Item> GetItemByGroup(string group)
{
return repository.GetAllItems().Where
(p =>p.ItemGroup.Name.Equals(group));
}
Full controller code 完整的控制器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TestService.Models;
using System.IO;
namespace TestService.Controllers
{
public class ItemsController : ApiController
{
static readonly IItemRepository repository = new ItemRepository();
public IEnumerable<Item> GetAllItems()
{
return repository.GetAllItems();
}
public IEnumerable<Item> GetItemsByGroup(string group)
{
return repository.GetAllItems().Where
(p => p.Category.Name.Equals(group));
}
}
}
When I run this I get the error: No type was found that matches the controller named 'item'.
当我运行此命令时,出现错误:
No type was found that matches the controller named 'item'.
When I called it using localhost:1234/api/item?group=group1
. 当我使用
localhost:1234/api/item?group=group1
调用它时。
How do I get the list of items in a particular category specified by a string? 如何获取字符串指定的特定类别中的项目列表?
Your URL is incorrect. 您的网址不正确。 Try .../api/ items ... instead of .../api/ item ...
尝试... / api / 项目 ...而不是... / api / 项目 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.