MVC內(nèi)置的視圖引擎有WebForm view engine和Razor view engine,當(dāng)然也可以自定義視圖引擎ViewEngine。本文想針對某個Model,自定義該Model的專屬視圖。
□ 思路
1、控制器方法返回ActionResult是一個抽象類
2、ActionResult的其中一個子類ViewResult,正是她使用IView實例最終渲染出視圖
3、需要自定義IView
4、IViewEngine管理著IView,同時也需要自定義IViewEngine
5、自定義IViewEngine是需要全局注冊的
□ IView接口
public interface IView
{
void Render(System.Web.Mvc.ViewContext viewContext, System.IO.TextWriter writer)
}
第一個參數(shù)ViewContext包含了需要被渲染的信息,被傳遞到前臺的強(qiáng)類型Model也包含在其中。
第二個參數(shù)TextWriter可以幫助我們寫出想要的html格式。
□ IViewEngine接口
public interface IViewEngine
{
System.Web.Mvc.ViewEngineResult FindPartialView(System.Web.Mvc.ControllerContext controllerContext, string partialViewName, bool useCache);
System.Web.Mvc.ViewEngineResult FindView(System.Web.Mvc.ControllerContext controllerContext, string viewName, string masterName, bool useCache);
void ReleaseView(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.IView view);
}
FindPartialView:在當(dāng)前控制器上下文ControllerContext中找到部分視圖。
FindView:在當(dāng)前控制器上下文ControllerContext中找到視圖。
ReleaseView:釋放當(dāng)前控制器上下文ControllerContext中的視圖。
模擬一個Model和數(shù)據(jù)服務(wù)類
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Score { get; set; }
}
public class DataAccess
{
List<Student> students = new List<Student>();
public DataAccess()
{
for (int i = 0; i < 10; i++)
{
students.Add(new Student
{
Id = i + 1,
Name = "Name" + Convert.ToString(i+1),
Score = i + 80
});
}
}
public List<Student> GetStudents()
{
return students;
}
}
實現(xiàn)IView接口,自定義輸出html格式
using System.Collections.Generic;
using System.Web.Mvc;
using CustomViewEngine.Models;
namespace CustomViewEngine.Extension
{
public class StudentView : IView
{
public void Render(ViewContext viewContext, System.IO.TextWriter writer)
{
//從視圖上下文ViewContext中拿到model
var model = viewContext.ViewData.Model;
var students = model as List<Student>;
//自定義輸出視圖的html格式
writer.Write("<table border=1><tr><th>編號</th><th>名稱</th><th>分?jǐn)?shù)</th></tr>");
foreach (Student stu in students)
{
writer.Write("<tr><td>" + stu.Id + "</td><td>" + stu.Name + "</td><td>" + stu.Score + "</td></tr>");
}
writer.Write("</table>");
}
}
}
實現(xiàn)IViewEngine,返回自定義IView
using System;
using System.Web.Mvc;
namespace CustomViewEngine.Extension
{
public class StudentViewEngine : IViewEngine
{
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
throw new System.NotImplementedException();
}
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
if (viewName == "StudentView")
{
return new ViewEngineResult(new StudentView(), this);
}
else
{
return new ViewEngineResult(new String[]{"針對Student的視圖還沒創(chuàng)建!"});
}
}
public void ReleaseView(ControllerContext controllerContext, IView view)
{
}
}
}
HomeController
using System.Web.Mvc;
using CustomViewEngine.Models;
namespace CustomViewEngine.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var students = new DataAccess().GetStudents();
ViewData.Model = students;
return View("StudentView");
}
}
}
全局注冊
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ViewEngines.Engines.Add(new StudentViewEngine());
}
}
瀏覽/Home/Index效果:
分類: MVC
標(biāo)簽: MVC, 自定義視圖引擎ViewEngine, 自定義視圖View