c# - RedirectToAction 重定向错误与其他 namespace asp.net mvc

标签 c# asp.net-mvc routetable

我的路线是这样写的。

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

当我想在同一个 Controller 中使用 RedirectToAction 从一个 Action 到另一个 Action 时,它会重定向到其他地方。

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

Plugin/UserRegistration/Register/Register

但是,它把我送到了

Admin/ECommerce/Register/Register

我不得不提一下,我的 Controller 在其他库中。

我的代码有什么问题?

更新: Controller 注册

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 从上到下读取路由表,将请求的 Url 与 UriTemplate 进行匹配。因此,在这种情况下,它将选择第一个模板。

尝试使用RedirectToRoute相反。

return RedirectToRoute("Registration", new 
{ 
    controller = "Registration", 
    action = "Register"
});

关于c# - RedirectToAction 重定向错误与其他 namespace asp.net mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41847071/

相关文章:

asp.net-mvc - 在 ASP.NET MVC 和 IIS7 中记录原始 HTTP 请求/响应

amazon-web-services - 了解 AWS 路由表 - 无法创建更具体的路由

c# - 继承 IComparable<T> 的接口(interface)引用对象的排序列表

c# - DependencyInjection : Initialize class, 在被调用之前(必需)?

c# - 如何使用参数值对 Controller 使用 Custom AuthorizeAttribute?

asp.net-mvc - ASP.NET MVC : How to validate an Ajax form with a specified UpdateTargetID?

amazon-web-services - terraform aws - 如何将默认路由添加到 vpc 默认路由表?

amazon-web-services - 了解路由表

c# - 替换 C# 字符串中的数字

c# - 为什么后续调用时不能使用IntPtr