Routing in ASP.NET Web API 2 - Attribute Routing - Tutorial NO. 03
In this post i will explain basics about Routing in ASP.NET Web API.
Other than Web API, Web API 2 support new new routing call Attribute routing. It will give you more control over URIs in Web API.
Download out previous Code Sample in Here.
For previous tutorials :
Create Simple Web API 2 Project in few minutes - .NET Framework - Tutorial NO. 01
Using Postman and Inspect tool in Google Chrome to Test your Web API - Tutorial NO. 02
For studying, let's modify our code little bit. Change default Convention-based routing temple as follows.
In here i changed my "routeTemplte" "api/{controller}/{id}" to "api/{controller}/{action}/{id}"
In my priviou tutorial we test HTTP GET method using following URI
http://localhost:60823/api/Home
After change code press F5 and debug application. Then type above URI and it will throw HTTP 404 Error like this.
http://localhost:60823/api/Home/Get
It will work normally. You can use Postman or inspect tool in browser to show more information about request. See Tutorial NO. 02
Now let's look in to Attribute-based routing using our sample project.
Now change your code as follows
I put the "Route" attribute on top of our "Get"method
Then press F5 to debug application and enter the URI that we test earlier.
http://localhost:60823/api/Home/Get
Our Application will return message like this.
- Convention-Based Routing
- Attribute-Based Routing
Other than Web API, Web API 2 support new new routing call Attribute routing. It will give you more control over URIs in Web API.
Download out previous Code Sample in Here.
For previous tutorials :
Create Simple Web API 2 Project in few minutes - .NET Framework - Tutorial NO. 01
Using Postman and Inspect tool in Google Chrome to Test your Web API - Tutorial NO. 02
- Convention-based routing is still available in your API (Earlier method). You can still choose what you want or you can use both method inside your project.
Difference between Convention-based routing and Attribute-routing
- In Convention-based routing you define one/more routeing templates, are basically parameterized strings.
- But advantage of the Convention-based routing is only define in singe place. WebApiConfig.cs file in App_Start folder.
- Convention-based routing is little bit hard to support URI patterns that are in RESTful APIs.
You can open our Previous tutorial project an open WebApiConfig.cs file in App_Start folder. You an see Conventional-based default routing template in the code. (like fallowing code sample)
Download Code Sample in Here.
Download Code Sample in Here.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace SimpleWebAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
For studying, let's modify our code little bit. Change default Convention-based routing temple as follows.
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
In here i changed my "routeTemplte" "api/{controller}/{id}" to "api/{controller}/{action}/{id}"
In my priviou tutorial we test HTTP GET method using following URI
http://localhost:60823/api/Home
After change code press F5 and debug application. Then type above URI and it will throw HTTP 404 Error like this.
I will explain about HTTP status codes in detail with separate post for now 404 error is basically "not found" error.
Then Change browser URI to
It will work normally. You can use Postman or inspect tool in browser to show more information about request. See Tutorial NO. 02
Now let's look in to Attribute-based routing using our sample project.
Now change your code as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace SimpleWebAPI.Controllers
{
public class HomeController : ApiController
{
[Route("products/get")]
[HttpGet]
public IHttpActionResult Get()
{
return Ok("Hi, I am Ishara");
}
}
}
Then press F5 to debug application and enter the URI that we test earlier.
http://localhost:60823/api/Home/Get
Our Application will return message like this.
Then enter URI with our new attribute routing configuration
http://localhost:60823/products/get
It will working fine. Again you can use Postman for testing your API.
Remember : There are lots of configuration with routing such as "Custom routing configuration"
But i am not planing to put all those things in beginner tutorial. this will enough for learning basics about Web API.
For now we are using XML response (default configuration) in our sample project.
In next tutorial i am planing to explain about working with JSON responses.
Enjoy...!
Informative blog and innovations on the contents also good....
ReplyDeletejava training in navalur
selenium training in navalur