mvc custom route gives 404

混江龙づ霸主 提交于 2019-12-12 03:57:19

问题


I am trying to make a custom route but I cannot get it working and even though everything seems okay it always returns 404.

So here are the route defined.

It is defined first before the default and according to route debugger this is the route that gets hit.(Matched Route: Game/{id}/{title})

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

Here is the path Im trying to reach: /Game/5/test

And this is the Controller declaration. The GamesController is placed in the Controllers folder and its view are in Views/Games/showGames.cshtml.

public GamesController()
{
}

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

The DefaultMvcRouteHandler doesnt do anything fancy.

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

The default route works without problems, and I have tried everything I can find like changing the name of the route so it doesnt match any folders or anything like that. If anyone have any ideas on what more to try I would be most grateful.


回答1:


As per my comment you are passing incorrect default route values for the controller and action values.

Update your route like so:

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


来源:https://stackoverflow.com/questions/15532493/mvc-custom-route-gives-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!