Default-Route for root of application

穿精又带淫゛_ 提交于 2019-12-05 14:44:45

When debugging http://localhost:54500/ should route to http://localhost:54500/Home/Index.

Actually, the way you have it configured, http://localhost:54500/ will route to the HomeController.Index method, not another URL.

The view 'Index' or its master was not found or no view engine supports the searched locations

This error indicates that routing succeeded, but the controller returned the path of a view that does not exist.

Since you also mentioned you are using an Area and have posted your configuration, it is clear what is happening. Your config is run in this order:

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

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

So, if you pass the URL http://localhost:54500/, the Area route will miss (because it doesn't start with /Home) and it will match the Root route. This Root route does not route to your Area. There are 2 ways to fix this.

Option 1 - Add the Root Route to the Home Area

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Root",
        "",
        new { controller = "Home", action = "Index" }
        );

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

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

Option 2 - Set the DataToken to indicate the Home Area

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

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    ).DataTokens["area"] = "Home";

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

In Core 1.0.1 you can do this in Startup.cs:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc(routes =>
            {
                routes.MapRoute(name: "areaRoute",
                 template: "{area:exists}/{controller=Home}/{action=Index}");

                routes.MapRoute(
                    name: "default",
                   // template: "{controller=Home}/{action=Index}");
                   template: "{area=MyArea}/{controller=Home}/{action=Index}");
            });
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!