【问题标题】:ASP.NET MVC default routh is wrongASP.NET MVC 默认路由错误
【发布时间】:2015-04-03 13:34:56
【问题描述】:

我有一个带有 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

【问题讨论】:

    标签: c# asp.net-mvc razor asp.net-mvc-routing


    【解决方案1】:

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

    调试器启动 url http://localhost:55815/Game/Game,因为文件 Game.cshtml 当前已在 Visual Studio 中打开。

    解决方案在您的 VisualStudio 项目配置中。 选择具有空值的特定页面,而不是当前/活动页面

    【讨论】:

    • 好的,我想到了一件事情——天哪!严肃的!我调试了几个小时! &%$#^@$#$#*%$(^(^% ... grrrrr 非常感谢!
    【解决方案2】:

    其实,

    /游戏/游戏

    /游戏/索引

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

    祝你好运

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      相关资源
      最近更新 更多