ASP.NET MVC 实现多模版的方法

需要解决的场景:

 

不用的场景下使用不用的mvc 模版。

 

目录结构希望是

/templates

     Default

     Blue

     Red 

     .....

 

当传入 “Blue” 参数的时候则调用 Blue 下面的View

 

查看 return View().. 的参数,发现可以指定 一个 IView 。

然后就有如下的代码。

            RazorView rv = new RazorView(this.ControllerContext, "~/tempate/Blue/???.cshtml", null, true, new string[] { ".cshtml", ".vbcshtml" });

            return View(rv); 

 

然后,就解决了路径的问题。

 

执行,出错,不是正确的页面文件。

 

这个错误解决方法很简单(但也浪费了我不少时间):

把 /Views 下面的 web.config 拷贝到 /templates 下 

 

 

更好一点的解决方法:

 

自己实现一个 IView。

 

  1 using System;

 2  using System.IO;
 3  using System.Web.Compilation;
 4  using System.Web.Mvc;
 5  using System.Web.WebPages;
 6 
 7  namespace System.Web.Mvc
 8 {
 9      public  class ThemeRazorView : IView
10     {
11          private  string template;   //模版的目录名称 
12 
13          public ThemeRazorView( string _template)
14         {
15              this.template = _template;
16         }
17 
18          public  void Render(ViewContext viewContext, TextWriter writer)
19         {
20              // 这个地方可以自己实现,或者从 web.config 里读取,或者从用户选择的模版中读取
21               if (template.IsNullOrEmpty())
22             {
23                 template =  " Default ";
24             }
25              string viewPath =  " ~/templates/ " + template +  " / " + viewContext.RouteData.GetRequiredString( " controller ") +  " / " + viewContext.RouteData.GetRequiredString( " action ") +  " .cshtml ";
26 
27             Type viewType = BuildManager.GetCompiledType(viewPath);
28              var page = Activator.CreateInstance(viewType)  as WebViewPage;
29            
30             page.VirtualPath = viewPath;
31             page.ViewContext = viewContext;
32             page.ViewData = viewContext.ViewData;
33             page.InitHelpers();
34 
35             WebPageContext pageContext =  new WebPageContext(viewContext.HttpContext,  nullnull);
36             WebPageRenderingBase startPage = StartPage.GetStartPage(page,  " _ViewStart "new  string[] {  " cshtml "" vbhtml " });
37 
38             page.ExecutePageHierarchy(pageContext, writer, startPage);
39         }
40 
41     }
42 }

 

 

完成。

 

 现在调用方法为:

            return View(new ThemeRazorView("Blue")); 

 


 

 参考了:http://www.cnblogs.com/artech/archive/2012/04/10/how-mvc-works.html  

转载于:https://www.cnblogs.com/cloudbeer/archive/2012/09/26/2703772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值