RedirectToAction 使用其他名称空间重定向错误 asp.net mvc
RedirectToAction redirects wrong with Other namespaces asp.net mvc
我的路线是这样写的
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");
没用。
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 从上到下读取路由 table 以匹配请求的 Url 与 UriTemplate。因此,在这种情况下,它将 select 第一个模板。
尝试使用 RedirectToRoute。
return RedirectToRoute("Registration", new
{
controller = "Registration",
action = "Register"
});
我的路线是这样写的
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");
没用。
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 从上到下读取路由 table 以匹配请求的 Url 与 UriTemplate。因此,在这种情况下,它将 select 第一个模板。
尝试使用 RedirectToRoute。
return RedirectToRoute("Registration", new
{
controller = "Registration",
action = "Register"
});