MVC自定义路由给404 [英] mvc custom route gives 404

查看:357
本文介绍了MVC自定义路由给404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个定制的路线,但我不能让它工作,即使一切似乎都还好,它总是返回404。

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.

这是第一次定义的默认之前,并根据路线调试器这是被击中的路线(匹配的路由:游戏/ {ID} / {标题})。

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)));

下面是路径我试着去达到: /游戏/ 5 /测试

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

这是控制器声明。在 GamesController 放置在控制器文件夹视图在查看/游戏/ showGames.cshtml

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);
}

该DefaultMvcRouteHandler没有做任何幻想。

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.

推荐答案

按我的评论您传递不正确的缺省路由值的控制器行动值。

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

更新,像这样的路线:

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

这篇关于MVC自定义路由给404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆