I am using Web Api with ASP.NET MVC, and I am very new to it. I have gone through some demo on asp.net website and I am trying to do the following.
I have 4 get meth
using Routing.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Routing.Controllers
{
public class StudentsController : ApiController
{
static List Lststudents =
new List() { new Students { id=1, name="kim" },
new Students { id=2, name="aman" },
new Students { id=3, name="shikha" },
new Students { id=4, name="ria" } };
[HttpGet]
public IEnumerable getlist()
{
return Lststudents;
}
[HttpGet]
public Students getcurrentstudent(int id)
{
return Lststudents.FirstOrDefault(e => e.id == id);
}
[HttpGet]
[Route("api/Students/{id}/course")]
public IEnumerable getcurrentCourse(int id)
{
if (id == 1)
return new List() { "emgili", "hindi", "pun" };
if (id == 2)
return new List() { "math" };
if (id == 3)
return new List() { "c#", "webapi" };
else return new List() { };
}
[HttpGet]
[Route("api/students/{id}/{name}")]
public IEnumerable getlist(int id, string name)
{ return Lststudents.Where(e => e.id == id && e.name == name).ToList(); }
[HttpGet]
public IEnumerable getlistcourse(int id, string name)
{
if (id == 1 && name == "kim")
return new List() { "emgili", "hindi", "pun" };
if (id == 2 && name == "aman")
return new List() { "math" };
else return new List() { "no data" };
}
}
}