0

我有以下控制器:

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

1 回答 1

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

我现在无法测试,但我想你可能会明白

更多信息在这里

于 2013-10-12T21:02:16.383 回答