Kelb56 Kelb56 - 3 months ago 16
ASP.NET (C#) Question

RedirectToAction problems in mvc

RedirectToAction method doesn't work, can't solve it for the past 4 hours. Here is the code from the 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");
}


It says that there is no route registered like that, although it adds users to the database, so only redirect does not work. This is my routeConfig file:

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

Answer

Because Home is not an action, it's route. You should use RedirectToRoute.

return RedirectToRoute("Home");