zoukankan      html  css  js  c++  java
  • View Engine 视图引擎(二) Kevin

    开始之前,先补习一下params关键字的用法:

    params

    params 关键字可以指定在参数数目可变处采用参数的方法参数。

    1. 在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字

    首先自定义View

     1  public class DebugView:IView
     2     {
     3         public void Render(ViewContext viewContext, System.IO.TextWriter writer)
     4         {
     5             Write(writer, "-----RouteData------");
     6             foreach (string key in viewContext.RouteData.Values.Keys)
     7             {
     8                 Write(writer, "Key:{0} Value:{1}", key, viewContext.RouteData.Values[key]);
     9             }
    10 
    11             Write(writer,"--------View Data--------")
    12             foreach(string key in viewContext.ViewData.Keys)
    13             {
    14                 Write(writer,"Key:{0},Value:{1}",key,viewContext.ViewData[key]);
    15             }
    16         }
    17 
    18         private void Write(TextWriter writer, string template, params object[] values)
    19         {
    20             writer.Write(string.Format(template, values) + "<p/>");
    21         }
    22     }

    然后,创建视图引擎

     1 public class DebugViewEngine:IViewEngine
     2     {
     3         public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
     4         {
     5             return new ViewEngineResult(new string[] {"Debug Data View Engine" });
     6         }
     7 
     8         public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
     9         {
    10             if (viewName == "DebugData")
    11             {
    12                 return new ViewEngineResult(new DebugView(), this);
    13             }
    14             else
    15             {
    16                 return new ViewEngineResult(new string[] {"Debug Data View Engine" });
    17             }
    18         }
    19 
    20         public void ReleaseView(ControllerContext controllerContext, IView view)
    21         {
    22             throw new NotImplementedException();
    23         }
    24     }

    最后,注册该视图引擎:

    1 protected void Application_Start() {
    2 AreaRegistration.RegisterAllAreas();
    3 ViewEngines.Engines.Add(new DebugDataViewEngine());
    4 RegisterGlobalFilters(GlobalFilters.Filters);
    5 RegisterRoutes(RouteTable.Routes);
    6 }

    这里说一句,同一个应用程序中可以支持多个视图引擎,如果有多个视图引擎支持同一个视图,将按照视图引擎在集合众的顺序进行显示,并且只显示第一个。所以,如果你想优先使用某个视图引擎,最好的方法是将她放到第一个位置。

    ViewEngines.Engines.Insert(0, new DebugDataViewEngine());

    接下来就可以进行测试了:

    创建一个HomeController,写入如下代码:

     1  public class HomeController : Controller
     2     {
     3         //
     4         // GET: /Home/
     5 
     6         public ViewResult Index()
     7         {
     8             ViewData["Message"] = "Hello New View Engine";
     9             ViewData["Time"] = DateTime.Now.ToShortDateString();
    10             return View("DebugData");
    11         }
    12 
    13     }

    运行,查看运行后的结果:

    到这里,我们的自定义视图就创建完了,下一篇将会讲解如何利用自带的Razor视图。

  • 相关阅读:
    公用导航栏的根据url控制选中导航js
    页面切换出动晃动解决
    redis五大数据类型
    redis简介
    Linux安装redis
    各种锁的理解
    原子引用
    理解CAS
    彻底玩转单例模式
    Volatile
  • 原文地址:https://www.cnblogs.com/kfx2007/p/2999277.html
Copyright © 2011-2022 走看看