Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution Order for the ApiController

Is there something like this for the common method order?

http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-WebApiStackDiagramsNew/6428.ASP.NET_5F00_MVC4_5F00_WebAPI_5F00_StackDiagram_5F00_Future.jpg

My Web Api solution has now a new order in the execution of some methods since I upgraded from Beta/RC version to RTM version. (Its not the reverse order of message handler execution)

Earlier this method of the APIControllers was called:

protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext)
{
}

Before the filtermethods of my AuthorizationFilter

public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
{
}

After RTM the OnAuthorization is called before Initialize(). Are there some informations about the execution order of the ApiController methods and the changes after RTM release?

like image 608
user437899 Avatar asked Sep 05 '12 08:09

user437899


People also ask

What is the difference between controller and ApiController?

They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data. ApiControllers are specialized in returning data.

What is the correct order of the following options for setting an attribute at more than one scope controller global action?

Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters.

Is ApiController deprecated?

There is indeed no particular ApiController class anymore since MVC and WebAPI have been merged in ASP.NET Core. However, the Controller class of MVC brings in a bunch of features you probably won't need when developing just a Web API, such as a views and model binding.

What is an ApiController?

Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder.


1 Answers

Assuming the request goes into the ApiController scope, the operation order is as below:

  • The ExecuteAsync method of the ApiController is invoked.
  • The Initialize method of the ApiController is invoked.
  • The registered Action Selector is retrieved.
  • The SelectAction method of the registered action selector is invoked. If only one action method is matched, the pipeline continues.
  • All registered Filters for the selected action is retrieved.
  • The Authorization Filters are called. The authorization filter can decide either to let the pipeline to continue executing or to terminate the pipeline.
  • If Authorization Filters didn't terminate the request, action parameter bindings are performed.
  • ApiController.ModelState is set.
  • Action Filters are invoked. The Action Filters can decide either to let the pipeline to continue executing or terminate the pipeline.
  • If Action Filters didn't terminate the request, registered Action Invoker is retrieved.
  • The InvokeActionAsync method of the registered Action Invoker is called to invoked the selected action method.
  • Note: If any exception occurs from the execution of the Authorization Filters to the execution of the action method, the exception filters are be called.

There are a few more things which happen in between but this is very close to a complete view. Check out the ApiController source code for more information.

like image 103
tugberk Avatar answered Nov 08 '22 13:11

tugberk