1、编写继承自RazorViewEngine的类,重写视图路径
1 public class ViewEngine : RazorViewEngine 2 { 3 /// <summary> 4 /// Initializes a new instance of the <see cref="ViewEngine"/> class. 5 /// </summary> 6 public ViewEngine() 7 { 8 var views = new[] 9 { 10 "~/Views/{1}/{0}.cshtml", 11 "~/Views/Shared/{0}.cshtml", 12 "~/Views/Base/{0}.cshtml", 13 "~/Views/Base/{1}/{0}.cshtml", 14 }; 15 16 this.PartialViewLocationFormats = views; 17 18 this.ViewLocationFormats = views; 19 } 20 21 /// <summary> 22 /// 添加视图规则 23 /// </summary> 24 /// <param name="viewEngineCollection">viewEngineCollection</param> 25 internal static void RegisterView(ViewEngineCollection viewEngineCollection) 26 { 27 viewEngineCollection.Add(new ViewEngine()); 28 }
2、在Application_Start()中添加语句:
// 注册视图规则
ViewEngine.RegisterView(ViewEngines.Engines);
原文:http://www.cnblogs.com/liuxiaoji/p/4571995.html