Introduction
In the Web API all HTTP requests pass through the routing system that decides what will manage the request. The main purpose of a routing system is to decide which action of which controller should be called to mange the actual request.
When we create a simple Web API application the template generates a default route for us in the following:
- namespace route_routeprifx {
- public static class WebApiConfig {
- public static void Register(HttpConfiguration config) {
-
- config.MapHttpAttributeRoutes();
-
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new {
- id = RouteParameter.Optional
- });
- }
- }
- }
The method config.Routes.MapHttpRoute contains the three parameters.
- A route name ("DefaultApi").
- A route Template: A template with API and two placeholders (controller and id).
- Defaults values: In this case, we are saying that the id is not mandatory in the request.
If you have used ASP.NET MVC then you will find several similarities. In MVC a route template action is present and here the action is not present, this is the main difference between the two routing systems in the Web API, the action is determined by the HTTP method.
The default route pattern for an ASP.Net MVC project is defined as follows:
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
- );
If the routing system does not find a correct match then it will retrun a HTTP status 404 Not Found.
To decide which action should be called, the routing system must analyze the HTTP method. If we place a GET request to the server, the action should be something like GetResource(). If we place a POST, the action should be something like PostResource().
When modifying or adding new routes in our project.
- amespace routwork
- {
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
-
-
- config.SuppressDefaultHostAuthentication();
- config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
-
-
- config.MapHttpAttributeRoutes();
-
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
-
- config.Routes.MapHttpRoute(
- name: "GetAllEmploye",
- routeTemplate: "api/values/GetAll",
- defaults: new { controller = "Values", action = "GetAll", id = UrlParameter.Optional }
-
- );
- config.Routes.MapHttpRoute(
- name: "Getdetail",
- routeTemplate: "api/values/Getdetail/{id}",
- defaults: new { controller = "Values", action = "Getdetail", id = UrlParameter.Optional }
-
- );
-
- config.Routes.MapHttpRoute(
- name: "GetEmployeDetailByTest",
- routeTemplate: "api/values/GetEmployeDetailByTest/{id}",
- defaults: new { controller = "Values", action = "GetEmployeDetailByTest", id = UrlParameter.Optional }
-
- );
-
- }
- }
- }
Here we have three new route names of all the new routes. They are not the same and all route templates contain various placeholders and in all the routes the id is optional.
To see the example working, we define a new ValuesController like this:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
-
- using routwork.Models;
-
- namespace routwork.Controllers
- {
-
-
- public class ValuesController : ApiController
- {
-
- [HttpGet]
- [ActionName("GetAll")]
-
- public List<Employe> GetAllEmploye()
- {
- List<Employe> emplist = new List<Employe>();
- emplist.Add(new Employe
- {
-
- Empid = 1,
- Empname = "atul",
-
-
- });
- emplist.Add(new Employe
- {
-
- Empid = 2,
- Empname = "taj",
-
-
- });
- return emplist;
- }
-
-
- [ActionName ("Getdetail")]
- public Employe GetEmployeDetail(int id)
- {
-
- return new Employe
- {
-
- Empid = 1,
- Empname = "atul"
-
- };
-
- }
-
-
- public Employe GetEmployeDetailByTest(int id)
- {
-
- return new Employe
- {
-
- Empid = 2,
- Empname = "taj"
-
- };
-
- }
- }
- }
URI |
ID |
GET api/values/GetAll |
N/A |
GET api/values/Getdetail/1 |
1 |
GET api/values/GetEmployeDetailByTest/2 |
2 |
Let us see the output of the related URL.
1. api/values/GetAll
2. api/values/Getdetail/1
3. api/values/GetEmployeDetailByTest/2
Above all calls is GET calls. If we try to POST to this URL we obtain the following error:
The requested resource does not support http method 'POST'
The possible attributes that match all the HTTP methods are:
- HttpGet
- HttpPost
- HttpPut
- HttpOptions
- HttpPatch
- HttpDelete
- HttpHead
Summary
In this article we learned how to use routing in the Web API and the difference between ASP.Net MVC and Web API Routing.