views:

24

answers:

2

I'm building my first Asp.Net MVC2 Site, and i'm now trying to add an /Admin area to the site.

I don't want this area to be visibile to the main set of users so will only be accessible when you enter http://Intranet/Admin

What I have is a NewsController for my regular users but I also want an Admin NewsController and I'm not sure how to setup the Class hierarchy and folders so that when I add the Views they are in the correct location.

Inside my Global.Asax.cs I've added and the routes resolve correctly.

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new string[] { "Intranet.Controllers" } 
);

routes.MapRoute(
    "Admin", // Route name
    "Admin/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Admin", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
      new string[] { "Intranet.Controllers.Admin" } 
);

And in the folder hierarchy I've setup

Views/
     Admin/
     News/
         ...I want the new view to go here...

In the Controllers

Controllers/
    Admin/
        AdminController.cs
        NewsController.cs (this is the one i want for administration)
    NewsController.cs (this is the regular one for viewing the list, specific item etc)

The problem I face is when I go into the admin/NewsController.cs on Index and Add View it tries to create it at the /News/Index.aspx rather than /Admin/News/Index.aspx.

This is the code for my admin news controller Controllers/Admin->Add->Controller

namespace Intranet.Controllers.Admin
{
    public class NewsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

Is there something I'm doing incorrectly, or what should I change so that when I add the views they are being created in the /Admin/{area} directory.

A: 

When you try to create a View for the existing Controller Action it always create on the root folder of the Views. The default route for the View is always pointing to the root of the Views folder.

For example:

 Controllers
     Admin
         AdminController.cs
         HomeController.cs
     HomeController.cs

In that hierarchy, both of the HomeController inside Admin and root shares the same Views in the Views Folder.

Views
    Home
        Index.aspx

Unless you return a specified View() in all of the ActionResults in your HomeController inside the Admin Folder of your Controllers. It will map to a certain View.

Example, ActionResult inside the HomeController.cs of Admin folder in Controllers.

namespace Intranet.Controllers.Admin
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View("Home/Index");
        } 
    }
}

This will be mapped in the Views folder like this

Views
    Admin
        Home
            Index.aspx

But if you do not specify the View path when you return a View in your ActionResult it will map to the default location of the Views which is like this.

Views
    Home
        Index.aspx

The reason for this is that even though you specify the routes in the Global.asax, that is only to map to which controller the url should point, not the Views folder.

When you right click and Create View on ActionResult of any Sublevels of the Controllers, it always create on the root of the Views folder to its corresponding Controller.

rob waminal
A: 

Since you're using MVC2, the easiest way to solve this is the create an actual MVC "Area" for your Admin section. Right now you're doing everything in the default section and just using an Admin folder. If you create an Admin area (under the well-known location Areas) folder, then you'll have an AdminAreaRegistration - where is where you'll configure your Admin routes. Because you'll do this as part of the Area, then the first segment of the URL "/Admin" will be used for the "area" token. This will disambiguate which controller to use and correctly pick up the controller you want. So you're folder structure will be:

/Areas
    /Admin
        /Controllers
            NewsController.cs
etc.
Steve Michelotti
Thanks. It took a little to get my head around the additional Controller steps to separate the 2 areas but all good now
Paul Farry