Finding and handling views
SEO Extensions optimizes the way ASP.NET MVC finds and handles the views for controllers by a sort of expansion over the
original support for areas, introduced with ASP.NET MVC 2. In fact, this feature takes at a higher level the structure of the folders
which host the MVC views for your application and, instead of having them named by every controller like in traditional
ASP.NET MVC, it reflects the structure of your applications and requires the views to be put into a folder structure that reflects
the namespaces of your code. Of course, this lead to a much cleaner application structure and to a better organization of your views,
especially in the event of a high number of items.
By default, whenever the SEO Extensions engine need to find a view for your MVC application it extracts the namespace of the calling
controller, splitting it by the dot symbol and considering every part as a subfolder of the standard
Views
folder.
Next, it takes the name of the controller without the standard
Controller
suffix - this part being similar
to the way views are traditionally found - and treats it as a subfolder. Finally, it tries to find the view file in that folder, using the
traditional fallback rules.
For example, if your controller lays on the
WebSite.Controllers.FooBar
namespace and is named
BazController
, SEO Extensions would try
to find its views on the
~/Views/WebSite/Controllers/FooBar/Baz
folder.
Root namespace prefix
While such a folder structure is highly desirable in real world MVC web applications, having it reflect every single part of the namespaces
of the controllers may lead to an overly complexity, especially if all of the controllers share a single namespace prefix. For this,
SEO Extensions allows to configure a
namespace prefix that should be stripped from the namespaces whenever the engine needs to find a
view: to configure this namespace prefix, you need to pass its value to the aforementioned
RegisterRoutes
static method of the
DeclarativeRouteRegistration
class.
In the following block of code, for example, the SEO Extensions routing manager is configured with the root namespace prefix of the aforementioned
BazController
type (not included in the sample); this way, whenever the system needs to locate views for
the BazController type it would look in the
~/Views/FooBar/Baz
folder.
public class MyApplication : System.Web.HttpApplication
{
// ...
public static void RegisterRoutes(RouteCollection routes)
{
// SEO routing model registration, with a root namespace prefix
DeclarativeRouteRegistration.RegisterRoutes("WebSite.Controllers");
// ...
}
// ...
}