ASP. net mvc 5 builds its own CodeTemplate and codetemplate
We know that when adding a View to the MVC project, you can select a template for the Panel to be added. Some people may wonder where this template is located? Can I build my own infrastructure?
First, you can answer the second question.
I am using Visual Studio 2015, ASP. the Base Frame template of net mvc 5 is located in the directory % programfiles % \ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ Extensions \ Microsoft \ Web \ Mvc \ Scaffolding \ Templates
(For Visual Studio 2013 or Visual Studio2012, the preceding directory Microsoft Visual Studio 14.0 needs to be changed to \ Microsoft Visual Studio 12.0)
The following shows how to build your own base frame.
Add the following directories to the project (note: the directory names must be consistent. Here we only demonstrate how to set up a view base shelf, so you only need to add the MvcView directory. To set up a controller base shelf, you can create a folder in the project according to the directory names supported in the preceding directory)
Open the above MvcView directory and we can see
Here is a view template, including C # and VB. Since the project uses C #, copy C # To the MvcView directory:
(Note: You need to copy Imports. include. t4 and ModelMetadataFunctions. cs. include. t4 at the same time, because the project requires these files to generate views using the base frame)
Open one of the templates and find that the Base Frame uses the T4 template.
Because no code coloring or smart prompts are available, A Visual Studio plugin Devart T4 Editor is installed to support T4 syntax highlighting and smart prompts.
Click Download to open the download page in the browser (originally thought it was directly downloaded in Visual Studio ...), download T4 Editor for Visual Studio 2015 and double-click it. The installation steps are not described here. After the installation is complete, restart Visual Studio.
Open the base frame again and find the code keyword coloring.
Here, we try to modify the return link in the Create. cs. t4 template to display Chinese characters.
After modification:
Use Create base frame when adding a template
After adding the link, we found that the returned link is in Chinese.
The following shows how to build your own base frame.
1. Add a Text Template (Note: You need to change the suffix tt to t4)
<# @ Template language = "C #" HostSpecific = "True" #> <# @ output extension = ". cshtml "#> <# @ include file =" Imports. include. t4 "#> @ model IEnumerable <# =" <"+ ViewDataTypeName +"> "#> <# // The following chained if-statement outputs the file header code and markup for partial view, a view using a layout page, or a regular view. if (IsPartialView) {#>< #} else if (IsLayoutPageSelected) {#>@{ ViewBag. title = "<# = ViewName #> "; <# if (! String. isNullOrEmpty (LayoutPageFile) {#> Layout = "<# = LayoutPageFile #> "; <#}#>}< h2> data list page View Code
The code for Student. cs is attached.
public class Student{ public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public bool Sex { get; set; }}
View Code