控制器和视图子文件夹中 [英] Controllers and views in subfolders

查看:137
本文介绍了控制器和视图子文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用ASP.NET MVC 4,我想整理我的控制器和视图以下列方式的Web应用程序:

I am developing a web application using ASP.NET MVC 4 and I would like to organize my controllers and views in the following way:

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

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

我尝试以下code登记的路线,但它不工作:

I tried the following code to register routes but it does not work:

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 }
                );

        }

你有什么意见?

推荐答案

您可以使用的 地区 ,他们是那种专为这种类型的隔离,并会给你一些分离的开箱即用的:

You could use areas, they are kind of designed for this type of segregation and will give you some sort of separation out of the box:

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

如果你不想遵循由ASP.NET MVC支持的标准约定,你将不得不 编写和注册自定义视图引擎

If you don't want to follow the standard conventions that are supported by ASP.NET MVC you will have to write and register a custom view engine.

这篇关于控制器和视图子文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆