Thursday, April 17, 2014

WebApi: Execution of RPCStyle action method in WebApi using MVC4

In this article I’ll share my thoughts on How to implement 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 can also visit these links to know more about Configuration over Convention in WebApi.

WEBAPI PATCH UPDATE USING FROMBODY PARAMETER IN WEBAPI USING MVC4 TEMPLATE

WebApi Configuration over Convention using MVC4 template

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

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

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

clip_image002

 

                                                                                                                                                                                                                                                                                                                                                                 clip_image003

The only difference now is that the action method that handles the GET request is GetEmployeeRpcStyle, which 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 RPC-style.

Let’s create a sample application and achieve this step by step.

Step 1: Let’s first create a sample web application and using ASP.NET MVC 4 Web Application and named It with your choice as I gave WebApiDemo shown in depict image below:

clip_image006

Step2: Click ok and choose Web API option from the templates shown in wizard window.

clip_image008

Step3: You’ll find the application structure as shown below at first sight.

clip_image010

Step 4: Right-click the Controllers folder in the Solution Explorer of Visual Studio. Select Add ➤Controller and give a name of EmployeesController for the controller. Leave the option Empty API Controller selected in the Template dropdown and click Add, as shown in Figure below. Notice that the generated controller class inherits from ApiController, a class that is part of the ASP.NET Web API framework.Kinldy add the following code into 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"
            },

        };

clip_image012

Step 5: Right-click the Models folder in the Solution Explorer of Visual Studio. Select Add ➤ Class

to add a new class with a name of Employee.

clip_image014

After creating the Employee.cs 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 as RPCStyleMethodFetchEmployee

,RPCStyleMethodFetchFirstEmployees, which follows the convention based (starts with Get, PUT, POST, Delete and PATCH) approach to call methods of WebApi

clip_image016

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

clip_image018

Great WebApi is up and running.

Kindly paste this url (http://localhost:57888/api/employees/RPCStyleMethodFetchFirstEmployees ) into browser and press enter, it will reach into your code segment. Where you’ve set a debugger point.

clip_image019

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

clip_image021

 

                                                                                                                                                                                                                                                                                                                                                                       clip_image003[1]

The only difference now is that the action method that handles the GET request is GetEmployeeRpcStyle, which is part of the URI http://localhost:57888/api/employees/RPCStyleMethodFetchFirstEmployees 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 RPC-style.The same process is for second action method declared in employees controller class.

 

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

Hope it will help you somewhere down the line Winking smile

Keep coding and Stay Happy Smile

Thanks

Sachin Kalia

2 comments :