确定REST服务的起点资源

时间:2016-08-08 13:00:49

标签: rest asp.net-web-api hateoas hypermedia

我正在为迷宫游戏创建ASP.Net Web API服务。

  1. 用户应获取迷宫中的所有细胞细节,以便在2D平面中显示迷宫。我使用get方法public List<Cell> Get()
  2. 实现了这一点
  3. 用户应通过提供CellID作为输入来获取单元格的详细信息。我使用get方法public CellRepresentation Get(string id)实现了这一点。请注意,CellRepresentation用于使用WebApi.Hal返回超媒体链接。这些链接代表可能的移动方式的目标单元格,如向上,向下,向左和向右。
  4. 现在,我需要一种沟通方式,这是用户启动游戏的起点单元格。 [在以下迷宫中,单元格“10”是起点]。什么是基于REST的发送此消息的方式?

    控制器

    public class CellsController : ApiController
        {
            public CellsController()
            {
    
            }
    
            //Get all cells in the maze
            public List<Cell> Get()
            {
                Maze m = new Maze();
                return m.GetCells();
            }
    
            //Get specific cell
            public CellRepresentation Get(string id)
            {
    
                Maze m = new Maze();
    
                Cell c = m.GetCell(id);
                List<Cell> possibleLists = m.GetPossibleRelatedCells(c);
                CellRepresentation beerRep = new CellRepresentation(c, possibleLists);
                return beerRep;
            }
        }
    

    HAL相关课程

    public class CellRepresentation : WebApi.Hal.Representation
            {
                //CellID
    
                Cell theCell;
                List<Cell> possibleMoveCells;
    
                public CellRepresentation(Cell c, List<Cell> possibleMoveCells )
                {
                    theCell = c;
                    this.possibleMoveCells = possibleMoveCells;
                }
    
                public override string Rel
                {
                    get { return LinkTemplates.CellLinks.CellEntry.Rel; }
                    set { }
                }
    
                public override string Href
                {
                    get { return LinkTemplates.CellLinks.CellEntry.CreateLink(new { id = theCell.CellID }).Href; }
                    set { }
                }
    
                protected override void CreateHypermedia()
                {
                    foreach (Cell relatedCell in possibleMoveCells)
                    {
                        if (relatedCell.RelativeName == "Up")
                        {
                            Links.Add(LinkTemplates.CellLinks.UpLink.CreateLink(new { id = relatedCell.CellID }));
                        }
                        if (relatedCell.RelativeName == "Right")
                        {
                            Links.Add(LinkTemplates.CellLinks.RightLink.CreateLink(new { id = relatedCell.CellID }));
                        }
                        if (relatedCell.RelativeName == "Down")
                        {
                            Links.Add(LinkTemplates.CellLinks.DownLink.CreateLink(new { id = relatedCell.CellID }));
                        }
                        if (relatedCell.RelativeName == "Left")
                        {
                            Links.Add(LinkTemplates.CellLinks.LeftLink.CreateLink(new { id = relatedCell.CellID }));
                        }
                    }
                }
            }
    
        public static class LinkTemplates
        {
    
            public static class CellLinks
            {
                public static Link CellEntry { get { return new Link("self", "~/api/cells/{id}"); } }
                public static Link UpLink { get { return new Link("up", "~/api/cells/{id}"); } }
                public static Link RightLink { get { return new Link("right", "~/api/cells/{id}"); } }
                public static Link DownLink { get { return new Link("down", "~/api/cells/{id}"); } }
                public static Link LeftLink { get { return new Link("left", "~/api/cells/{id}"); } }
            }
        }
    

    商务课程

    public class Cell
        {
            public int XVal { get; set; }
            public int YVal { get; set; }
            public bool TopIsWall { get; set; }
            public bool RightIsWall { get; set; }
            public bool BottomIsWall { get; set; }
            public bool LeftIsWall { get; set; }
    
            public bool IsStartCell { get; set; }
            public bool IsExtCell { get; set; }
    
            public string RelativeName { get; set; }  //Top, Right, Etc.
    
            public string CellID
            {
                get
                {
                    string characterID = XVal.ToString() + YVal.ToString();
                    return characterID; //Example 10
                }
    
             }  
        }
    
    
        public class Maze
        {
            List<Cell> cells;
            public Maze()
            {
                cells = CreateCells();
            }
    
    
            public Cell GetFirtCell()
            {
                Cell firstCell = null;
    
                foreach (Cell c in cells)
                {
                    if(c.IsStartCell )
                    {
                        firstCell = c;
                        break;
                    }
                }
                return firstCell;
            }
    
            public Cell GetCell(string cellID)
            {
                Cell theCell = null;
    
                foreach (Cell c in cells)
                {
                    if (c.CellID == cellID)
                    {
                        theCell = c;
                        break;
                    }
                }
                return theCell;
            }
    
            public List<Cell> GetCells()
            {
                return cells;
            }
    
            public List<Cell> GetPossibleRelatedCells(Cell inputCell)
            {
                List<Cell> possibleCells = new List<Cell>();
    
                foreach (Cell c in cells)
                {
    
                    if (c.XVal == inputCell.XVal-1 && c.RightIsWall == false  && c.YVal== inputCell.YVal  )
                    {
                        //Go left from the input cell
                        c.RelativeName = "Left";
                        possibleCells.Add(c);
    
                    }
                    else if (c.XVal == inputCell.XVal + 1 && c.LeftIsWall == false && c.YVal == inputCell.YVal )
                    {
                        //Go right from the input cell
                        c.RelativeName = "Right";
                        possibleCells.Add(c);
                    }
                    else if (c.YVal == inputCell.YVal - 1 && c.TopIsWall == false && c.XVal == inputCell.XVal )
                    {
                        //Go down from the input cell
                        c.RelativeName = "Down";
                        possibleCells.Add(c);
                    }
                    else if (c.YVal == inputCell.YVal + 1 && c.BottomIsWall == false && c.XVal == inputCell.XVal)
                    {
                        //Go up from the input cell
                        c.RelativeName = "Up";
                        possibleCells.Add(c);
                    }
    
                }
                return possibleCells;
            }
    
            public List<Cell> CreateCells()
            {
                List<Cell> cells = new List<Cell>();
                //cells = 
    
                Cell cell1 = new Cell
                {
                    XVal = 0,YVal = 0,TopIsWall = false,RightIsWall = false,BottomIsWall = true,LeftIsWall = true,
                    RelativeName="Self"
                };
    
                Cell cell2 = new Cell
                {
                    XVal = 1,YVal = 0,TopIsWall = true,RightIsWall = false,BottomIsWall = false,LeftIsWall = false,
                    IsStartCell = true, //--Start
                    RelativeName="Self"
                };
    
                Cell cell3 = new Cell
                {
                    XVal = 2,YVal = 0,TopIsWall = false,RightIsWall = false,BottomIsWall = true,LeftIsWall = false,
                    RelativeName="Self"
                };
    
                Cell cell4 = new Cell
                {
                    XVal = 3,YVal = 0,TopIsWall = false,RightIsWall = true,BottomIsWall = true,LeftIsWall = false,
                    RelativeName = "Self"
                };
    
    
                Cell cell5 = new Cell
                {
                    XVal = 0,YVal = 1,TopIsWall = true,RightIsWall = false,BottomIsWall = false,LeftIsWall = true,
                    RelativeName = "Self"
                };
    
    
                Cell cell6 = new Cell
                {
                    XVal = 0,YVal = 0,TopIsWall = true,RightIsWall = false,BottomIsWall = false,LeftIsWall = false,
                    RelativeName = "Self"
                };
    
    
                Cell cell7 = new Cell
                {
                    XVal = 1,YVal = 1,TopIsWall = true,RightIsWall = false,BottomIsWall = true,LeftIsWall = false,
                    RelativeName = "Self"
                };
    
    
                Cell cell8 = new Cell
                {
                    XVal = 2,YVal = 1,TopIsWall = false,RightIsWall = true,BottomIsWall = false,LeftIsWall = false,
                    RelativeName = "Self"
                };
    
    
                Cell cell9 = new Cell
                {
                    XVal = 3,YVal = 1,TopIsWall = false,RightIsWall = true,BottomIsWall = false,LeftIsWall = true,
                    RelativeName = "Self"
                };
    
    
                Cell cell10 = new Cell
                {
                    XVal = 2,YVal = 2,TopIsWall = true,RightIsWall = true,BottomIsWall = false,LeftIsWall = true,
                    RelativeName = "Self"
                };
    
                Cell cell11 = new Cell
                {
                    XVal = 3,YVal = 2,TopIsWall = true,RightIsWall = true,BottomIsWall = false,LeftIsWall = true,
                    RelativeName = "Self"
                };
    
                Cell cell12 = new Cell
                {
                    XVal = 3,YVal = 3,TopIsWall = true,RightIsWall = true,BottomIsWall = false,LeftIsWall = false,
                    IsExtCell = true, //--Exit
                    RelativeName = "Self"
    
    
                };
    
                cells.Add(cell1);
                cells.Add(cell2);
                cells.Add(cell3);
                cells.Add(cell4);
                cells.Add(cell5);
                cells.Add(cell6);
    
                cells.Add(cell7);
                cells.Add(cell8);
                cells.Add(cell9);
                cells.Add(cell10);
                cells.Add(cell11);
                cells.Add(cell12);
    
    
                return cells;
    
            }
    
        }
    

    迷宫

    enter image description here

0 个答案:

没有答案