【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板

iDotNetSpace發表於2008-07-22
動態切換view來換主題
[views]
.|
.---[Controler1]
...|
...---[blue]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
...---[Red]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
.---[Controler2]
...|
...---[blue]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx
...---[Red]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx
這樣,主題view無法獨立出來,不利於分離主題,管理主題。
而我覺得,最理想的結構是
[views]
.|
.---[blue]
...|
...---[Controler1]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
...---[Controler2]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx
.---[Red]
...|
...---[Controler1]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
...---[Controler2]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx
但是如果用這種,預設的模板路徑是不支援的。
看了你的文章恍然大悟。
改一下ViewLocator的邏輯就可以了對吧。

在這裡簡單給大家說下如何實現,只要給大家提供一點思路。其實把框架研究透徹了,真的很多問題都迎刃而解。

大家如果有看過我的上篇文章 ASP.NET MVC : 實現我們自己的檢視引擎 ,其實就明白的了。

在這裡,首先我們需要一個配置檔案來配置站點要使用的模板(主題)。

Config/Site.config :

xml version="1.0" encoding="utf-8"?>
<site>
<appsetting ViewTheme="Default" />
site>

 

然後修改預設的ViewLocator  :


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtpublic class uLinkViewLocator : ViewLocator
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板    
{
        
public uLinkViewLocator()
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板        
{
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板            
base.ViewLocationFormats = new string[] { BaseviewPath + "/{1}/{0}.aspx",
                                                      BaseviewPath 
+ "/{1}/{0}.aspx",
                                                      BaseviewPath 
+ "/Shared/{0}.aspx",
                                                      BaseviewPath 
+ "/Shared/{0}.aspx",
                                                      
"~/Views/{1}/{0}.aspx",
                                                      
"~/Views/{1}/{0}.aspx",
                                                      
"~/Views/Shared/{0}.aspx",
                                                      
"~/Views/Shared/{0}.aspx"
            }
;

        }


        
private string _baseviewPath;
        
private string BaseviewPath
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板        
{
            
get
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板            
{
                
if (string.IsNullOrEmpty(_baseviewPath))
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板                
{
                    
string viewTheme = ConfigXml.Element("appsetting").Attribute("ViewTheme").Value;
                    viewTheme 
= string.IsNullOrEmpty(viewTheme) ? "Default" : viewTheme;
                    _baseviewPath = "~/Views/" + viewTheme;
                }

                
return _baseviewPath;
            }

        }


        
private XElement configXml;
        
private string path = System.Web.HttpRuntime.AppDomainAppPath + "/Config/Site.config";
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板        
/// 
        
/// 載入XML檔案
        
/// 

        private XElement ConfigXml
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板        
{
            
get
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板            
{
                
if (configXml == null)
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板                
{
                    configXml 
= XElement.Load(path);
                }

                
return configXml;
            }

        }

    }
 

還有修改ViewEngine的ViewLocator 為剛才建立的uLinkViewLocator :


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtpublic class uLinkViewEngine : WebFormViewEngine
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板    
{

        IViewLocator _viewLocator 
= null;

        
protected override void RenderView(ViewContext viewContext)
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板        
{
            
base.ViewLocator = this.ViewLocator;
            base.RenderView(viewContext);
        }


        
public IViewLocator ViewLocator
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板        
{
            
get
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板            
{
                
if (this._viewLocator == null)
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板                
{
                    
this._viewLocator = new uLinkViewLocator();
                }

                
return this._viewLocator;
            }

            
set
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板            
{
                
this._viewLocator = value;
            }

        }

    }
 

然後建立一個ControllerFactory繼承自預設的DefaultControllerFactory,以修改預設的controller中的ViewEngine為我們建立的uLinkViewEngine :


<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtpublic class uLinkControllerFactory : DefaultControllerFactory
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板    
{
        
protected override IController CreateController(RequestContext requestContext, string controllerName)
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板        
{
            Controller controller 
= (Controller)base.CreateController(requestContext, controllerName);
            controller.ViewEngine = new uLinkViewEngine();
//修改預設的檢視引擎為我們剛才建立的檢視引擎
            return controller;
        }


    }

在Global.asax.cs中的Application_Start 事件中註冊我們的 uLinkControllerFactory :

ControllerBuilder.Current.SetControllerFactory(typeof(uLinkControllerFactory));
然後在寫一個設定模板的Controller:
【Q.Lee.lulu】ASP.NET MVC: 修改ViewLocator來動態切換模板ThemeController

相關文章