Areas, Routes and defaults in MVC 2 RC

by Christoph Menge in Software

Download sample

There has been some discussion over at stackoverflow on setting a default route to an area in mvc, so I want to post some very crude, but working example code here… I will write some explanatory text here if I find the time.

I feel that the fact that the order of operation in Application_Start() matters is quite problematic, but that has been discussed elsewhere (I’ll be searching for the link…) Anyway, when registering the areas first, the default route can be set to an area.

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

In the AreaRegistration,

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "ShopArea_default",
                "{controller}/{action}/{id}",
                new { action = "Index", id = "", controller = "MyRoute" },
                new { controller = "MyRoute" }
            );
        }

will do the job. Note the use of the constraint that will only accept requests to the areas’ controller that should be used as a default.

This is a working solution, but I think it is quite error-prone and not very elegant. In the meantime, I started to use MvcContrib, because it’s portable areas are really smart, make deployment easier and foster reusability of the code, and the views.

Post to Twitter Post to Delicious Post to Digg Post to Facebook

No related posts.

Tags: , ,

← Previous

Next →

1 Comment

  1. Hi Chris,

    an unrelated idea, since you touched route registration in the post: type safe registration, sample on my blog here: http://codevanced.net/post/Type-safe-route-registration-in-ASPNET-MVC.aspx

    I wonder what you think of it :)

Leave a Comment