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:
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/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:
Step 2: Click OK and choose Web API from the templates shown in the wizard window.
Step 3: You'll find the application structure as shown below at first sight.
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.
- public static IList<Employee> listEmp = new List<Employee>()
- {
- new Employee()
- {
- ID =001, FirstName="Sachin", LastName="Kalia"
- },
- new Employee()
- {
- ID =002, FirstName="Dhnanjay" ,LastName="Kumar"
- },
- new Employee()
- {
- ID =003, FirstName="Ravish", LastName="Sindhwani"
- },
- new Employee()
- {
- ID =004, FirstName="Amit" ,LastName="Chaudhary"
- },
- };
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".
After creating the Employee class, kindly add the following code into this class.
- public class Employee
- {
- public string ID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
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
Press F5 and run your application; it will show the following image:
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.
Press F5 again and see the result as shown below in the image.
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.