为什么返回RedirectToAction(“Index”,“Home”);如果我有一个带有索引操作的Home控制器,则无法工作?

时间:2013-10-12 20:49:40

标签: asp.net-mvc asp.net-mvc-routing

我有以下控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

并且,路线:

routes.MapRoute(
    "spa",
    "{section}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { section = @"home|questions|admin" });

当我使用以下内容时,收到错误消息:

return RedirectToAction("Index", "Home");

错误讯息:

Server Error in '/' Application.

No route in the route table matches the supplied values.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.InvalidOperationException: No route in the route table matches the supplied values.

有人可以向我解释为什么这不起作用以及为什么以下有效:

return Redirect("~/home");

1 个答案:

答案 0 :(得分:3)

正如错误消息所示,没有匹配的路由,因为您拥有的路径不期望控制器和操作作为参数。您需要添加像这样的路线图

routes.MapRoute(
        "spa",
        "{controller}/{action}/{section}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { section = @"home|questions|admin" });

或者像这样

routes.MapRoute(
        "spa",
        "Home/Index/{section}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { section = @"home|questions|admin" });

我现在无法测试,但我认为你可能会得到这个想法

更多信息here