English 中文(简体)
ASP.NET MVC 2 Beta single-project Area registration getting HTTP 404
原标题:

I m trying to get ASP.NET MVC 2 single-project area registration to work. Tried with Preview 2 and now with Beta version with no luck. I used the "Add area" dialog to create a "NewsModule" area. Created a NewsModuleController inside it and an Index view for it.

The route registration for this Area looks like this:

 context.MapRoute(
                "NewsModule_default",
                "NewsModule/{action}/{id}",
                new { action = "Index", id = "", controller = "NewsModule", area = "NewsModule" }
            );

I added the AreaRegistration.RegisterAllAreas(); call to my Global.asax. Accessing http://localhost/mymvcproj/NewsModule gets a HTTP 404 error.

Using Phil Haack s route debugger, I could confirm that the route is being correctly mapped and catched by this URL, however, it seems like the framework is not being able to locate the Area files, maybe?

Anyone can help?

Thanks, Felipe

最佳回答

Problem solved. Here is what I did to solve:

AreaRegistration.cs file:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "NewsModule_default",
        "NewsModule/{controller}/{action}/{id}",
        new { controller = "NewsModule", action = "Index", id = "" });
}

IMPORTANT: Add the ".Areas." to the namespace (Namespace.Areas.ControllerName).

Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // must be called RIGHT AFTER IgnoreRoute()
    AreaRegistration.RegisterAllAreas();

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

    //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
问题回答

Try removing "area = "NewsModule" from the route registration in the Area/NewsModule folder. So it appears like,

context.MapRoute(
                "NewsModule_default",
                "NewsModule/{action}/{id}",
                new { action = "Index", id = "", controller = "NewsModule"}
            );

Here is my Account Area Routes Registration,

Location /Areas/Account/

public class AccountAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get { return "Account"; }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Account_default",
                "Account/{controller}/{action}/{id}",
                new { controller = "", action = "", id = "" }
            );
        }
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签