如何更改 ASP.NET Core MVC 中的默认页面?

How to change the default page in ASP.NET Core MVC?

我想更改 ASP.NET Core 中的默认页面。而不是 home/index,我想 return 从 GameController 使用此默认路由的 Index 视图:

GuessingGame/{controller=Game}/{action=Index}/{id?}

我这样更改了启动设置。

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:9149/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchUrl": "GuessingGame",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000/GuessingGame/Game",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "assignment1": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000/GuessingGame/Game",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

并在启动时添加了这几行代码class:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(name: "GuessingGame", pattern: "GuessingGame/{controller=Game}/{action=Index}/{id?}");
    endpoints.MapControllerRoute(name: "Doctor", pattern: "{controller=Doctor}/{action=CheckFever}/{id?}");
    endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});

但不幸的是,它仍然 return 是 DoctorControllerIndex 视图。

您应该在 UseEndpoints 上修改您的默认路由 this naming convention

app.UseEndpoints(endpoints =>
{
    //the default route
    endpoints.MapControllerRoute(name: "default", pattern: "GuessingGame/{controller=Game}/{action=Index}/{id?}");
 
    //other routes
    endpoints.MapControllerRoute(name: "GuessingGame", pattern: "GuessingGame/{controller=Game}/{action=Index}/{id?}");
    endpoints.MapControllerRoute(name: "Doctor", pattern: "{controller=Doctor}/{action=CheckFever}/{id?}");
    endpoints.MapControllerRoute(name: "Home", pattern: "{controller=Home}/{action=Index}/{id?}");
});

删除 {controller=Game}...

之前的 GuessingGame/
endpoints.MapControllerRoute(name: "GuessingGame", pattern: "{controller=Game}/{action=Index}/{id?}");

太奇怪了,我花了两个小时来处理它。 我删除了 .vs 文件夹,因为我得到了 'HTTP Error 500.35 - ANCM Multiple In-Process Applications ' 错误并重建了我的项目并刚刚添加了 '"launchUrl": "GuessingGame",'in launchsetting.json 。 现在可以正常使用了。