为什么返回重定向到操作( "Index" , "Home" );如果我有一个具有索引操作的家庭控制器,则不起作用?



我有以下控制器:

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

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

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

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

更多信息在这里

相关内容

最新更新