3

While reading this question today and making same mistake in past myself, I wondered if it's required (due to default convention?) that we MUST suffix "Controller" to a class name that is derived from Controller class in ASP.NET MVC, why compliler does not complain at all? At least a warning would save someone's time.

So below code will not work:

public class Search : Controller
{
    // GET: /Search/
    public ActionResult List()
    {
        var model = new MyModel();
        return View(model);
    }
}

So my question are:

  1. Can we change this convention i.e. can I change setting somewhere and make suffix text 'MvcController' instead?
  2. More important one, What is the reason C# compiler doesn't complain? It can not figure it out or it is not desired/illogical?

The question and answer does not answer both of my question so I think it is not exact duplicate.

Community
  • 1
  • 1
SBirthare
  • 5,117
  • 4
  • 34
  • 59
  • http://haacked.com/archive/2012/07/25/finding-bad-controllers.aspx/ – Claies Jun 25 '15 at 08:28
  • The compiler does not complain because its a valid name for a class. –  Jun 25 '15 at 08:28
  • @StephenMuecke - Agreed, but it's not a normal class. It is being derived from Controller which makes it different than a normal class. – SBirthare Jun 25 '15 at 08:29
  • 1
    Of course it's a normal class! It's just a convention built into a framework made up of other normal classes. – BenjaminPaul Jun 25 '15 at 08:30

2 Answers2

6

The question you link to points out exactly why the "Controller" suffix is the default convention, as explained in Why do MVC controllers have to have the trailing 'Controller' convention on their class name?:

Imagine having a ProductController that likely handles Product application model entity instances. By not having the controller naming convention, we'd have two types with the same name hence would always have to provide namespaces to distinguish between the two.

To answer your questions:

Can we change this convention i.e. can I change setting somewhere and make suffix text 'MvcController' instead?

Yes, by building your own IControllerFactory. See Adding a controller factory to ASP MVC.

What is the reason C# compiler doesn't complain? It can not figure it out or it is not desired/illogical?

The naming convention is just an MVC construct. MVC is a framework that runs on top of .NET, nothing special about it.

The compiler can't figure this out, as there is no trivial way to expose runtime requirements that need to be checked at compile-time, like "If a class inherits from System.Web.Mvc.Controller or Microsoft.AspNet.Mvc.Controller, its name must end in 'Controller', unless there's a non-default ControllerFactory registered, in that case, use the conventions from that factory, whatever they are".

MVC relies on reflection to inspect types (and their names) at runtime. The C# compiler works at compile-time.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thank you. You explained it very well. My doubt is clear. – SBirthare Jun 25 '15 at 08:44
  • Also by definition a compiler checks only if the grammar of the language is respected and as long as your code respect the C# specifications, the compiler does not complain (and must never). – LeBaptiste Jun 25 '15 at 08:45
0

Unless you are under MVC 6, you need to name your controllers as follows

 public class NameController:Controller{}

a controller must

  • be a public class
  • inherit from a controller or from a base class that inherits from controller class
  • not be abstract class
  • not be subclass
  • be named in a word ending in "Controller"
Bellash
  • 7,560
  • 6
  • 53
  • 86