MVC默认区域不工作

本文关键字:工作 区域 默认 MVC | 更新日期: 2023-09-27 18:26:44

我有一个没有注册区域的网站。然后我注册了一个名为"MyNewArea"的区域。

现在我默认的网站链接,如博客等不再工作。

所以我现在有了一个areas文件夹,其中有一个单独的区域,以及我最初创建项目时的默认文件夹。

在我所在的地区,我有AreaRegistration班;

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

但这看起来与的默认值相冲突

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

我需要做什么才能让该区域与默认站点和控制器一起工作?

MVC默认区域不工作

你是对的,映射的路由会发生冲突(在"冲突"的意义上,它将首先匹配)。您需要更改您的地图区域路线,使其类似于:

        context.MapRoute(
            "MyArea_default",
            "MyArea/{controller}/{action}/{id}",
            new { controller = "MyAreaController", action = "Index", id = UrlParameter.Optional }
        );

添加此区域(以及区域路由)后,您的URL断开的原因是,它使用您的区域路由来处理MyArea区域中不存在的内容。

将新区域路由表更改为:

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