ASP.NET MVC routing and areas

Dan Appleyard picture Dan Appleyard · Oct 25, 2009 · Viewed 13.5k times · Source

I am messing around with ASP.NET MVC 2 Preview 2 and am trying to figure out how routing works with areas and such. In a single project implementation of areas, I want an area named "admin".

I am trying to be able to have urls like this:

(root)/admin/apples/search
(root)/admin/apples/edit/3
(root)/admin/apples/add
(root)/admin/oranges/search
(root)/admin/oranges/edit/5
(root)/admin/oranges/add
(root)/admin

I have the area created. I have the controllers created with their respective views, but it is the routing that I can't seem to get. Any advice as to how I would achieve such routing?

I am sure this may be extremely simple for some, but I haven't had too much luck in finding examples that go beyond the basic stuff.

Thanks!

Addition to the Question (10/25/2009) I am basically asking what routes and in what order would I set up in the Area's AreaRegistration class? I have done everything mentioned so far, but with no results.

Answer

Bcelik picture Bcelik · Oct 25, 2009

Register areas in single project

You have to add routes.cs file to the admin area folder.

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAreasSingleProject.Areas.Admin
{
    public class Routes : AreaRegistration
    {
        public override string AreaName
        {
            get { return "admin"; }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "admin_default",
                "admin/{controller}/{action}/{id}",
                new { controller = "Admin", action = "Edit", id = "" }
            );
        }
    }
}