ITPub博客

首页 > 应用开发 > IT综合 > NFinal 控制器—URL

NFinal 控制器—URL

原创 IT综合 作者:xiaolu6t 时间:2015-07-04 16:01:01 0 删除 编辑

获取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.         }

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


点击(此处)折叠或打开

  1. using System;
  2.         using System.Collections.Generic;
  3.         using System.Web;

  4.         namespace WebMvc.App.Web.Default.SampleController
  5.         {
  6.             public class ShowAction : Controller
  7.      {
  8.          public ShowAction(System.IO.TextWriter tw):base(tw){}
  9.          public ShowAction(string fileName) : base(fileName) {}
  10.                 public void Show(string user)
  11.                 {
  12.                     Write(string.Format(\"Hello {0}.\",user));
  13.                 }
  14.             }
  15.         }

修改Show.html文件中的URL

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

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

点击(此处)折叠或打开

  1. <!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"];  





 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30321499/viewspace-1722638/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: NFinal AJAX
请登录后发表评论 登录
全部评论

注册时间:2015-07-02

  • 博文量
    10
  • 访问量
    9784