NFinal 控制器—URL

来源:互联网 发布:linux wget 磁力链接 编辑:程序博客网 时间:2024/05/17 09:21

URL路由规则

规则:http://网址/模块名/控制器的相对路径的文件名/函数名.htm

例: http://localhost/App/IndexController/Index.htm
http://localhsot/App/Admin/IndexController/Index.htm

传参

http://网址/模块名/控制器的相对路径的文件名/函数名/参数名1/参数值1/.../参数名N/参数值N.htm

例: http://localhost/App/IndexController/Index/id/1.htm
http://localhost/App/ListController/Index/pageSize/5/pageIndex/1.htm  

获取URL参数

在控制器的方法中加入一些参数,例如user,然后输出. 

using System; 
using System.Collections.Generic; 
using System.Web;  
namespace WebMvc.App.Controllers
{     
<span style="white-space:pre"></span>public class SampleController:Controller    
<span style="white-space:pre"></span> {        
<span style="white-space:pre"></span> public void Show(string user)        
<span style="white-space:pre"></span> {             
<span style="white-space:pre"></span>Write(string.Format("Hello {0}.",user));       
<span style="white-space:pre"></span> }
<span style="white-space:pre"></span> }
} 
运行WebCompiler.aspx重新生成.
然后把Web/Default/SampleControler文件夹包括在项目中.
其中Show.cs代码如下  :
<span style="white-space:pre"></span>using System;        using System.Collections.Generic;        using System.Web;        namespace WebMvc.App.Web.Default.SampleController        {            public class ShowAction  : Controller        {        public ShowAction(System.IO.TextWriter tw):base(tw){}        public ShowAction(string fileName) : base(fileName) {}                public void Show(string user)                {                    Write(string.Format("Hello {0}.",user));                }            }        }

修改Show.html文件中的URL

URL为:http://localhost/App/SampleController/Show/user/Lucas.htm

其中Show.html中的代码如下: 

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

用浏览器查看Show.html.则浏览器输出Hello Lucas.

参数说明

NFinal会自动帮你转换好你所需要的参数类型,但必须保证参数名前后保持一致,
函数内的参数不仅可以获取URL中的参数,同样也可以获取POST中的参数.
但NFinal不支持获取?id=1这样的参数.
参数类型可以为int,string,float等基本类型.

当然Controller内置的_get变量也可以像传统的ASPX那像手动获取并转换参数.
比如string user=_get["user"];  



0 0