您的位置:首页 > 编程语言 > ASP

Part 3 - Using custom view engines with asp.net mvc

2016-10-14 15:40 441 查看
Out of the box ASP.NET MVC supports the following 2 view engines 
1. ASPX
2. Razor 

In addition to the above 2 view engines, there are several custom view engines that can be used with asp.net mvc. The following are a few of these custom view engines.
1. Spark
2. NHaml
3. SharpDOM
4. Brail etc....

For example, if you want to use Spark as the view engine for your asp.net mvc 4 project, then install Spark.Web.Mvc4 using NuGet Package Manager

At this point, right click on any controller action method and select "Add View" from the context menu. Notice that, "View Engine" dropdownlist in "Add View" dialog
box only shows - Razor and ASPX view engines. If you want the "Spark" view engine to be listed, then, we need to register "Spark View Engine" templates. 

Step 1: Navigate to the following path and create a folder with name="Spark". Spark specific templates must be copied into "Spark" folder.
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView

Step 2: Create an xml file with in "Spark" folder. The name of the XML file must be"ViewEngine.xml" and must contain the following xml content.
<?xml version="1.0" encoding="utf-8" ?>
<ViewEngine DisplayName="Spark" 
  ViewFileExtension=".spark" 
  DefaultLayoutPage="~/Views/Shared/_Layout.spark" 
  PartialViewFileExtension=".spark" />

Now, right click on any controller action method and select "Add View" from the context menu. Notice that, "Spark" view engine is also listed. 

此时Scoffold Template是空的,如下图:



如果希望他能像另外两个ViewEngine一样,有Create, Delete等Template, 只需要把他们的文件Create.tt, Delete.tt等文件拷贝到Spark文件夹即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: