Category support in ASP.NET MVC?

For some time, I have been desiring a method for grouping a set of related Controllers into categories. A search for such a category style scheme showed me that, in the web MVC world, such a concept is known as areas, and I found a few examples of people implementing such features on top of ASP.NET MVC.


http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

http://blog.codeville.net/2008/11/05/app-areas-in-aspnet-mvc-take-2/


Both of these are the same implementation, with the former having little flexibility and being very fragile, with the latter adding flexibility at the expense of complexity, while still being as fragile. To add to that, neither solution supports discoverability (something I will get to with a later post).

My implementation will consist of adding the route name 'category' to the list of required route names, just like 'controller' and 'action'.  Also, the use of an attribute would declare the category of a controller.

[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=false)]

public abstract class CategoryAttribute : Attribute {}

This will allow one to inherit from CategoryAttribute and decorate their controller class with the corresponding CategoryAttribute.

public class MainCategoryAttribute : CategoryAttribute { }

 

[MainCategory]

public class MainController : Controller {

    public ActionResult Main() {

        return View();

    }

}

By defining the route using "{category}/{controller}/{action}/{id}" (note, in this example, {id} is not necessary), the following URL will activate the MainController.Main() method:

Main/Main/Main/

(I notice the lack of originality with naming the sections, and I apologize; I'm not the best at naming things, especially for example purposes.)

 

My next article will be more in depth about how this model actually works.

Print | posted on Friday, February 06, 2009 2:40 PM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)
 
Please add 2 and 4 and type the answer here: