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<Students> Lststudents =
new List<Students>() { 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<Students> getlist()
{
return Lststudents;
}
[HttpGet]
public Students getcurrentstudent(int id)
{
return Lststudents.FirstOrDefault(e => e.id == id);
}
[HttpGet]
[Route("api/Students/{id}/course")]
public IEnumerable<string> getcurrentCourse(int id)
{
if (id == 1)
return new List<string>() { "emgili", "hindi", "pun" };
if (id == 2)
return new List<string>() { "math" };
if (id == 3)
return new List<string>() { "c#", "webapi" };
else return new List<string>() { };
}
[HttpGet]
[Route("api/students/{id}/{name}")]
public IEnumerable<Students> getlist(int id, string name)
{ return Lststudents.Where(e => e.id == id && e.name == name).ToList(); }
[HttpGet]
public IEnumerable<string> getlistcourse(int id, string name)
{
if (id == 1 && name == "kim")
return new List<string>() { "emgili", "hindi", "pun" };
if (id == 2 && name == "aman")
return new List<string>() { "math" };
else return new List<string>() { "no data" };
}
}
}
I have two get methods with same or no parameters
[Route("api/ControllerName/FirstList")]
[HttpGet]
public IHttpActionResult FirstList()
{
}
[Route("api/ControllerName/SecondList")]
[HttpGet]
public IHttpActionResult SecondList()
{
}
Just define custom routes in AppStart=>WebApiConfig.cs
=> under register method
config.Routes.MapHttpRoute(
name: "GetFirstList",
routeTemplate: "api/Controllername/FirstList"
);
config.Routes.MapHttpRoute(
name: "GetSecondList",
routeTemplate: "api/Controllername/SecondList"
);
Also you will specify route on action for set route
[HttpGet]
[Route("api/customers/")]
public List<Customer> Get()
{
//gets all customer logic
}
[HttpGet]
[Route("api/customers/currentMonth")]
public List<Customer> GetCustomerByCurrentMonth()
{
//gets some customer
}
[HttpGet]
[Route("api/customers/{id}")]
public Customer GetCustomerById(string id)
{
//gets a single customer by specified id
}
[HttpGet]
[Route("api/customers/customerByUsername/{username}")]
public Customer GetCustomerByUsername(string username)
{
//gets customer by its username
}
From here Routing in Asp.net Mvc 4 and Web Api
Darin Dimitrov has posted a very good answer which is working for me.
It says...
You could have a couple of routes:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ApiById",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = @"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null,
constraints: new { name = @"^[a-z]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Get" }
);
}
}