1. C # basic use of NVelocity in MVC Learning,

Source: Internet
Author: User

1. C # basic use of NVelocity in MVC Learning,

I'm a little white guy. I just started learning. Don't laugh ......

NVelocity is a very easy-to-use framework. It can be learned from it and can be developed step by step.

First, create an empty web application and create a general handler Login2.ashx.

Then, introduce NVelocity,

C

Create login.htm on the Html page to implement the logon framework.

NVelocity syntax starts with $

<Html> 

Write the Login2.ashx. cs page and use NVelocity to process requests

Public class Login2: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; string username = context. request ["username"]; string password = context. request ["password"]; if (string. isNullOrEmpty (username) & string. isNullOrEmpty (password) {VelocityEngine vltEngine = new VelocityEngine (); vltEngine. setProperty (RuntimeConstants. RESOURCE_LOADER, "File"); vltEngine. SetProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. Web. Hosting. HostingEnvironment. MapPath ("~ /Templates "); // vltEngine. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put ("username", ""); // set parameters. In the template, you can use $ data to reference vltContext. put ("password", ""); vltContext. put ("msg", ""); Template vltTemplate = vltEngine. getTemplate ("login.htm"); System. IO. stringWriter vltWriter = new System. IO. stringWriter (); vltTemplate. merge (vltContext, vltWriter); string html = vltWriter. GetStringBuilder (). toString (); context. response. write (html);} else {if (username = "admin" & password = "123") {context. response. write ("Logon successful");} else {VelocityEngine vltEngine = new VelocityEngine (); vltEngine. setProperty (RuntimeConstants. RESOURCE_LOADER, "file"); vltEngine. setProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. web. hosting. hostingEnvironment. mapPath ("~ /Templates "); // vltEngine. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put ("username", username); // you can use $ data to reference vltContext in the template. put ("password", password); vltContext. put ("msg", "incorrect user name or password"); Template vltTemplate = vltEngine. getTemplate ("login.htm"); System. IO. stringWriter vltWriter = new System. IO. stringWriter (); vltTemplate. merge (vltContext, vltWriter); string html = vltWriter. getStringBuilder (). toString (); context. response. write (html) ;}} public bool IsReusable {get {return false ;}}

NVelocity is more powerful than that. It can also assign values to object attributes. See the following example.

<Html> 

Background processing program:

Context. response. contentType = "text/html"; Person person = new Person (); person. name = "Zhang San"; person. age = 30; Person dad = new Person (); dad. name = "Zhang San's father"; dad. age = 60; person. father = dad; VelocityEngine vltEngine = new VelocityEngine (); vltEngine. setProperty (RuntimeConstants. RESOURCE_LOADER, "file"); vltEngine. setProperty (RuntimeConstants. FILE_RESOURCE_LOADER_PATH, System. web. hosting. hostingEnv Ironment. MapPath ("~ /Templates "); // vltEngine. init (); VelocityContext vltContext = new VelocityContext (); vltContext. put ("p", person); // you can use $ data in the Template to reference Template vltTemplate = vltEngine. getTemplate ("test.htm"); System. IO. stringWriter vltWriter = new System. IO. stringWriter (); vltTemplate. merge (vltContext, vltWriter); string html = vltWriter. getStringBuilder (). toString (); context. response. write (html );

Code: http://download.csdn.net/detail/murongqingsi/9156661

NVelocity: http://download.csdn.net/detail/murongqingsi/9156623

 

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.