$ ViewData. User. Name <br/>
</Body>
</Html>
Of course, we can also register it as the default View engine, so that you do not need to create an IView every time and pass it to View ().
Public class MvcApplication: System. Web. HttpApplication
{
Protected void Application_Start ()
{
ViewEngines. Engines. Clear ();
ViewEngines. Engines. Add (new NVelocityViewEngine ());
RegisterRoutes (RouteTable. Routes );
}
}
Or write an IResultFilter, which is marked on the ActionMethod that requires NVelocityViewEngine.
Public class nvelocityviewattriter: ActionFilterAttribute
{
Public NVelocityViewAttribute ()
{
}
Public nvelocityviewattripath (string viewPath)
{
This. ViewPath = viewPath;
}
Public string ViewPath {get; set ;}
Public override void OnResultExecuting (ResultExecutingContext filterContext)
{
If (String. IsNullOrEmpty (ViewPath ))
{
Foreach (var ext in new [] {"html", "htm "})
{
ViewPath = String. Format ("~ /Views/{0}/{1}. {2 }",
FilterContext. RouteData. GetRequiredString ("controller "),
FilterContext. RouteData. GetRequiredString ("action "),
Ext );
If (System. IO. File. Exists (HttpContext. Current. Server. MapPath (ViewPath) break;
}
}
Var result = filterContext. Result as ViewResult;
If (result! = Null)
{
Result. View = new NVelocityView (filterContext, ViewPath );
}
}
}
Now it is more convenient to use.
[NVelocityView]
Public ActionResult Index ()
{
ViewData ["user"] = new
{
Name = "Tom ",
Age = 13
};
Return View ();
}
You can also manually specify a template.
[NVelocityView ("/Views/Test/Index2.htm")]
Public ActionResult Index ()
{
ViewData ["user"] = new
{
Name = "Tom ",
Age = 13
};
Return View ();
}
Article Source: Fire Network, original: http://zhan.liehuo.net/a/200908/207790.html