3

I wrote my routes like this.

routes.MapRoute(
    name: "AdminECommerce",
    url: "Admin/ECommerce/{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional },
    namespaces: new []{ "AdminEcommerce.Controllers" }
);

routes.MapRoute(
    name: "ECommerce",
    url: "ECommerce/{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional },
    namespaces: new []{ "Ecommerce.Controllers" }
);

routes.MapRoute(
    name: "User",
    url: "Plugin/User/{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional },
    namespaces: new []{ "User.Controllers" }
);

routes.MapRoute(
    name: "UserRegistration",
    url: "Plugin/UserRegistration/{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional },
    namespaces: new []{ "UserRegistration.Controllers" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

When I want to use RedirectToAction from one Action to other Action in the same Controller It Redirects Somewhere else.

For Example, I want it to redirect me to

Plugin/UserRegistration/Register/Register

But,It Sends me to

Admin/ECommerce/Register/Register

I Have to mention that my controllers are in other libraries.

What is wrong with my code?

Update: Controller Registration

public ActionResult Register()
    {
        ViewBag.securityQuestions = _dataRepository.GetContext().User_SecurityQuestion;
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RegisterNewUser(User_User model, FormCollection collection)
    {

        try
        {
            // register new user 
              ...
            return RedirectToAction("Register");
        }
        catch
        {
            // making error messages 
              ...
            return RedirectToAction("Register");
        }

I also tried this,

return RedirectToAction("Register","Registration");

It doesn't work.

1 Answers1

2

ASP.NET MVC recognizes these two routes, as the same by UriTemplate

routes.MapRoute(
    name: "AdminECommerce",
    url: "Admin/ECommerce/{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional },
    namespaces: new []{ "AdminEcommerce.Controllers" }
);

routes.MapRoute(
    name: "UserRegistration",
    url: "Plugin/UserRegistration/{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional },
    namespaces: new []{ "UserRegistration.Controllers" }
);

MVC reads Route table from top to bottom to match requested Url with UriTemplate. So, in this case it will select the first template.

Try to use RedirectToRoute instead.

return RedirectToRoute("Registration", new 
{ 
    controller = "Registration", 
    action = "Register"
});
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • 2
    for first paramter I replaced "Registration" with "UserRegistration" and now it works well, Thanks. – Ali Vahidinasab Jan 25 '17 at 09:49
  • Glad to have been of help :) – Roman Marusyk Jan 25 '17 at 09:58
  • I can confirm that this is true. I had the same problem. The easiest way was to not have the same names for Action methods and Class names. But anyway, i hope that this will be improved somehow if possible. :/ The second option is to use a redirect to url instead of redirectToAction like this: `Redirect("http://www.google.com/")`. – tedi Nov 09 '17 at 09:42