我正在使用 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/