Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Migrate from WCF Rest Services to Web API 2- Basics of Attribute Routing

4.43/5 (3 votes)
11 Jan 2015CPOL2 min read 35.1K   330  
Web API 2- Basics of Attribute Routing

Introduction

There are many applications written in WCF Rest framework. As new clients (mobile) request more new features to be supported like JSON vs XML or Authentication, we should require migrating them to new Web API framework without breaking existing clients especially URIs.

Background

There were times SOAP is the only best way to create any Web Service. As technology evolves, the demand for REST based services are increasing rapidly, because REST is light weight and any other technology can easily make a call to REST services (downloading WSDL for SOAP makes more complex).

So most of the developers choose to develop REST services with WCF Rest framework. And existing service URL looks like below:

When we have multiple clients for single API services, each client would like to choose medium of communication like XML and JSON. Then we require modifying the service in such a way that it should support content negotiation without impacting existing clients. The obvious choice is to use WebAPI . However I don’t see a big use, until Attribute routing has been introduced.

What is Attribute Routing?

In simple words, Routing is a Directory in a service. We define which action of controller should be invoked when a request URI arrives from client. To achieve my above requirements, I have to write a complex routings in web API configuration. However Attribute routing gives us more flexibility and control over URI definitions. We can add any meaningful URI structure in the Route attribute of each Actions.

C++
public class ActivityController : ApiController
{
   [Route("ActivityService/PendingActivities)]
   public IHttpActionResult GetPendingActivities()
   {
   }
   [Route("UpdateActivityService/UpdateActivity")]
   [HttpPost]
   public IHttpActionResult UpdateActivity(Activity activity)
   {
   }
}

In most of the scenarios, the service name (or) first part of the URI is common for all actions in the controller, then we can mention the common route prefix above the controller name itself.

C++
[RoutePrefix("ActivityService")]
public class ActivityController : ApiController
{
   [Route("PendingActivities)]
   public IHttpActionResult GetPendingActivities()
   {
   }
   [Route("UpdateActivity")]
   [HttpPost]
   public IHttpActionResult UpdateActivity(Activity activity)
   {
   }
}

To make sure that the above pattern should work, check the below setting is registered in WebApiConfig.

C++
config.MapHttpAttributeRoutes();

If you use the VS 2013 Web API template, this should already exist and you can concentrate on URIs.

Migrating from WCF Rest Service to WebApi2

By using the above mentioned routing technique, IHttpActionResult and new ASP.NET Authentication technique, we can simply replace the OperationContract class with appropriate controller class. However, we can just use the same data access and business layers.

For more information on Attribute routing, please go through the ASP.NET documentation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)