SHASHANGKA SHEKHAR'S BLOG

Hi, I am Shashangka Shekhar, Working with Microsoft Technologies. This is my personal blog where I share some of my experiences.

ASP.NET MVC5 Routing with WEB Api2 & Area

In this article we will get explained how to map Web Api2 with Area. We will step into:

  1. Routing with WEB Api2 & Area
  2. Attribute Routing
  3. Parameters with Api2 Methods

Let’s get started: Routing with WEB Api2 & Area

First of all, we need to create a new MVC and Api Project with Visual Studio 2015.

Fig: 1.0

api_1

Fig: 1.1

api_2

Next we need to create an area in our project like below.

Fig:1.2

api_3

Now we will configure and add some controllers to our area. We also need to changes route in our Area Registration.

The main route(RouteConfig & WebApiConfig) will remain as it is.

Default Area Route: Below is the code sample of our Area registration.

Let’s create a Api2 empty Controller and create a method to invoke. Below is a image showing how to add an api2 Controller.

Fig:1.3

api_4

Method in api2 Controller.Let’s see what happened after build & run our application.

Output: This is showing that unable to locate the url content.

GET: AdminPanel/api/Dashboard/

api_5

Routing with WEB Api2 & Area: Let’s modify our existing Area route.

Let’s put a break point in our apiController get method after that again build & run the application, put the previous URL and press enter.

Fig:1.4

api_6

This time it  hits the debug point like below image.

Output: 

api_7

Next we will focus how to use Attribute Routing.

Register Attribute Routing:

Finally Main route config

Let’s add another method with attribute [Route(“AdminPanel/api/Dashboard/Get/{id:int}”)]

Let’s put a break point in our apiController get method after that again build & run the application, put the previous URL and press enter.

Fig:1.5

api_8

Output: 

api_9

 

Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help 🙂

Post a comment