Freemmarker模板引擎,来自Java的JSP。.NET上进行了移植,形成Freemarker.Net模板引擎。
关于Freemarker.Net的详细介绍,请参考:http://freemarkernet.codeplex.com/documentation
用法:
首先引用一下类库:
2、在全局配置文件Global.asax.cs中,配置如下:
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
//ViewEngines.Engines.Clear();
*ViewEngines.Engines.Add(new FreemarkerViewEngine(this.Server.MapPath("~/")));*
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
3、创建控制器
public class HomeController : Controller {
public ActionResult Index() {
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About() {
return View();
}
}
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<link href="${url.Content("~/Content/Site.css")}" rel="stylesheet" type="text/css" />
<script src="${url.Content("~/Scripts/jquery-1.5.1.min.js")}" type="text/javascript"></script>
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>FreeMarker MVC Application</h1>
</div>
<div id="logindisplay">
<#if request.IsAuthenticated>
Welcome <strong>${http.User.Identity.Name}</strong>!
[ ${html.ActionLink("Log Off", "LogOff", "Account")} ]
<#else>
[ ${html.ActionLink("Log On", "LogOn", "Account")} ]
</#if>
</div>
<div id="menucontainer">
<ul id="menu">
<li>${html.ActionLink("Home", "Index", "Home")}</li>
<li>${html.ActionLink("About", "About", "Home")}</li>
<li>${html.ActionLink("Person", "Index", "Person")}</li>
</ul>
</div>
</div>
<div id="main">
<h2>${controller.ViewData.Message}</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<div id="footer">
</div>
</div>
</div>
</body>
</html>
最后,运行ASP.NET程序。
无意中,做了一次社区的搬运工,感兴趣的朋友,可以试用下。