3

I'm having some issues getting Areas working correctly within MVC 3. I have the following folder structure and an Admin area set up:

enter image description here

I'm trying to navigate from the admin page (Index) to the the other view pages in the Admin area for example Admin/Floor/Create etc... but I get The resource cannot be found error on every url combination i've tried for example:

  • @Html.ActionLink("floors", "Index", "Floor", new { area = "Admin" }, null)
  • /Floor/Index/
  • /Admin/Floor/Index/

None of which work. I managed to use the first ActionLink one to link to the admin index page from outside of the area but it's no use here.

The area registration looks like this:

context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

Can anyone offer some help?

Thankyou

dtsg
  • 4,408
  • 4
  • 30
  • 40
  • I know it's silly, but did you rebuild the project after you add a new controller? In you routing everything looks ok, you don't need any default controller. I could repeat the error only if I added a new controller with view and didn't build the project. – Maciej Rogoziński Aug 06 '12 at 15:10
  • I'll give rebuilding a shot but i doubt that's it – dtsg Aug 06 '12 at 15:11
  • 1
    Is this what you want to achieve? https://dl.dropbox.com/u/1171045/MvcApplication7.zip – Maciej Rogoziński Aug 06 '12 at 15:21
  • Yes that's exactly it. Looking at what you have there it's basically identical to what I have, I am totally lost as to why this won't work... – dtsg Aug 06 '12 at 15:25
  • Do you have AreaRegistration.RegisterAllAreas(); in global.asax MvcApplication.Application_Start() method? Did you create Area using wizard or manually? – Maciej Rogoziński Aug 06 '12 at 15:30
  • `RegisterAllAreas` is under `app_start`, have tried putting it in `RegisterRoutes` before my default route is initialized and still no luck. I added the area by right clicking solution > area > add. I didn't know there was a wizard? – dtsg Aug 06 '12 at 15:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14956/discussion-between-maciej-rogozinski-and-duane) – Maciej Rogoziński Aug 06 '12 at 15:34
  • I feel *insanely* stupid! I originally had the controllers under the namespace `TheHeights.Controllers` but just copied them over to the /Admin/Controllers/ folder without updating the namespaces to `TheHeights.Areas.Admin.Controllers` doh! Everything works correctly now, thankyou for your help! – dtsg Aug 06 '12 at 15:40

1 Answers1

0

The problem is with your routing. You need to set the default controller to be AdminController:

context.MapRoute( 
    "Admin_default", 
    "Admin/{controller}/{action}/{id}", 
    new { controller = "Admin", action = "Index", id = UrlParameter.Optional } 
); 

If you don't specify this, MVC doesn't know quite what you're looking for and actually expects you to navigate to /admin/admin in order to display the initial view. So change the routing as I've mentioned above and then use this action link to get to FloorController.Create():

@Html.ActionLink("floors", "create", "floor", new { area = "admin" }, null)

To expand a little, with your routing setup this way, your URLs will look like this:

/admin // Executes AdminController.Index()
/admin/floor // Executes FloorController.Index()

Update

Area Layout

Having downloaded Maciej Rogoziński's project, this gives me the same problem that your project currently has. The link from the default action is linking to /admin/admin/, which as I mentioned earlier, is what your project is looking for because no default controller has been specified for the area routing (this also applies to Maciej's project). Specifying the default controller allows you to navigate to /admin, which results in AdminController.Index() being invoked. Without specifying that controller, you can only retrieve this view from routing to /admin/admin, which again, is what Maciej's application is doing.

John H
  • 14,422
  • 4
  • 41
  • 74
  • 1
    Thanks for the suggestion. I've already tried setting the default controller to be `Admin`, this results in the same issue, have tried with your `ActionLink` and still it can't find the right view? – dtsg Aug 06 '12 at 15:08
  • @Duane I have a test project setup here which is working correctly. I'll update my answer with some screenshots so you can see how everything is laid out. – John H Aug 06 '12 at 15:10
  • @Duane Sorry for the slow update. I had to answer the phone. – John H Aug 06 '12 at 15:41
  • @JohnH but when you use @Html.ActionLink("floors", "create", "floor", new { area = "admin" }, null) you speficy a controller, mainly floor so this should not be a problem – Maciej Rogoziński Aug 06 '12 at 15:42
  • 1
    Hi @John H, no problem. I actually managed to solve this, it was a simple case of not updating the namespace after moving folders. I blame mondays! – dtsg Aug 06 '12 at 15:42
  • @MaciejRogoziński I agree but that wasn't what the question initially stated: `I'm trying to navigate from the admin page (Index) to the the other view pages in the Admin area`. That says to me that Duane was trying to navigate from `/admin` and being unable to. Without specifying the default controller, navigating to `/admin` won't invoke `AdminController.Index()` and in this case, will result in a 404. – John H Aug 06 '12 at 15:44