mvc中的RedirectToAction问题

时间:2016-04-19 12:29:52

标签: asp.net asp.net-mvc

RedirectToAction方法不起作用,过去4小时无法解决。以下是User controler的代码:

[HttpPost, ValidateAntiForgeryToken]
public ActionResult Register(UsersNew form)
{
    var user= new User();
    UserDB db = new UserDB();
    db.addUser(form.Username, form.Password, form.SchoolName, form.Country, form.City);
    return RedirectToAction("Home");
}

它表示没有这样的路由注册,虽然它将用户添加到数据库,因此只有重定向不起作用。这是我的routeConfig文件:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute("Home", "", new { controller = "First", action = "FirstView" });
    routes.MapRoute("Register", "register", new { Controller = "Users", action = "Register" });
    routes.MapRoute("About", "about", new { Controller = "About", action = "About" });
}

1 个答案:

答案 0 :(得分:2)

因为Home不是动作,所以它的路线。您应该使用RedirectToRoute

return RedirectToRoute("Home");