Web API发布问题

时间:2017-03-31 05:49:54

标签: asp.net asp.net-web-api

我有一个Web API,我有五个板球运动员名单。通过使用Web表单,我可以执行CRUD操作。当我在Get Cricketers上提交时,我能够获得我在controller中传递给static的所有板球运动员名单。我在我的Web Form类中创建了post的代码,并希望发布一个cricketer当我点击PostCricketer按钮时。它已成功发布,但有一些问题,Cricketer不应按升序插入相同的等级。请帮助截图点

  • 点击获得板球运动员后,我成功获得了所有五个板球运动员
  • 插入的记录应按升序复制
  • 不应插入Rank,应显示Cricketer已存在此级别的简单标签消息。

这是我的控制器代码

public class CricketersController : ApiController
{
    public static List<Cricketer> cricketers = InitCricketers();
    public IEnumerable<Cricketer> Get()
    {
        return cricketers;
    }
    public Cricketer Get(int id)
    {
        return cricketers.Where(c => c.Rank == id).FirstOrDefault();
    }
    public void Post(Cricketer cricketer)
    {
        cricketers.Add(cricketer);
    }
    public void Put(Cricketer newCricketer)
    {
        Cricketer cricketer = cricketers.Where(c => c.Rank == newCricketer.Rank).FirstOrDefault();
        cricketers.Remove(cricketer);
        cricketers.Add(newCricketer);
    }
    public void Delete(int rank)
    {
        Cricketer cDel = cricketers.Where(p => p.Rank == rank).FirstOrDefault();
        cricketers.Remove(cDel);
    }
    private static List<Cricketer> InitCricketers()
    {
        List<Cricketer> cricketers = new List<Cricketer>();
        cricketers.Add(new Cricketer
        {
            Rank = 1,
            Name = "A.B. de Villiers",
            Runs = 5000
        });
        cricketers.Add(new Cricketer
        {
            Rank = 2,
            Name = "D.A.Warner",
            Runs = 10000        
        });
        cricketers.Add(new Cricketer
        {
            Rank = 3,
            Name = "ViratKohli",
            Runs = 8480
        });
        cricketers.Add(new Cricketer
        {
            Rank = 4,
            Name = "J.E.Root",
            Runs = 6620
        });
        cricketers.Add(new Cricketer
        {
            Rank = 5,
            Name = "F.duPlessis",
            Runs = 2000
        });
        return cricketers;
    }

这是我的PostButton代码

protected void btnPostCricketer_Click(object sender, EventArgs e)
{
    lblCricketerPosted.Text = "";
    int crank = Int32.Parse(txtNewCricketerRank.Text);
    string cName = txtNewCricketerName.Text;
    int cRuns = int.Parse(txtNewCricketerRuns.Text);    
    Cricketer newCricketer = new Cricketer
    {
        Rank = crank,
        Name = cName,
        Runs = cRuns
    };
    HttpResponseMessage response = client.PostAsJsonAsync(address.ToString(), newCricketer).Result;

    // Check that response was successful or throw exception
    response.EnsureSuccessStatusCode();
    response.Content.ReadAsAsync<HttpResponseMessage>().Wait();
    lblCricketerPosted.Text = "New Crciketer Added!";

    btnGetAllCricketers_Click(sender, e);
}

enter image description here

1 个答案:

答案 0 :(得分:0)

列表本身不会检查是否已添加排名值,因此您需要在将值添加到列表之前检查该值。 此外,如果请求成功,您应该向请求者返回响应。在这个例子中,我只返回IHttpActionResult

public IHttpActionResult Post(Cricketer cricketer)
{
    if(!cricketer.Any(a=>a.Rank == cricketer.Rank))
    {
        cricketers.Add(cricketer);
        Return Ok("Success");
    }else{
        Return Content(HttpStatusCode.InternalServerError, "Rank already exists!"); //You could use a more suitable http code here
    }
}

要按升序获取值,您可以

public IEnumerable<Cricketer> Get()
{
    return cricketers.OrderBy(x => x.Rank).ToList();
}