NFinal 控制器—URL

獲取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中的代碼如下: 

<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"];  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章