asp.net-mvc - Mvc区域路由?

标签 asp.net-mvc url-routing area

区域文件夹如下所示:

Areas 
    Admin
        Controllers
            UserController
            BranchController
            AdminHomeController

项目目录如下所示:

Controller
    UserController
        GetAllUsers

区域路线登记

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

项目路线注册

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

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new string[] { "MyApp.Areas.Admin.Controllers" });
}

当我这样路由时:http://mydomain.com/User/GetAllUsers 我收到资源未找到错误(404)。将 UserController 添加到 Area 后出现此错误。

如何修复此错误?

谢谢...

最佳答案

你弄乱了你的 Controller 命名空间。

您的主要路线定义应该是:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new string[] { "MyApp.Controllers" }
);

您的管理区域路由注册应该是:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new { controller = "Branch|AdminHome|User" },
        new[] { "MyApp.Areas.Admin.Controllers" }
    );
}

注意应如何使用正确的命名空间。

关于asp.net-mvc - Mvc区域路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15616240/

相关文章:

ios - 如何计算有机形状的面积?

asp.net-mvc - 从 ASP.NET MVC 5 中的 Facebook v2.4 API 访问 OAuth ExternalLoginCallback 中的电子邮件地址

javascript - 创建 2 个图表,在一张图表中显示 2 个不同的数据 (asp.net MVC)

asp.net-mvc - ASP.NET MVC URL 路由

.net-core - 更改.Net Core 3.1中的请求路径

python - webapp2 CRUD 路由返回 405

MATLAB 填充线之间的区域

c++ - 检索元素大小的函数

asp.net-mvc - 带有 IoC 的存储库模式 - 它应该如何正确使用?

css - 编写 MVC 3 应用程序时的表单设计方法