本帖最后由 李维强-15级 于 2019-6-23 13:36 编辑  
 
可以参考https://www.cnblogs.com/genesis/p/5220879.html 
 
实现控制器里面有子目录,view里面也有子目录。如下图所示 
 
 
控制器代码 
 
 
-------------------------------------------------------- 
正常情况下,如果单独建立这种目录然后直接去访问的话,肯定有问题,那么需要按照如下方式设置 
 
首先需要重新配置路由, 
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode     public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            //路由规则匹配是从上到下的,优先匹配的路由一定要写在最上面。因为路由匹配成功以后,他不会继续匹配下去。
            routes.MapRoute(
                name: "LayUI",
                url: "LayUI/{controller}/{action}/{id}",
                defaults: new { Controller = "Admin", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "WxPage", action = "Index", id = UrlParameter.Optional }
            );
        }
    } 
然后配置了路由过后,发现也不能访问, 
因为,默认视图也是有自己的调用路径的,如下: 
        Views/{1}/{0}.cshtml 
        Views/Shared/{0}.cshtml 
其中{1}表示Controller的名称,{0}表示视图名称,Shared是存放模板页的文件夹。一看就很清楚了。这个就是寻找视图的规则,所以我们存放在"Views/LayUI/Admin/Index.cshtml的存放规则就不满足。 
那么下面就需要修改这个默认的view路径,也就是重写它,并且保留原来的那些东西 
那么我建立一个自己的类,继承RazorViewEngine方法 
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WuYuHotel.Tools
{
    public sealed class LayUIViewEngine:RazorViewEngine
    {
        public  LayUIViewEngine()
        {
            ViewLocationFormats = new[]
            {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml",
                //以上两个是默认视图引擎路径
                "~/Views/LayUI/{1}/{0}.cshtml"//我们的规则
            };
        }
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }
    }
} 
然后需要再global里面去注册一下 
 
 
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using WuYuHotel.WX_AccToken;
using System.Threading;
using WuYuHotel.Tools;
namespace WuYuHotel
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            RegisterView();
        }
        protected void RegisterView()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new LayUIViewEngine());
        }
    }
} 
 
然后就可以访问到了~~~ 
 
 
 |