WebApi: Execution of RPC Style Action Method in WebApi Using MVC4

In this article I'll share my thoughts on how to implement a RPC Style action method in WebApi using MVC4.

In simple words RPC style is a way to call any method via URL, whereas RPC stands for Remote Procedure Call.

Kindly you can also visit these links to learn more about Configuration over Convention in the WebApi.

I've defined a method named RPCStyleMethodFetchEmployee.To call this method we need to put the URL in the browser http://localhost:57888/api/employees/RPCStyleMethodFetchEmployee/

For this RPC-style selection of an action method to work, you need to make an entry in the WebApiConfig.cs file under App_Start folder.

Kindly make this entry in WebApiConfig.cs under the Register method under WebApiConfig.cs:

WebApiConfig 
 
The only difference now is that the action method that handles the GET request is GetEmployeeRpcStyle, that is part of the URI.
 
 URI

(http://localhost:57888/api/employees/RPCStyleMethodFetchEmployee/) route data. Review theURI you used. It is no longer in the REST style. The action method is also part of the URI and is in the RPC-style.

The action method is also part of the URI and is in RPC-style.

Let's create a sample application and do this step-by-step.

Step 1: Let's first create a sample web application and use an ASP.NET MVC 4 Web Application and name it as you choose; I used WebApiDemo as depicted in the image below:

create a web application

Step 2: Click OK and choose Web API from the templates shown in the wizard window.

Web API option

Step 3: You'll find the application structure as shown below at first sight.
 
application structure

Step 4: Right-click the Controllers folder in the Solution Explorer of Visual Studio. Select "Add" ➤"Controller" and provide a name of EmployeesController for the controller. Leave the option for Empty API Controller selected in the Template dropdown and click "Add", as shown in the figure below. Notice that the generated controller class inherits from ApiController, a class that is part of the ASP.NET Web API framework. Kindly add the following code into the EmployeesController class.

  1. public static IList<Employee> listEmp = new List<Employee>()  
  2. {  
  3. new Employee()  
  4. {  
  5. ID =001, FirstName="Sachin", LastName="Kalia"  
  6. },  
  7. new Employee()  
  8. {  
  9. ID =002, FirstName="Dhnanjay" ,LastName="Kumar"  
  10. },  
  11. new Employee()  
  12. {  
  13. ID =003, FirstName="Ravish", LastName="Sindhwani"  
  14. },  
  15. new Employee()  
  16. {  
  17. ID =004, FirstName="Amit" ,LastName="Chaudhary"  
  18. },  
  19. }; 

Controllers folder 

Step 5: Right-click the Models folder in the Solution Explorer of Visual Studio. Select "Add" -> "Class" to add a new class with the name "Employee".
 
add a new class

After creating the Employee class, kindly add the following code into this class.
  1. public class Employee  
  2. {  
  3. public string ID { getset; }  
  4. public string FirstName { getset; }  
  5. public string LastName { getset; }  

Now I've declared two methods named RPCStyleMethodFetchEmployee, that follows the convention based (starts with Get, PUT, POST, Delete or PATCH) approach to call methods of the WebApi
 
methods of WebApi 

Press F5 and run your application; it will show the following image:
 
run your application 

Great WebApi is up and running.

Kindly paste this URL (http://localhost:57888/api/employees/RPCStyleMethodFetchFirstEmployees ) into the browser and press Enter, it will reach into your code segment. Where you've set a debugging breakpoint.
 
debugger 

Press F5 again and see the result as shown below in the image.
 
result 

The only difference now is that the action method that handles the GET request is GetEmployeeRpcStyle, that is part of the URI.

 
http://localhost:57888/api/employees/RPCStyleMethodFetchFirstEmployees route data. Review the URI you used. It is no longer in the REST style. The action method is also part of the URI and is in RPC-style.The same process is for second action method declared in the employees controller class.

Conclusion

In this article we looked into a RPC Style action method call in WeApi.