Process of Selecting Controller Method in ASP.NET Web API Framework

Introduction

In this article, we will define the process of selecting the controller method in the ASP. NET Web API.

There is a flow chart of the Web API framework that describes the process of selecting the controller method by the Web API framework. These methods are executed for handling the HTTP request..

selection2.jpg

  • Action Based

    If there is an action parameter defined then the method is selected on the basis of the value of this parameter. According to the method name and method alias the matching is performed. Which methods are matched are those methods filtered by the verb. The returned methods are only those that match the incoming request. The filtering is done by attributes applied to the method.

  • Verb Based

    For selecting the method on the basis of the verb we need the HTTP Verb used in the request. After selecting the method there are two ways of filtering the controller.

    The first is the Get of all the methods of the HTTP verb attributes and the matching of these methods with the HTTP method in the request. Such as the HttpPut and HttpPost attributes.

    Second is the performance of the prefix matching with the HTTP methods used in the request.

  • Check Result Found

    All the methods are either based on the verb or based on the action approach. If no candidate is found then it throws an exception. If more than one candidate is found then it is necessary to analyze the parameter to determine the best possible fit.

  • Find the route by the parameter

    There are two ways to perform the comparison of the method's parameters, the first is the parameters from the route and the second is the parameters from the query string. The method will be selected without a parameter when the parameter is neither selected in the query string nor in the route. If there is more than one parameter found then we use the parameter that is the best fit.

The following is a diagram defining the process of the specific part:

selected1.jpg