简体   繁体   中英

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'. When I called it using 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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM