3

我这样写了我的路线。

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

当我想RedirectToAction在同一个控制器中从一个动作使用到另一个动作时,它会重定向到其他地方。

例如,我希望它把我重定向到

Plugin/UserRegistration/Register/Register

但是,它把我送到

Admin/ECommerce/Register/Register

我不得不提到我的控制器在其他库中。

我的代码有什么问题?

更新:控制器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");
        }

这个我也试过

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

它不起作用。

4

1 回答 1

2

ASP.NET MVC 识别这两个路由,与 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 从上到下读取路由表以匹配请求的 Url 和 UriTemplate。因此,在这种情况下,它将选择第一个模板。

尝试改用RedirectToRoute

return RedirectToRoute("Registration", new 
{ 
    controller = "Registration", 
    action = "Register"
});
于 2017-01-25T09:21:29.943 回答