简体   繁体   English

来自区域中控制器的ASP.NET MVC [Authorize]在根文件夹中找不到Account / Login ActionResult

[英]ASP.NET MVC [Authorize] from controller in area can't find Account/Login ActionResult in root folder

I'm trying to setup admin logins for my MVC site. 我正在尝试为我的MVC网站设置管理员登录名。 I have an area called Admin with a controller called ControlPanel . 我有一个名为Admin的区域,其中有一个名为ControlPanel的控制器。 I've placed the [Authorize] attribute above the ControlPanel Controller declaration. 我已经将[Authorize]属性放在ControlPanel Controller声明上方。 When I make a call to the index of that controller I get an error saying 当我调用该控制器的索引时,出现错误提示

it can't find the Account/Login resource. 它找不到帐户/登录资源。

The AccountController is stored in the root's 'Controllers' folder. AccountController存储在根目录的“ Controllers”文件夹中。 The view exists in the root's 'Views' folder. 该视图位于根目录的“视图”文件夹中。

My AdminAreaRegistration class: 我的AdminAreaRegistration类:

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "{area}/{controller}/{action}/{id}",
            new { area="Admin", action = "Index", id = UrlParameter.Optional }
        );
    }
}

My RouteConfig class: 我的RouteConfig类:

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

        routes.MapRoute(
            name: "Default",
            url: "{area}/{controller}/{action}/{id}",
            defaults: new { area = "", controller = "Startup", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Thanks in advance 提前致谢

Your route seems to be messed up. 您的路线似乎混乱了。 Try the code below to configure your routing. 请尝试以下代码来配置您的路由。

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { area="Admin", action = "Index", id = UrlParameter.Optional }
    );
}


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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new {controller = "Startup", action = "Index", id = UrlParameter.Optional }
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.Net MVC实体框架的帐户控制器中传递来自登录方法的ID? - How can I pass the Id from Login method in Account Controller in ASP.Net MVC Entity Framework? Asp.net Mvc 5,两个帐户控制器和一个区域,但重定向到错误的登录页面 - Asp.net Mvc 5, two account controller and Area but redirect to wrong login page 登录后仍无法使用[Authorize(Roles =“ Admin”)]访问ASP.NET MVC控制器方法 - can not get access to the ASP.NET MVC controller method with [Authorize(Roles = “Admin”)] even after login Asp.Net MVC5项目管理区域路由-错误路由控制器ActionResult - Asp.Net MVC5 Project Admin Area Route - Error Routing Controller ActionResult ASP.Net MVC 找不到未包含在控制器中的命名空间 - ASP.Net MVC can't find a namespace that is not included in the controller ASP.NET MVC 找不到我的控制器方法 - ASP.NET MVC can't find my controller method 在ASP.Net MVC 4中授权管理控制器 - Authorize Admin Controller in ASP.Net MVC 4 ASP.NET MVC:从区域到根的RouteLink抛出异常 - ASP.NET MVC: RouteLink from Area to Root throw Exception 具有异步任务的单元测试ASP.NET MVC 5控制器<ActionResult> - Unit test ASP.NET MVC 5 Controller with async Task<ActionResult> 可以在ASP.NET MVC 2中的区域级别进行[授权]吗? - Possible to [Authorize] at the Area level in ASP.NET MVC 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM