具有多个区域的MVC3应用程序上的InvalidRouteException

时间:2013-12-09 12:36:54

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

我有一个MVC3 Web应用程序,其中包含一个默认区域和一个管理区域。登录后,两者都有他们赢得的主页。成功登录到应用程序后,如果我尝试访问管理区域,我将获得InvalidRouteException。

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

访问管理区域时

http://localhost/admin

我得到以下例外。

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

{controller} / {action} / {id} {controller:'admin';行动:'LogOn'}   描述:执行期间发生未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:LoginSystem.Web.Exceptions.InvalidRouteException:   {controller} / {action} / {id} {controller:'admin';行动:'LogOn'}

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

这样的网址
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 donot包含Return URL。我应该怎么做才能包含returnurl。

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

请建议。

1 个答案:

答案 0 :(得分:0)

读取就像你没有配置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的路由。第二行说,寻找一个看起来像的网址:

http://localhost/admin/

采取适当的“家庭”行动。

您的管理员控制器应该以:

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

然后在处理您的授权时,您需要调整LogOn的[Http]帖子上的帐户控制器,特别是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]帖子:

 [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。