Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IController vs ControllerBase vs Controller vs MyController? [closed]

I have an ASP.NET MVC project I have been building and was looking through some documentation when I came across how the Controller class is implemented. I am pretty familiar with OOP but have a few questions about why it is implemented in such a manner.

I looked on codeproject and some stack overflow questions but couldn't find quite the same question I am wondering.

  1. in MVC 5 What is the purpose of this chain: IController -> ControllerBase -> Controller -> MyController.
  2. If ControllerBase is supposed to be the minimum it takes to make a controller then why have a Controller class? Or vise versa if Controller is the minimum implementation then ControllerBase isn't really a base class?

Thanks in advance!

like image 643
David Bell Avatar asked Jul 24 '17 22:07

David Bell


People also ask

What is difference between controller and ControllerBase?

ControllerBase class Web API controllers should typically derive from ControllerBase rather from Controller. Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. If the same controller must support views and web APIs, derive from Controller .

What is ControllerBase?

ControllerBase : A base class for an MVC controller without view support. Controller : A base class for an MVC controller with view support. Thus if we are not creating views i.e. creating Web API use ControllerBase else use Controller.

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.

What is the difference between MVC and API controllers in MVC framework?

Asp.Net MVC is used to create web applications that return both views and data but Asp.Net Web API is used to create full-blown HTTP services with an easy and simple way that returns only data, not view.


1 Answers

In MVC 5, ControllerBase only acts simply like a base class for mostly internal usage and internally implements the IController.Execute(RequestContext requestContext), and here is the list of usages I got from Resharper enter image description here

The Controller then implements all of the features for filters, model and view binding. Therefore, to implement your MyController, you need to derive from Controller

Your SO reference already explained the purpose of ControllerBase and Controller.

Thing look a lot more interesting in MVC 6 (subsequently called ASP.NET MVC Core) when ASP.NET team converged MVC, WebAPI into one framework

Please look at the source code for the Controller and ControllerBase class in GitHub for ASP.NET Core 1.1.1. In the <summary> tag for each class, they say:

Controler.cs

A base class for an MVC controller with view support.

ControllerBase.cs

A base class for an MVC controller without view support.

You may ask when to use ControllerBase. My instinct tells me that if I only use ASP.NET MVC for WebAPI and doesn't require the View feature, you could derive your MyController directly from ControllerBase. Most of the time, you could derive from Controller even if you don't use View and only return string or JSON for WebAPI. Controller.cs allows you to return a View and take advantage of the auto binding.

like image 73
LxL Avatar answered Sep 19 '22 08:09

LxL