asp.net-mvc RedirectToAction 导致 ASP.NET MVC 3 中的“路由表中没有与提供的值匹配的路由”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7504255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:29:11  来源:igfitidea点击:

RedirectToAction Causes "No route in the route table matches the supplied values" in ASP.NET MVC 3

asp.net-mvcasp.net-mvc-3

提问by Schmalls

I have a project that I recently upgraded to ASP.NET MVC 3. On my local machine, everything works fine. When I deploy to the server, I get an error anytime I use a RedirectToActioncall. It throws a System.InvalidOperationExceptionwith the error message No route in the route table matches the supplied values.My assumption is that there is some configuration problem on the server, but I can't seem to be able to figure it out.

我有一个最近升级到 ASP.NET MVC 3 的项目。在我的本地机器上,一切正常。当我部署到服务器时,每次使用RedirectToAction调用时都会出错。它抛出System.InvalidOperationException带有错误消息的路由表中没有路由与提供的值匹配。我的假设是服务器上存在一些配置问题,但我似乎无法弄清楚。

采纳答案by Schmalls

I had a similar problem once with RedirectToActionand found out that you need a valid route registered that leads to that action.

我曾经遇到过类似的问题,RedirectToAction并发现您需要注册一个有效的路由来引导该操作。

回答by Al Stevens

I ran into this with areas within MVC3 when redirecting across areas. As others have said, Glimpse is very useful here.

我在跨区域重定向时遇到了 MVC3 中的区域。正如其他人所说,Glimpse 在这里非常有用。

The solution for me was to pass in the Area within the route values parameter changing:

我的解决方案是在路由值参数更改中传入区域:

return RedirectToAction("ActionName", "ControllerName");

to:

到:

return RedirectToAction("ActionName", "ControllerName", new { area = "AreaName" });

回答by Adam Tuliper - MSFT

Check out glimpse and see if you can get some route debugging information: http://getglimpse.com/

看一看,看看能不能得到一些路由调试信息:http: //getglmpse.com/

回答by John Kennedy

You could add a route table to your RouteConfig.cs file like below:

您可以将路由表添加到 RouteConfig.cs 文件中,如下所示:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapMvcAttributeRoutes();

        var namespaces = new[] { typeof(HomeController).Namespace };

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("name", "url", new { controller = "controllerName", action = "actionName" }, namespaces);
    }

NB: the "url" is what you'd type into the address bar say: localhost:/home

注意:“url”是您在地址栏中输入的内容:localhost:/home

After setting up the route, use RedirectToRoute("url").

设置好路由后,使用RedirectToRoute("url").

Or if you'd prefer the RedirectToAction()then you don't need to set up the above route, use the defaults. RedirectToAction(string action name, string controller name);

或者,如果您更喜欢,RedirectToAction()则不需要设置上述路由,请使用默认值。 RedirectToAction(string action name, string controller name);

I hope this helps.

我希望这有帮助。

回答by Abel

There's a difference with trailing slashes in routes not working with MVC 3.0. MVC 2.0 doesn't have a problem with them. I.e., if you change the following:

不使用 MVC 3.0 的路由中的尾部斜杠有所不同。MVC 2.0 对它们没有问题。即,如果您更改以下内容:

"{controller}.mvc/{action}/{id}/"

to:

到:

"{controller}.mvc/{action}/{id}"

it should fix this (from this thread, worked for me). Even when you use the upgrade wizard to move to MVC 3.0, this still throws InvalidOperationException. I'm not aware whether this is what Schmallswas talking about though.

它应该解决这个问题(从这个线程,对我来说有效)。即使您使用升级向导移动到 MVC 3.0,这仍然会抛出InvalidOperationException. 我不知道这是否是Schmalls所说的。