SteveCl
SteveCl

Reputation: 4569

MVC 3 - New Area - 404 error - Resource not found - have tried route debugger

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?

Upvotes: 11

Views: 11051

Answers (3)

karthik kasubha
karthik kasubha

Reputation: 424

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 }
            );
        }
    }
}

Upvotes: 0

jamie
jamie

Reputation: 3043

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.

Upvotes: 31

Tongayi
Tongayi

Reputation: 31

You were missing the controller part in your maproute

Upvotes: 3

Related Questions