11

I have a small MVC 3 app - bit of a demo ground. I have one area and thats been working fine.

I have just added another area expecting to just spin up the app and it work - but no, 404 - The resource cannot be found.

The map route in the AreaRegistration is the default (as is the first area i created).

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Postcard_default",
            "Postcard/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

I have tried adding in a specific controller into this, but nothing.

So I downloaded Phil Haack's RouteDebugger and my route is found when typing in http://server/Postcard/Create (which is where I am trying to get too)

Structure of the Area

alt text

My controller

    public class CreateController : Controller
{
    private ILogger Logger { get; set; }
    private ICardSender Emailer { get; set; }
    private IOCCardRepository CardRepository { get; set; }

    public CreateController(ILogger logger, ICardSender cardSender, IOCCardRepository repository)
    {
        this.Logger = logger;
        this.Emailer = cardSender;
        this.CardRepository = repository;
    }


    //
    // GET: /Postcard/Create/

    public ActionResult Index()
    {
        var model = new OCPostcardModel().Create();

        return View(model);
    }

NOW: I have since deleted the entire area, tried again it didn't work. So I added in the specific controller in the route (Inside AreaRegistration file)

context.MapRoute(
            "Postcard_default",
            "Postcard/{controller}/{action}/{id}",
            new { controller = "Create", action = "Index", id = UrlParameter.Optional }
        );

And its working...I don't know why it didn't work when I did this before, but it is now.

Still curious though as I've not seen anyone add in this controller into route in any of the demo's i've looked at - and I haven't got it in my other area?

SteveCl
  • 4,529
  • 6
  • 29
  • 38
  • worth adding - this started as an MVC Preview 1 app, I have since installed MVC Preview 2 via the Platform installer – SteveCl Dec 21 '10 at 14:36
  • 1 more! I set a breakpoint on the RegisterArea method in my Postcard AreaRegistration - which is hit. I also set a break point on the constructor of the Controller (and the Index Action) neither are hit? – SteveCl Dec 21 '10 at 14:43
  • Please provide more information. What does your controller look like? – Haacked Dec 21 '10 at 21:57

3 Answers3

31

I ran into this when I moved a controller into an Area but forgot to update the namespace. The controller name is scoped to the Area's namespace. So "Some" in "Area" will map to App.Areas.Area.Controllers.SomeController, which didn't exist.

jamie
  • 2,963
  • 1
  • 26
  • 27
  • +1, If you implement areas you should go back and update ALL of your routes to include the namespaces you want the route isolated to. This would cause issues if you had a controller of the same name, like 'Home" in both the root and in an area. – Nick Bork Apr 20 '12 at 20:31
  • In my case, the solution was similar to the one proposed here: I copy/pasted the AreaRegistration, but forgot to update the namespace as well. – Bertvan Oct 21 '12 at 15:37
3

You were missing the controller part in your maproute

Tongayi
  • 31
  • 2
0

Try to add a class PostCardAreaRegistration under PostCard Area

using System.Web.Mvc;

namespace Areas.PostCard
{
    public class PostCardAreaRegistration: AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "PostCard";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "PostCard_default",
                "PostCard/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
Charles
  • 1,121
  • 19
  • 30
karthik kasubha
  • 392
  • 2
  • 13