Specifically, does a controller class name have to have the Controller suffix, and can you change the folder structure in your project if you want to, without breaking things?
Are there other conventions that can be overridden, and how?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Most of the conventions are malleable provided you know how the framework operates. Let’s tackle two of the biggest conventions:
the “{controller}/{action}/” magic keywords for instantiating controllers from a route
the way the framework searches for Views first in the controller directory, and then in the Shared directory.
Every route that you create is associated with an instance of an
MvcRouteHandlerobject by default. When the route is matched, that handler is invoked to deal with the incoming request. Here’s what the MvcHandler’s ProcessRequest looks like:Notice the hardcoded string “controller”. Well, you can replace this handler for any route you’d like if you want to code your own controller-finding logic. Simply do something like this (shameless blog plug):
Now when the route is matched, it invokes your own logic, and you can do whatever you please. Incidentally, the reflection that is used to find the XXXXController class with the “Controller” suffix is part of the
DefaultControllerFactoryobject, invoked in the handler above, and this factory is replaceable.So, controller picking is one convention that’s overridable. What about when it looks for Views when you do a “
return View()” from any controller method? Well here’s the constructor for theWebFormViewEngine, the default view engine of the framework:So if you didn’t like the convention of looking in the controller directory, and then shared – you could easily extend
WebFormViewEngine(or use an entirely different view engine) and plop it in your global.asax:One of the amazing things about the MVC framework is how flexible it really is. You can replace almost any part of it with your own logic – and all the code is available to see what they’ve done.