asp.net-mvc-4 - 子文件夹中的 Controller 和 View

标签 asp.net-mvc-4 directory-structure

我正在使用 ASP.NET MVC 4 开发一个 Web 应用程序,我想按以下方式组织我的 Controller 和 View :

/Controller    
    /Admin
        /LessonController.cs
        /ExerciseController.cs    
    HomeController.cs

/Views
        /Admin
              /Lesson
                   /Index.cshtml
        /Home
              /Index.cshtml

我尝试了以下代码来注册路由,但它不起作用:
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 }
            );

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

        }

你有什么建议吗?

最佳答案

你可以使用 areas ,它们是为这种类型的隔离设计的,会给你一些开箱即用的分离:

/Areas
     /Admin
         /Controllers
             /LessonController.cs
             /ExerciseController.cs    
         /Views
             /Lesson
                 /Index.cshtml
             /Exercise
                 /Index.cshtml
/Controllers    
    /HomeController.cs
/Views
    /Home
        /Index.cshtml

如果您不想遵循 ASP.NET MVC 支持的标准约定,则必须使用 write and register a custom view engine

关于asp.net-mvc-4 - 子文件夹中的 Controller 和 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14368427/

相关文章:

mysql - 处理上传的大量图像的最佳目录结构是什么?

javascript - jQuery ajax 响应绑定(bind)到 MVC 模型表

asp.net - 如何在OnActionExecuting中获取 Controller 和 Action 名称?

c# - 使用 MVC 模型显示只读文本

php - 在自定义 OO PHP 框架中使用接口(interface)

asp.net-mvc - 在 ASP.NET MVC 中,放置我的 Entity Framework DataContext 类的 "best practice"位置(文件夹)在哪里?

iPhone文档目录和UIFileSharingEnabled,隐藏某些文档

基于 Ant 构建的 Java 项目布局最佳实践

.net - 哪个带有MVC 4的.NET框架?

c# - FileLoadException 未被用户代码处理