设置区域路由后,在登录和LogOff方法上使用404

时间:2014-11-28 12:11:38

标签: asp.net asp.net-mvc asp.net-mvc-routing

我有一个使用ASP Identity 2和Fluentsecurity的MVC5应用程序,在Home控制器中使用以下方法处理登录页面并注销:

public ActionResult Login(string returnUrl)
    {
        this.ViewBag.ReturnUrl = returnUrl;
        return this.View();
    }

public ActionResult LogOff()
    {
        this.AuthenticationManager.SignOut();
        return this.RedirectToAction("Login", "Home");
    }

这绝对没问题。

我现在需要在我的应用程序中添加一个区域并相应地设置路由。我在“Areas”文件夹下添加了一个名为“Admin”的子文件夹,并将相关的控制器和视图放在相应的子文件夹中。

然后我在Global.asax中设置了如下路由:

private static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "AppName.Web.Controllers " });

        routes.MapRoute(
            "Admin",
            "Admin/{controller}/{action}/{querydata}",
            new { querydata = UrlParameter.Optional },
        new[] { "AppName.Web.Areas.Admin.Controllers" });

    }

然后调用如下:

private void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        SecurityConfigurator.Configure(SecurityConfig.Configure);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        FilterConfig.RegisterGlobalApiFilters(GlobalConfiguration.Configuration.Filters);

        RegisterRoutes(RouteTable.Routes);
        ControllerBuilder.Current.DefaultNamespaces.Add("AppName.Web.Areas.Admin.Controllers");

    }

如果我然后运行应用程序,路由在我的区域和主应用程序中按预期工作,除了它似乎打破了上述Home控制器中的Login和LogOff方法,现在在尝试访问时出现404错误他们。同一家庭控制器的索引方法工作正常。

如果它与Fluentsecurity相关,则这就是安全性的配置方式:

internal static void Configure(ConfigurationExpression configuration)
    {
        configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
        configuration.GetRolesFrom(GetRoles);

        configuration.For<HomeController>(x => x.Login(default(string))).DenyAuthenticatedAccess();
        configuration.For<HomeController>(x => x.Index()).DenyAnonymousAccess();
        configuration.For<HomeController>(x => x.LogOff()).AllowAny();

        //Admin Home
        configuration.For<Areas.Admin.Controllers.HomeController>(x => x.Index()).DenyAnonymousAccess();

    }

我可能出错的任何想法?

1 个答案:

答案 0 :(得分:-2)

private static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Admin",
            "Admin/{controller}/{action}/{querydata}",
            new { querydata = UrlParameter.Optional },
        new[] { "AppName.Web.Areas.Admin.Controllers" });

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "AppName.Web.Controllers " });



    }