获取URL参数
在控制器的方法中加入一些参数,例如user,然后输出.
-
using System;
-
using System.Collections.Generic;
-
using System.Web;
-
-
namespace WebMvc.App.Controllers
-
{
-
public class SampleController:Controller
-
{
-
public void Show(string user)
-
{
-
Write(string.Format(\"Hello {0}.\",user));
-
}
-
}
-
}
运行WebCompiler.aspx重新生成.
然后把Web/Default/SampleControler文件夹包括在项目中.
其中Show.cs代码如下
-
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"];
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30321499/viewspace-1722638/
,如需转载,请注明出处,否则将追究法律责任。