【问题标题】:InvalidRouteException on MVC3 application with multiple areas具有多个区域的 MVC3 应用程序上的 InvalidRouteException
【发布时间】:2013-12-26 13:50:19
【问题描述】:

我有一个带有一个默认区域和一个管理区域的 MVC3 Web 应用程序。登录后两者都有自己的主页。成功登录应用程序后,如果我尝试访问管理区域,我会收到 InvalidRouteException。

场景就像我成功登录应用程序一样。当我尝试使用

访问管理区域时

http://localhost/admin

我得到以下异常。

“/”应用程序中的服务器错误。

{controller}/{action}/{id} { 控制器:'admin';行动:“登录”} 说明:执行过程中发生未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:LoginSystem.Web.Exceptions.InvalidRouteException: {controller}/{action}/{id} { 控制器:'admin';操作:“登录”}

但是,如果我注销然后使用像

这样的 URL
http://localhost/admin

成功进入登录页面。

请提出建议。

下面是我的消费区 RegisterArea 函数。

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Consumer_default",
                "Consumer/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }, new[] { "MyPortal.Web.Areas.Consumer.Controllers" }
            );

            context.MapRoute(
            "Admin", // Route name
            "Admin/{action}/{id}", // URL with parameters
            new { controller = "Admin", action = "Index", id = UrlParameter.Optional }, new[] { "MyPortal.Web.Controllers" }// Parameter defaults
        );
       }

更多信息 - 更新 1 注册“管理员”路由后,我可以移动到登录页面。 我的应用程序有 2 个区域 Client 和 ADmin(这是默认区域。)这两个区域都经过身份验证。 现在的情况是我登录到客户端模块。当我登录时,我做到了

http://localhost/admin

并到达登录页面。但问题是现在 url 不包含返回 URL。我应该怎么做才能包含returnurl。

在正常情况下,当我们未登录时,登录页面会打开,并在其 url 中添加返回 url。

请提出建议。

【问题讨论】:

  • 如果已经登录,LogOn 操作中是否有重定向?
  • 没有这样的东西。添加更多,上面提到的url甚至没有点击action方法登录。
  • 似乎如果登录,则 url 以其他方式解析,如果未登录,则 url 以其他方式解析,并以某种方式映射到操作方法登录。我是 mvc 新手,所以我不知道 url 是如何映射到操作方法的。以及应用程序如何知道它已登录。?
  • 未登录时是否有来自localhost/admin 的重定向离开

标签: asp.net-mvc-3 exception login asp.net-mvc-areas


【解决方案1】:

看起来您没有正确配置 global.asax.cs 和 AccountController.cs。前几天我遇到了类似的问题,我花了一点时间才想起我正在对我的路由进行自定义。我有一个与您的非常相似的本地主机地址的管理部分。这是我所做的:

在您的 global.asax.cs 文件中,添加以下内容:

 routes.MapRoute(
            "Admin", // Route name
            "Admin/{action}/{id}", // URL with parameters
            new { controller = "Admin", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

上面描述了一个名为 admin 的路由。第二行,说要寻找一个看起来像这样的 url:

http://localhost/admin/

使用适当的“home”动作。

您的管理控制器应以:

 //
    // GET: /Admin/
    [Authorize]
    public ViewResult Index()
    {
        return View(db.Tutorial.ToList());
    }

然后在处理您的授权时,您需要调整 LogOn 的 [Http] 帖子上的 Account 控制器,特别是将 RedirectToAction 调整为:

[HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Admin");
                }

另一个需要调整的地方是[Http] post:

 [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index","Admin");
            }

当然,我假设您是在谈论默认的安全/成员资格提供程序。

HTH。

【讨论】:

  • 我已经添加了你描述的新路线。但仍然有同样的错误。
  • @user3056239 没有你所拥有的完整代码示例,我在黑暗中行走。
  • 如何为您提供更多详细信息?请告诉我。
  • @user3056239 我昨天意识到您可能不需要在 global.asax.cs 中添加自定义地图路径。只需提供一些代码示例即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多