·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> NFinal 控制器—URL

NFinal 控制器—URL

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

NFinal 控制器—URL

URL路由规则

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

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

传参

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

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

获取URL参数

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

 1  using System; 2  using System.Collections.Generic;  3 using System.Web;   4 namespace WebMvc.App.Controllers 5  {     6      public class SampleController:Controller    7       {         8          public void Show(string user)       9           {           10               Write(string.Format("Hello {0}.",user));   11           }     12         } 13 }  
Controller Code

运行WebCompiler.aspx重新生成.然后把Web/Default/SampleControler文件夹包括在项目中.其中Show.cs代码如下 :

 1 using System.Collections.Generic;  2 using System.Web;  3  namespace WebMvc.App.Web.Default.SampleController 4  {     5      public class ShowAction  : Controller    6       {     7          public ShowAction(System.IO.TextWriter tw):base(tw){}     public ShowAction(string fileName) : base(fileName) {}       8            public void Show(string user)       9            {            10              Write(string.Format("Hello {0}.",user));       11             }   12         } 13 }        
Show.cs Code

修改Show.html文件中的URL

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

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

1 <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 Code

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

参数说明

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

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