.NET MVC 实现动态换版

         .NET 开发项目非常的方便,但是动态换版功能,需要自己去实现,因为项目需要用到,快速换不同的版,对于.Net里面 UrlRewrite + Nvelocity 实现的MVC,换版,非常简单。只在此谈NET MVC 2.0 - 3.0(2.0-3.0 虽然模板引擎有所区别,但是方法同样适用) 实现动态换版功能.

关于动态换版,.net mvc 实现的方式非常多,一下为第一个例子

首先我们来看看我们需要首先的目录:

 如何实现 换版我们需要查看的是MVC RazorViewEngine 可一看到一下受保护的方法

我们不需要实现一个模板引擎 只是改一下路径就行

 1  // 摘要:
 2         //     Creates a partial view using the specified controller context and partial
 3         //     path.
 4         //
 5         // 参数:
 6         //   controllerContext:
 7         //     The controller context.
 8         //
 9         //   partialPath:
10         //     The path to the partial view.
11         //
12         // 返回结果:
13         //     The partial view.
14         protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath);
15         //
16         // 摘要:
17         //     Creates a view by using the specified controller context and the paths of
18         //     the view and master view.
19         //
20         // 参数:
21         //   controllerContext:
22         //     The controller context.
23         //
24         //   viewPath:
25         //     The path to the view.
26         //
27         //   masterPath:
28         //     The path to the master view.
29         //
30         // 返回结果:
31         //     The view.
32         protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath);

   这两个方法受保护,他们的功能给所需要的查找的模板路径,返回一个继承IView 的对象

因为方法受保护 无法在外面调用  我们写一个公开的方法 调用即可。


代码如下

 1 namespace System.Web.Mvc
 2 {
 3     public class CupsViewEngin : RazorViewEngine
 4     {
 5         /// <summary>
 6         /// 返回IView 借口对象RazorView,WebFormView
 7         /// </summary>
 8         /// <param name="controllerContext">controllerContext 实例对象</param>
 9         /// <param name="viewRoot">自定义 模板根路径对象,用于枚举 前台,后台,配置,模块,4个视图根文件夹</param>
10         /// <param name="themes">当前使用的主题,对应文件夹的名称</param>
11         /// <param name="ControllerName">控制器名称</param>
12         /// <param name="viewName">视图名称</param>
13         /// <returns>返回IView</returns>
14         public IView GetsIView(ControllerContext controllerContext, CupsRoot viewRoot, string themes, string ControllerName, string viewName)
15         {
16             if (string.IsNullOrEmpty(themes) || string.IsNullOrEmpty(viewName) || controllerContext == null || string.IsNullOrEmpty(ControllerName))
17                 new ArgumentNullException();
18             return base.CreateView(controllerContext, string.Format("~/Views/{0}/{1}/{2}/{3}.cshtml", viewRoot, themes, ControllerName, viewName), null);
19         }
20         public IView GetsPartial(ControllerContext controllerContext, CupsRoot viewRoot, string themes, string viewName)
21         {
22             if (string.IsNullOrEmpty(themes) || string.IsNullOrEmpty(viewName) || controllerContext == null)
23                 new ArgumentNullException();
24             return base.CreatePartialView(controllerContext, string.Format("~/Views/{0}/{1}/{2}.cshtml", viewRoot, themes, viewName));
25         }
26     }
27 }
 1 namespace System.Web.Mvc
 2 {
 3     public enum CupsRoot
 4     {
 5         admin,
 6         Cups,
 7         Profile,
 8         Module
 9     }
10 }

   我们的类CupsViewEngin 继承RazorViewEngine  什么也没做只是添加了两个方法用于公开调用CreateView(),CreatePartialView()两个方法

 

下一步工作,就是我们在 应用程序开始时将我们的模板引擎 添加到  ViewEngines里

 

修改Global.asax 文件

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Routing;
 7 
 8 namespace Cups
 9 {
10     // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
11     // visit http://go.microsoft.com/?LinkId=9394801
12 
13     public class MvcApplication : System.Web.HttpApplication
14     {
15         public static void RegisterGlobalFilters(GlobalFilterCollection filters)
16         {
17             filters.Add(new HandleErrorAttribute());
18         }
19 
20         public static void RegisterRoutes(RouteCollection routes)
21         {
22             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
23 
24             routes.MapRoute(
25                 "Default", // Route name
26                 "{controller}/{action}/{id}", // URL with parameters
27                 new { controller = "Login", action = "Login", id = UrlParameter.Optional } // Parameter defaults
28                 , new { controller = @"^[A-Za-z_][1-9a-zA-Z_]*$", action = @"^[A-Za-z_][1-9a-zA-Z_]*$" }
29             );
30 
31         }
32 
33         protected void Application_Start()
34         {
35             AreaRegistration.RegisterAllAreas();
36 
37             RegisterGlobalFilters(GlobalFilters.Filters);
38             RegisterRoutes(RouteTable.Routes);
39 
40             ViewEngines.Engines.Clear();//清理模板引擎(这个地方可以不做清理)
/// 将我们的模板引擎添加到集合内
41 ViewEngines.Engines.Add(new CupsViewEngin());//注册模板引擎 42 } 43 } 44 }

 
添加controller 基类 Base 代码

 

using System.Linq;
using System.Web.Mvc;
using Cups.Models;

namespace Cups.Controllers
{
    public abstract class Base : Controller
    {
        protected CupsEntities db;
        //定义一个模板引用 CupsViewEngin类
        protected CupsViewEngin CupsViewEngin;
        #region 构造函数
        public Base(ref CupsEntities db)
        {
            this.db = db;
            //在基类Controller 实例化时从模板引擎中取出CupsViewEngin
            CupsViewEngin = (CupsViewEngin)ViewEngines.Engines.Last();
        }
        public Base()
        {

            this.db = new CupsEntities();
            CupsViewEngin = (CupsViewEngin)ViewEngines.Engines.Last();
        }
        #endregion
        protected ViewResult View(string viewName, CupsRoot Root, string Skin, object model)
        {
            return base.View(CupsViewEngin.GetsIView(this.ControllerContext,
                Root,
                Skin,
                this.GetType().Name.Replace("Controller", ""),
                viewName),
                model);
        }
        protected ViewResult MView(string viewName, CupsRoot Root, string Skin, object model)
        {
            return base.View(CupsViewEngin.GetsPartial(this.ControllerContext,
                           Root,
                           Skin,
                           viewName),
                           model);
        }
        ~Base()
        {
            db.Dispose();
        }

    }
}

我们写controller  只需继承Base  

然后调用 Base 的View方法。当然在我的项目里 Base 下面还分两个 前台 基类 和后台基类

以上就是mvc 实现动态换版的 介绍

转载请注明出处 http://www.cnblogs.com/Arrays

 

原文地址:https://www.cnblogs.com/Arrays/p/2686879.html