ASP.NET 核心 2.2 MVC MapRoute 项目到 ASP.NET 3.0 端点 MapAreaControllerRoute "This localhost page can't be found"

ASP.NET Core 2.2 MVC MapRoute project to ASP.NET 3.0 endpoints MapAreaControllerRoute "This localhost page can't be found"

我们正在将应用程序从 ASP.NET Core 2.2 项目升级到使用 EndPoint 路由的 ASP.NET 3.0。我卡在了 "No webpage was found" 的以下问题上。我相信我对 ASP.NET 3.0 应用了正确的更改,并且尝试了各种更改。

这里是asp.net核心2.2代码区

startup.cs


        public void ConfigureServices(IServiceCollection services)
        ......
            //Asp.net Core 2.2
            services.AddRouting();
            //AddMvc
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);


            .AddViewLocalization(
            LanguageViewLocationExpanderFormat.Suffix, options => { options.ResourcesPath = "Areas\Resources"; })
            .AddDataAnnotationsLocalization();

       public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        .....
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
                //
                routes.MapRoute(name: "Navigation", template: "Navigation/{controller}/{action}/{id?}";

我们用 :

装饰我们的控制器
    namespace Areas.Navigation.Controllers
    {
        [Area("Navigation")]
        [Route("[area]/[controller]/[action]")]

        public class NavigationHomeController : Controller
       {.......

这是我们更改的 asp.net 3.0 代码区域

startup.cs

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

           app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                //Endpoints Start
                endpoints.MapAreaControllerRoute(areaName: "Navigation", name: "Navigation", pattern: "Navigation/{controller}/{action}/{id?}");

我的 Razor 页面 包括以下内容:

    <a asp-area="Navigation" asp-controller="NavigationHome" asp-action="IndexAsync">Navigation</a>

我遇到以下问题

通过使用 Route 属性装饰您的控制器

[Area("Navigation")]
[Route("[area]/[controller]/[action]")] //<-- attribute routing
public class NavigationHomeController : Controller {
    //...

控制器现在使用属性路由,而不是基于约定的路由。

但是,您没有在配置中启用属性路由。

In the following example:

  • MapControllers adds support for attribute-routed controllers.
  • MapAreaControllerRoute adds a conventional route for controllers in an area.
  • MapControllerRoute adds a conventional route for controllers.
//...

app.UseEndpoints(endpoints => {
    //Attribute routing
    endpoints.MapControllers();

    //Area convention-based routing
    endpoints.MapAreaControllerRoute(
        areaName: "Navigation", 
        name: "Navigation", 
        pattern: "Navigation/{controller}/{action}/{id?}");

    //Convention-based routing
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

//...

这样做是为了避免在将请求与注册路由匹配时发生路由冲突。

在处理基于约定的路由时,注册顺序也会影响路由 table 解释请求的方式。

因此,请确保先注册特定路由,然后再注册更通用的路由。区域也应该在一般路线之前注册。

这个问题的解决方案是审查"Migrate from ASP.NET Core 2.2 to 3.0"这最初是完成的,但忽略了本文档中的建议。

我的项目由 130 个其他子项目组成。为了解决我的问题,我执行了以下操作:

  1. Review "Migrate from ASP.NET Core 2.2 to 3.0" again.

  2. 删除 Startup 中对 MapRoute 的所有 Core 2.2 引用。例如:

    routes.MapRoute(name: "Navigation", template:     
                    Navigation/{controller}/{action}/{id?}";
    
  3. 使用"How to define an endpoint Route to multiple Areas"
    的建议 在 Startup 中,代码如下所示:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapAreaControllerRoute(
                name: "areas",
                areaName: "areas",
                pattern: "{area}/{controller}/{action}/{id?}"
                );
    
            endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");});
    
  4. 最后的关键项目,直到我们修复和更新我们的代码。 "Async suffix removal from controller action names"

    services.AddMvc(options =>options.SuppressAsyncSuffixInActionNames = false);
    

    这是启动中遗漏的小细节,如 "Migrate from ASP.NET Core 2.2 to 3.0"

  5. 中所述
  6. 进行更改后,应用程序按预期运行。