2023年7月4日发(作者:)
实现4多级Views⽬录建⽴⾃⼰MyViewEngine类让他继承RazorViewEngine,之后在构造函数⾥⾯写⼊设置视图位置格式代码如下:public class MyViewEngine : RazorViewEngine{ public MyViewEngine() { ViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", "~/Views/UserInfo/{1}/{0}.cshtml",//UserInfo为Controllers控制器下的⽂件夹 //可以⼀直往下添 "~/Views/Admin/{1}/{0}.cshtml"//Admin为Controllers控制器下的⽂件夹 //注:{1}表⽰Controller的名称,{0}表⽰视图名称 }; } //public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) //{ // return ew(controllerContext, viewName, masterName, useCache); //}}进⼊默认RouteConfig类⾥⾯添加路由配置添加如下代码:public static void RegisterRoutes(RouteCollection routes){ Route("{resource}.axd/{*pathInfo}"); te( name: "UserInfo", url: "UserInfo/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = al } ); te( name: "Admin", url: "Admin/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = al } ); te( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = al } );}在Global下的Application_Start注册你添加你的CustomViewEngine类代码如下:public class MvcApplication : plication{ protected void Application_Start() { erAllAreas(); er(uration); erGlobalFilters(s); erRoutes(); erBundles(s); RegisterView();//注册视图访问规则 } protected void RegisterView() { ();//移除默认视图配置 (new Engine());//注册⾃⼰编写的视图格式 }}
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688420414a135729.html
评论列表(0条)