用NFinal框架开发的项目类似于MVC的那种开发方式,有Controller层、Model层、View层,还包括表现层Web层,在NFinal开发的项目中真正执行的代码也就是Web层中的代码,Web中的代码是根据Controller与View模板中的代码生成的执行代码,我们只需要在Controller中写好逻辑,在View中设计好页面然后运行WebComplier.aspx即可生成整个Web文件夹,然后只需要运行Web中相应的HTML页面即可。那么首先来看下控制器相关的东西。



控制器的定义

1.控制器必须写在Controllers目录下.

2.命名空间采用默认的命名空间,类名必须以Controller结尾,且必须继承自Controller基类.

3.其函数返回值类型为void,修饰符为public.


例:

1、在Controllers下新建SampleController.cs

usingSystem.Collections.Generic;usingSystem.Web;//由于项目名和模块名不同,命名空间也会不同,复制该代码也是无法运行的.//必须手动添加该类,或是修改为正确的命名空间.namespaceWebMvc.App.Controllers{publicclassSampleController:Controller{publicvoidShow(){Write("HelloWorld.");}}}

2、右键点击WebCompiler.aspx选择在浏览器中查看

生成开始

生成结束

3、刷新项目文件夹会发现在Web层的Default文件夹下会出现SampleController文件夹.右键点击该文件夹选择包括在项目中,可以看到文件夹下有Show.cs与Show.html两个文件.

其中Show.cs的代码如下:

usingSystem;usingSystem.Collections.Generic;usingSystem.Web;namespaceWebMvc.App.Web.Default.SampleController{publicclassShowAction:Controller{publicShowAction(System.IO.TextWritertw):base(tw){}publicShowAction(stringfileName):base(fileName){}publicvoidShow(){Write("HelloWorld.");}}}

Show.html中的代码如下:

<!DOCTYPEhtml><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title</title></head><body><script>window.location.href="/App/SampleController/Show.htm";</script></body></html>

Show中的代码只是跳转并执行SampleController下的Show()方法.


4、右键点击并选择在浏览器中查看.可以看到浏览器输出Hello World.