Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the controller name derived from the class name?

This is a newbie question...

I am looking at the default asp.net mvc3 project and noticed that there is a controller called:

public class AccountController : Controller

I looked throughout the code and couldn't find a place that specified AccountController maps to /Account/ for the URL.

I discovered that you can change the routing using routes.MapRoute(..) in the Global.asax, but I still don't know where they specified that AccountController maps to /Account/.

If it is assumed from the class name, then does that mean all controller classes have to be named xxxxxController?

like image 854
rkw Avatar asked Jul 20 '11 18:07

rkw


People also ask

What is the base class where is the controller is derived?

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."

What is controller name in MVC?

In ASP.NET MVC, every controller class name must end with a word "Controller". For example, the home page controller name must be HomeController , and for the student page, it must be the StudentController . Also, every controller class must be located in the Controller folder of the MVC folder structure.

What is the controller class?

A controller class is normally a class part of the Model View Controller (MVC) pattern. A controller basically controls the flow of the data. It controls the data flow into model object and updates the view whenever data changes.

How does MVC know which controller to use?

This is because of conventions. The default convention is /{controller}/{action} , where the action is optional and defaults to Index . So when you request /Scott , MVC's routing will go and look for a controller named ScottController , all because of conventions.


2 Answers

Yes you are right, all controllers need to follow the naming convention of an ending "Controller".

See the ControllerName property in the ASP.NET MVC code on CodePlex:

public virtual string ControllerName {
    get {
        string typeName = ControllerType.Name;
        if (typeName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
        {
            return typeName.Substring(0, typeName.Length - "Controller".Length);
        }
        return typeName;
    }
}

Anyhow, you could change the naming convention by using your own controller factory.

Hope that helps.

like image 66
Martin Buberl Avatar answered Oct 19 '22 23:10

Martin Buberl


Yes, this is a key aspect of the MVC framework called CoC, Convention over Configuration. The idea is that, as long as you are willing to follow the default conventions for things like class names, method names, folder structure, etc., you can minimize the amount of work you need to do for things to work. You only put in effort if you want to deviate from those conventions, which you certainly can do.

There are a number of such items in the MVC framework. In addition to the convention that all controllers are classes named XxxxController, there is the convention that all views are found in a folder named View\Xxxx\Yyyyy.cshtml.

like image 24
Michael Edenfield Avatar answered Oct 19 '22 23:10

Michael Edenfield