Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a base controller for entire asp.net MVC 4 project

I am thinking of using a base controller for the entire MVC 4 project that I am working on. I have found conflicting views online about it and not really sure if it is against best practices or if it is just a matter of personal preference.

Here is a stackoverflow post that says dont do it

Here is a post that has shown how to do it like there are no harmful effects of it. Here and here as well they explain its usage where no one really is pointing out that it is bad practice or could lead to any issues going forward.

So what really is the view on using a couple of base controllers in an MVC 4 project? Good? Bad?

Edit

I'd also like to point out that my immediate goal for using a base controller is so that I can have the Authorization done in one controller and so that all the controllers dont need to have the Authorize attribute. I will create separate base controllers for each role. Since the roles are never going to change I will never need to create another base controller for another role. What do you think of this way of going about designing the controllers?

Thanks for your time.

like image 955
user20358 Avatar asked Mar 14 '13 08:03

user20358


People also ask

Is the base class for all MVC controllers?

On codeproject: "The abstract ControllerBase class represents the base class for all MVC controllers." On MSDN: "The base class for all controllers is the ControllerBase class, which provides general MVC handling. The Controller class inherits from ControllerBase and is the default implement of a controller."

Should I create a controller for each model?

It is not recommended to create huge controllers with hundreds of actions, because they are difficult to understand and support. It is recommended to create new controller class for each model (or for most important ones) of your business logic domain.

What is the use of base controller in MVC?

The Controller handles incoming URL requests. MVC routing sends requests to the appropriate controller and action method based on URL and configured Routes. All the public methods in the Controller class are called Action methods. The Controller class must be derived from System.

Which controller is used in ASP.NET MVC?

The sample ASP.NET MVC application includes a controller named HomeController. cs located in the Controllers folder. The content of the HomeController.


2 Answers

IMHO what the post you reference says is absolutely true, but that's not a reason to not use a base controller. In fact I use a base controller in some of my ASP.NET MVC applications because of commodity.

This is no longer advisable:

Having a base controller to apply the [Authorize] attribute once is a common practice, and I don't see anything wrong on it.

Since MVC3 you can register global action filters like this:

GlobalFilters.Filters.Add(new MyAuthorizeAttribute());

like image 193
eiximenis Avatar answered Oct 18 '22 13:10

eiximenis


I have used a base controller before when dealing with things like overriding the User principal (see here for an old question of mine describing the idea: Is this Custom Principal in Base Controller ASP.NET MVC 3 terribly inefficient?).

I honestly couldn't think of a better way of doing this, so I feel that in this kind of scenario using a base controller can be a good thing.

I probably wouldn't have different base controllers for different authorization roles though as it is fairly simple (and less code) just to decorate the controller with [Authorize(Roles="whatever")] and it will be easier to see exactly what is happening.

It may be worth considering a custom AuthorizeAttribute.

like image 28
Tom Chantler Avatar answered Oct 18 '22 13:10

Tom Chantler