Click here to Skip to main content
15,891,248 members
Articles / Web Development / HTML
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
11 Jan 2015CPOL2 min read 34.6K   329   7   2
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)


Written By
Software Developer (Senior)
United States United States
10+ years of building high availability and complex line of business applications using Microsoft.net technologies and AWS cloud expertise.
Exposure to Microsoft Technologies(C#, Asp.Net Core, WPF, ASP.Net MVC, Angular 2,Angular 4, Web API,.Net Core Stack, WCF, Silverlight, Windows Forms, Windows Services, Web Services, Entity Framework, jQuery,ASP.Net, SOAP, Rest Services , NUnit and SQL Server) and Cloud Technologies - AWS S3, SQS, Lambda, EC2, ECS, ELB, Beanstack, RDS, Cloudformation and Terraform.

Comments and Discussions

 
Questionmigrate wcf to asp.net web api 2 Pin
Member 6238705-Sep-15 4:36
Member 6238705-Sep-15 4:36 
AnswerRe: migrate wcf to asp.net web api 2 Pin
RaviAakula28-Sep-15 18:22
RaviAakula28-Sep-15 18:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.