ASP. net mvc 1.0 NVelocityViewEngine

Source: Internet
Author: User

$ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.