ASP.NET MVC 默认路由错误

ASP.NET MVC default routh is wrong

我得到了带有 1 个默认路由的简单 Web 应用程序:

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

我的控制器包含以下操作:

public class GameController : Controller
{   
    public ActionResult Index()
    {
        // some actions            
        return View();
    }                     

    [HttpPost]
    public ActionResult CreateGame(Game game, User user)
    {
        // some actions
            return View("Game");                      
    }

    [HttpPost]
    public ActionResult JoinGame(User user)
    {
        // some actions
        return View("Game");                        
    }
}

也在 Views/Game 文件夹下,我获得了 "Index" 和 "Game" 的观看次数。 但是当我不时(不总是!)开始申请时,它要求

http://localhost:55815/Game/Game 

而不是

http://localhost:55815 or http://localhost:55815/Game/Index

您的应用程序默认路由工作正常。

调试器启动 url http://localhost:55815/Game/Game 因为文件 Game.cshtml 当前在您的 Visual Studio.

中打开

解决方案在您的 VisualStudio 项目配置上。 选择具有空值的特定页面而不是 current/active 页面

实际上,

/Game/Game

/Game/Index

两者相同。当您检查默认路由文件时,您可以看到您的主控制器是用预定义的控制器和操作编写的。所以程序会根据那个自动解析这个url。大多数情况下,不要在 visual studio 调试中尝试您的 Web 应用程序,而是将其放在 IIS 下,如果您需要调试,请调试 IIS 实例。

祝你好运