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

让人想犯罪 __ 提交于 2019-12-14 04:27:19

问题


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

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?


回答1:


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.




回答2:


You were missing the controller part in your maproute




回答3:


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


来源:https://stackoverflow.com/questions/4500165/mvc-3-new-area-404-error-resource-not-found-have-tried-route-debugger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!