原文:返璞归真 asp.net mvc (4) - View/ViewEngine

[索引页]

[源码下载]

返璞归真 asp.net mvc (4) - View/ViewEngine

作者:webabcd





介绍

asp.net mvc 之 View 和 ViewEngine

  • ViewData 和 TempData 都可以向 View 传递数据,其中 TempData 是保存在 Session 中的,一次请求后此 Session 会被清除
  • HtmlHelper - 在 View 中显示 HTML 元素的一个帮助类
  • IViewEngine - 自定义的视图引擎需要实现此接口
  • VirtualPathProviderViewEngine - 实现了 IViewEngine 接口的抽象类,实现了根据指定的路径格式搜索对应的页面文件的功能(内用缓存机制)
  • IView - 只有一个需要实现的方法,就是呈现 HTML 结果

示例

1、演示 View 的 Demo

ViewDemoController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax; using MVC.Models; namespace MVC.Controllers
{
    public class ViewDemoController : Controller
    {
        ProductSystem ps = new ProductSystem();         public ActionResult Details(int id)
        {
            var product = ps.GetProduct(id);             if (product == null)
            {
                return View("NotFound");
            }
            else
            {
                product.CategoriesReference.Load();                 // 编辑 Product 的时候需要在一个 DropDownList 中选择其所对应的 Category, 所以这里要构造一个 SelectList 类型的 ViewData
                if (product.Categories == null)
                    ViewData["CategoryList"] = new SelectList(new CategeorySystem().GetCategory(), "CategoryId", "CategoryName");
                else
                    ViewData["CategoryList"] = new SelectList(new CategeorySystem().GetCategory(), "CategoryId", "CategoryName", product.Categories.CategoryID);                 // ViewData 和 TempData 都可以向 View 传递数据,其中 TempData 是保存在 Session 中的,一次请求后此 Session 会被清除
                // 在 View 中使用的时候,ViewData[key] 或 TempData[key] 即可
                TempData["Temp"] = "TempData";                 return View("Details", product);
            }
        }         [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(int id, FormCollection formValues)
        {
            var product = ps.GetProduct(id);             // 可以通过 UpdateModel, 让系统自动为属性赋值(通过反射的方式,取得对象的属性名称,然后和 Request 的 key 做匹配,匹配成功的则赋值)
            UpdateModel<Products>(product);             // 通过以下的方式让 UpdateModel 只更新指定属性
            // string[] allowedProperties = new[] { "ProductName", "UnitPrice" };
            // UpdateModel(product, allowedProperties);             var category = new CategeorySystem().GetCategory(int.Parse(Request.Form["Category"]));
            product.CategoriesReference.EntityKey = ps.CreateEntityKey("Categories", category);             if (!product.IsValid)
            {
                foreach (var validation in product.GetValidation())
                {
                    // 设置验证信息
                    ModelState.AddModelError(validation.PropertyName, validation.ErrorMessage);
                }
            }
            else
            {
                ps.Save();
            }             ViewData["CategoryList"] = new SelectList(new CategeorySystem().GetCategory(), "CategoryId", "CategoryName", category.CategoryID);             return View("Details", product);
        }
    }
}

Details.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC.Models.Products>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <style type="text/css">
        .bold
        {}{
            font-weight: bold;
        }
    </style>
    <h2>
        Details</h2>
    <%= Html.ValidationSummary("输入信息有误") %>
    <% Html.BeginForm("Update", "ViewDemo", new { id = Model.ProductID }, FormMethod.Post); %>
    <p>
        <strong>ProductID:</strong>
        <%= Html.Encode(Model.ProductID) %>
    </p>
    <p>
        <label for="ProductName">
            ProductName:</label>
        <%= Html.TextBox("ProductName", Model.ProductName, new { style = "color: blue;", @class = "bold" })%>
        <%= Html.ValidationMessage("ProductName", "*") %>
    </p>
    <p>
        <label for="Category">
            Category:</label>
        <%-- Html.ListBox() 和 Html.DropDownList() 需要 IEnumerable<SelectListItem> 类型的数据做数据源 --%>
        <%= Html.DropDownList("Category", ViewData["CategoryList"] as SelectList)%>
    </p>
    <p>
        <strong>UnitPrice:</strong>
        <%= Html.Encode(string.Format("{0:F2}", Model.UnitPrice))%>
    </p>
    <p>
        <input type="submit" value="Save" />
    </p>
    <p>
        <%= TempData["Temp"]%>
    </p>
    <% Html.EndForm(); %>
    <p>
        <%=Html.RouteLink("返回首页", new { Controller = "Home" })%>
    </p>     
<%-- 需要使用 Web Form 方式的话,则在后置代码中继承 System.Web.Mvc.ViewPage 或 System.Web.Mvc.ViewPage<T> 即可-- %>
    
    
<%-- 
HtmlHelper 简要说明: 可以用如下的方式生成 form
using (Html.BeginForm()) { }
using (Html.BeginRouteForm()) { }
Html.BeginForm(); Html.EndForm(); 可以使用 Html.ListBox(), Html.RadioButton() 之类的来生成 html 元素 Html.ValidationMessage() - 指定的 ModelName 输入信息不合法时所输出的验证信息
Html.ValidationSummary() - 汇总所有验证信息
验证信息可以在 Action 中用 ModelState.AddModelError() 的方式来添加
验证信息的样式通过样式表修改 .field-validation-error{} .input-validation-error {} .validation-summary-errors {} Html.Encode(); Html.AttributeEncode(); 用于对输出的内容做编码 Html.RenderPartial() - 引入一个 Partial View Html.ActionLink() - 根据 Action 找目标
Html.RouteLink() - 根据路由找目标 Html.ViewContext - View 的上下文信息。包括 Controller, TempData, ViewData, 路由信息, HttpContext 等信息
--%>
</asp:Content>

2、创建一个自定义的 ViewEngine 的 Demo

MyView.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; using System.Web.Mvc;
using System.IO;
using System.Text.RegularExpressions; namespace MVC
{
    /**//// <summary>
    /// 自定义的视图
    /// 视图需要继承 IView 接口
    /// </summary>
    public class MyView : IView
    {
        // 视图文件的物理路径
        private string _viewPhysicalPath;         public MyView(string viewPhysicalPath)
        {
            _viewPhysicalPath = viewPhysicalPath;
        }         /**//// <summary>
        /// 实现 IView 接口的 Render() 方法
        /// </summary>
        public void Render(ViewContext viewContext, TextWriter writer)
        {
            // 获取视图文件的原始内容  
            string rawContents = File.ReadAllText(_viewPhysicalPath);             // 根据自定义的规则解析原始内容  
            string parsedContents = Parse(rawContents, viewContext.ViewData);             // 呈现出解析后的内容
            writer.Write(parsedContents);
        }         public string Parse(string contents, ViewDataDictionary viewData)
        {
            // 对 {##} 之间的内容作解析
            return Regex.Replace
            (
                contents, 
                @"\{#(.+)#\}",                  // 委托类型 public delegate string MatchEvaluator(Match match)
                p => GetMatch(p, viewData)
            );
        }         protected virtual string GetMatch(Match m, ViewDataDictionary viewData)
        {
            if (m.Success)
            {
                // 获取匹配后的结果,即 ViewData 中的 key 值,并根据这个 key 值返回 ViewData 中对应的 value
                string key = m.Result("$1");
                if (viewData.ContainsKey(key))
                {
                    return viewData[key].ToString();
                }
            }             return string.Empty;
        }
    }
}

MyViewEngine.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; using System.Web.Mvc; namespace MVC
{
    // MvcContrib 中提供了很多 ViewEngine, 还提供了以 asp.net mvc 框架为基础的一些额外的功能
    // 地址:http://www.codeplex.com/MVCContrib     /**//// <summary>
    /// 自定义的视图引擎
    /// 视图引擎需要继承 IViewEngine 接口
    /// VirtualPathProviderViewEngine 继承了 IViewEngine 接口,实现了根据指定的路径格式搜索对应的页面文件的功能(内用缓存机制)
    /// </summary>
    public class MyViewEngine : VirtualPathProviderViewEngine
    {
        public MyViewEngine()
        {
            // 自定义 View 路径格式
            base.ViewLocationFormats = new string[] 
            { 
                "~/Views/{1}/{0}.my", "~/Views/Shared/{0}.my" 
            };
        }         protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return this.CreateView(controllerContext, partialPath, string.Empty);
        }         /**//// <summary>
        /// 根据指定路径返回一个实现了 IView 接口的对象
        /// </summary>
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            var physicalPath = controllerContext.HttpContext.Server.MapPath(viewPath);             return new MyView(physicalPath);
        }
    }
}

Global.asax.cs

protected void Application_Start()
{
    // 增加新的视图引擎 ViewEngine
    ViewEngines.Engines.Add(new MyViewEngine());  
}

CustomViewEngineController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax; namespace MVC.Controllers
{
    /**//// <summary>
    /// 用于演示自定义的 ViewEngine 的 Controller
    /// </summary>
    public class CustomViewEngineController : Controller
    {
        public ActionResult Index()
        {
            ViewData["name"] = "webabcd";
            ViewData["age"] = "70";             // 如果视图文件中有 {##} 形式的字符串,则 MyViewEngine 会对其做相应的解析
            // 如 {#name#} 会被解析为 webabcd             return View();
        }  
    }
}

Index.my(智能感知在“工具 - 选项 - 文本编辑器 - 文件扩展名”中编辑)

<html>
<head>
    <title>创建自定义的 ViewEngine 的 Demo</title>
</head>
<body>
    <div>name: {#name#}</div>
    <div>age: {#age#}</div>
</body>
</html>

运行结果:

name: webabcd

age: 70





OK

[源码下载]

返璞归真 asp.net mvc (4) - View/ViewEngine的更多相关文章

  1. 返璞归真 asp.net mvc (9) - asp.net mvc 3.0 新特性之 View(Razor)

    原文:返璞归真 asp.net mvc (9) - asp.net mvc 3.0 新特性之 View(Razor) [索引页][源码下载] 返璞归真 asp.net mvc (9) - asp.ne ...

  2. 返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller

    原文:返璞归真 asp.net mvc (7) - asp.net mvc 3.0 新特性之 Controller [索引页][源码下载] 返璞归真 asp.net mvc (7) - asp.net ...

  3. 返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model

    原文:返璞归真 asp.net mvc (8) - asp.net mvc 3.0 新特性之 Model [索引页][源码下载] 返璞归真 asp.net mvc (8) - asp.net mvc ...

  4. 返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test

    原文:返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test [索引页] [源码下载] 返璞归真 ...

  5. 返璞归真 asp.net mvc (3) - Controller/Action

    原文:返璞归真 asp.net mvc (3) - Controller/Action [索引页] [源码下载] 返璞归真 asp.net mvc (3) - Controller/Action 作者 ...

  6. 返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo

    原文:返璞归真 asp.net mvc (1) - 添加.查询.更新和删除的 Demo [索引页] [源码下载] 返璞归真 asp.net mvc (1) - 添加.查询.更新和删除的 Demo 作者 ...

  7. 返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性

    原文:返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 [索引页][源码下载] 返璞归真 asp.net mvc (12) - asp.net mvc ...

  8. 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性

    原文:返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性 [索引页][源码下载] 返璞归真 asp.net mvc (6) - asp.net mvc 2.0 新特性 ...

  9. 预热ASP.NET MVC 的View

    ASP.NET MVC 的View 预设是Load on Demand(按需加载),也就是说View 第一次要Render 的时候才会去载入跟编译,这个就会造成一个现象,即使Web 应用程式已经完成启 ...

随机推荐

  1. 201215-03-19---cocos2dx内存管理--具体解释

    因为cocos2dx我们的使用c++写的,所以内存管理就是一个绕只是去的坎,这个你不懂内存仅仅懂业务逻辑的话,还玩什么c++,今天看了半天这个东西,事实上本质上是理解的,可是就是有一个过不去的坎,最终 ...

  2. sql使用存储过程和交易

    在过去的一年.学习数据库的时候学校有存储过程.永远只是知道一些理论,我不知道怎么用.时隔一年,最终找到怎样使用存储过程了. 在机房收费系统中.有些操作.须要多次运行sql语句,多次运行完毕才算是完毕这 ...

  3. java.lang.IllegalAccessError: class javax.activation.SecuritySupport12 cannot access its superclass

    最近加入新的项目组,eclipse + tomcat7 + spring +ibatis + restful 遇到了这样的问题, 说是不能访问父类,我一开始以为是版本的原因,但是久经更改,错误依然,实 ...

  4. JavaScript 中创建对象的方法(读书笔记思维导图)

    面向对象(Object-Oriented, OO)的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.而 ECMAScript 中没有类的概念,所以我们可以使用 ...

  5. HDU1789Doing Homework again(贪婪)

    HDU1789Doing Homework again(贪心) 题目链接 题目大意:给你n们作业的最后期限和过了这个期限没做须要扣的分数.问如何安排能够使得扣分最少. 解题思路:贪心,将扣分多的作业排 ...

  6. WebService开启远程测试

    WebService部署成站点之后,如果在本地测试webservice的接口可以运行,在远程却显示“测试窗体只能用于来自本地计算机的请求”或者"The test form is only a ...

  7. unix解释器文件详解

    exec执行普通文件和解释器文件的区别 2014-11-15 23:52:45 分类: LINUX exec执行普通文件和解释器文件的区别 ——lvyilong316 1. 从一个问题开始 首先要从项 ...

  8. Python的控制结构(转)

    首先我的工作第一语言是c/c++(面向对象子集).选择学习python一方面是因为看很多人都说python开发效率高,所以想验证一下:另一方面,Eric S. Raymond在文章:如何成为一名黑客 ...

  9. GDAL切割重采样遥感图像

    一个小测试程序开发全过程实录,完全新手入门级的实例,如果你还在为处理大影像而发愁,来试试这个称手的工具吧. Imagec 开发日记 2013-6-25 需求: 影像数据切割,重采样 数据切割的要求是简 ...

  10. finger用户名、主目录、停滞时间、登录时间

    finger yum install finger    1.作用    finger用来查询一台主机上的登录账号的信息,通常会显示用户名.主目录.停滞时间.登录时间.登录Shell等信息,使用权限为 ...