首页 新闻 赞助 找找看

关于MVC5 自定义模板路问题

0
悬赏园豆:20 [已解决问题] 解决于 2017-01-04 12:16

现在的问题是:View中Layout不能正常使用。

 

下面直接帖代码:

1:先自定义一个Attribute

public class SupportSkinAttribute : Attribute
    {
    }

2:重写模板路径:

public class CustomViewEngineHelper
    {
        internal static string[] AddNewLocationFormats(IEnumerable<string> defaultLocationFormats, IEnumerable<string> newLocationFormats)
        {
            List<string> allItems = new List<string>(newLocationFormats);
            foreach (string s in defaultLocationFormats)
            {
                allItems.Add(s);
            }
            return allItems.ToArray();
        }
        
    }
复制代码
public class CustomRazorViewEngine : RazorViewEngine
    {
        public CustomRazorViewEngine() { 

        }
        //查找视图文件
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            string[] mastersLocation = new[] {
                string.Format("~/template/{0}/_Layout.cshtml", controllerContext.RouteData.Values["siteDir"]) };
            MasterLocationFormats = CustomViewEngineHelper.AddNewLocationFormats(
                new List<string>(MasterLocationFormats),
                mastersLocation);
            string[] viewsLocation = new[] {
                string.Format("~/template/{0}/{{0}}.cshtml", controllerContext.RouteData.Values["siteDir"])};
            //视图文件位置路径的格式
            ViewLocationFormats =
                PartialViewLocationFormats =
                CustomViewEngineHelper.AddNewLocationFormats(new List<string>(ViewLocationFormats), viewsLocation);return base.FindView(controllerContext, viewName, masterName, useCache);
        }
    }
复制代码

然后 在控制器上打上[SupportSkin] 标签,这样所有的Action就都会从指定的目录查找对应的视图。

但是现在碰到一个问题,不管有没有在视图中指定Layout,就算指定Layout=null,实际解释后的Layout还是~/template/{0}/_Layout.cshtml这样的。

求解。。

[SupportSkin] 打在控制上,能不能排除某个action不起作用?

DZCMS的主页 DZCMS | 初学一级 | 园豆:5
提问于:2017-01-03 22:18
< >
分享
最佳答案
0
/// <summary>
    /// 跳过检查
    /// </summary>
    public class SkipAttribute : Attribute
    {
         
    }

action上打Skip标签

收获园豆:20
大师兄丶 | 小虾三级 |园豆:843 | 2017-01-03 22:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册