ASP. net mvc displays views using the AderTemplateEngine template engine
1. Create the location class for the ArtTemplateViewLocator View File. The Code is as follows:
Namespace ArtLib
{
Class ArtTemplateViewLocator: ViewLocator
{
Public ArtTemplateViewLocator ()
{
Base. ViewLocationFormats = new string [] {"~ /Views/{1}/02.16.htm ",
"~ /Views/{1}/02.16.html ",
"~ /Views/Shared/share0).htm ",
"~ /Views/Shared/custom 02.16.html"
};
Base. MasterLocationFormats = new string [] {""};
}
}
}
2. Create a VariableResolver class for parsing the AdrTemplate template view and register the custom component TagList. The Code is as follows:
Namespace ArtLib
{
Public static class VariableResolver
{
Public static object ArtTemplateResolve (string filePath, IDictionary viewData)
{
// Initialize the template engine
TemplateManager template1 = TemplateManager. FromFile (filePath );
// Set template data
Template1.SetValue ("ViewData", viewData );
// Register a custom tag
Template1.RegisterCustomTag ("list", new TagList ());
Return template1.Process ();
}
}
}
3. Create the ArtTemplateViewEngine class to process view requests and assign ViewData to the template engine. The Code is as follows:
IViewLocator _ viewLocator = null;
Public IViewLocator ViewLocator
{
Get
{
If (this. _ viewLocator = null)
{
This. _ viewLocator = new ArtTemplateViewLocator ();
}
Return this. _ viewLocator;
}
Set
{
This. _ viewLocator = value;
}
}
Public string TemplatePath {get; set ;}
# Region IViewEngine Member
Public void RenderView (ViewContext viewContext)
{
String viewLocation = ViewLocator. GetViewLocation (viewContext, viewContext. ViewName );
If (string. IsNullOrEmpty (viewLocation ))
{
Throw new InvalidOperationException (string. Format ("View {0} cocould not be found.", viewContext. ViewName ));
}
String viewPath = HttpContext. Current. Request. MapPath (viewLocation );
// Template Parsing is as follows
String viewTemplate = VariableResolver. ArtTemplateResolve (viewPath, viewContext. ViewData) as string;
HttpContext. Current. Response. Write (viewTemplate );
}
# Endregion
4. Create the ArtTemplateControllerFactory class for Golbal. asax to bind views. The Code is as follows:
Namespace ArtLib
{
Public class ArtTemplateControllerFactory: DefaultControllerFactory
{
Protected override IController CreateController (RequestContext requestContext, string controllerName)
{
Controller controller = (Controller) base. CreateController (requestContext, controllerName );
Controller. ViewEngine = new ArtTemplateViewEngine (); // modify the default view engine for the view engine we just created
Return controller;
}
}
}
5. Create a custom template label class: TagList. cs is used to display the data list. The Code is as follows:
Namespace ArtLib
{
Public class TagList: ITagHandler
{
# Region ITagHandler Member
Public void TagBeginProcess (TemplateManager manager, Ader. TemplateEngine. Parser. AST. Tag tag Tag, ref bool processInnerElements, ref bool captureInnerContent)
{
ProcessInnerElements = true;
CaptureInnerContent = true;
}
Public void TagEndProcess (TemplateManager manager, Ader. TemplateEngine. Parser. AST. Tag tag, string innerContent)
{
Expression exp;
Exp = tag. AttributeValue ("pageSize ");
If (exp = null)
Throw new Exception ("Missing attribute: pageSize ");
String pageSize = manager. EvalExpression (exp). ToString ();
Exp = tag. AttributeValue ("currentPage ");
If (exp = null)
Throw new Exception ("Missing attribute: currentPage ");
String currentPage = manager. EvalExpression (exp). ToString ();
Business business = new BusinessLogic ();
List <InformationSimple> list = new List <InformationSimple> ();
If (string. IsNullOrEmpty (pageSize ))
{
List = business. Select <InformationSimple> ();
}
Else
{
Page page = new Page ();
Page. PageSize = Convert. ToInt32 (pageSize );
Page. CurrentPage = Convert. ToInt32 (currentPage );
List = business. SelectByPage <InformationSimple> (ref page );
}
String html = "";
Foreach (InformationSimple info in list)
{
Html = html + "<tr> <td>" + info. InfoTitle + "</td> </tr> ";
}
Html = "<table>" + html + "</table> ";
Manager. WriteValue (html );
}
# Endregion
}
6. Modify the Application_Start () method of the Global. asax file of the ASP. net mvc test project and add the custom controller factory class: ArtTemplateControllerFactory. The Code is as follows:
Protected void Application_Start ()
{
ControllerBuilder. Current. SetControllerFactory (typeof (ArtTemplateControllerFactory ));
RegisterRoutes (RouteTable. Routes );
}
7. Create the template file Views/Home/Index.htm. The Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/5o/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
<Ad: list pageSize = "10" currentPage = "1"> </ad: list>
# ViewData ["Title"] #
</Body>
</Html>