【问题标题】:mvc custom route gives 404mvc 自定义路由给出 404
【发布时间】:2013-03-20 19:03:54
【问题描述】:

我正在尝试创建自定义路由,但无法使其正常工作,即使一切正常,它总是返回 404。

所以这里是定义的路线。

它在默认值之前首先定义,根据路由调试器,这是被命中的路由。(Matched Route: Game/{id}/{title})

routes.Add(
    "GamesDefault",
    new Route("Game/{id}/{title}",
    new RouteValueDictionary(new { controller = "Games", action = "ShowGame" }),
    new DefaultMvcRouteHandler(urlTranslator, urlFoundAction)));

这是我试图到达的路径:/Game/5/test

这是控制器声明。 GamesController 位于 Controllers 文件夹中,其视图位于 Views/Games/showGames.cshtml 中。

public GamesController()
{
}

public ActionResult ShowGames(int id, string title)
{
    return View(title);
}

DefaultMvcRouteHandler 没有做任何花哨的事情。

public class DefaultMvcRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MvcHandler(requestContext);
    }
}

默认路由没有问题,我已经尝试了我能找到的一切,比如更改路由的名称,使其与任何文件夹或类似的东西不匹配。 如果有人对更多尝试有任何想法,我将不胜感激。

【问题讨论】:

  • 您正在传递“Games”和“ShowGame”的默认路由值,但您的控制器称为“GamesController”,操作方法称为“ShowGames”。
  • omg,我知道它是这样的,但我就是找不到它,已经在这几个小时了。非常感谢

标签: c#-4.0 asp.net-mvc-4 routes


【解决方案1】:

根据我的评论,您传递的 controlleraction 值的默认路由值不正确。

像这样更新你的路线:

routes.Add(
   "GamesDefault",
   new Route("Game/{id}/{title}",
   new RouteValueDictionary(new { controller = "GamesController", action = "ShowGames" }),
   new DefaultMvcRouteHandler(urlTranslator, urlFoundAction)));

【讨论】:

    猜你喜欢
    • 2017-06-05
    • 1970-01-01
    • 2021-12-26
    • 2016-01-13
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多