標籤:style blog http color ar os 使用 sp 檔案
- Nancy官網——https://github.com/NancyFx/Nancy
- 概述:Nancy是一個開源的Web輕型架構核心符合MVC思想,有開發方便,路由簡單的特點,而且功能齊全
- 起步:Hellow World
一.建立Asp.Net空Web應用程式
二.使用NuGet添加Nancy包的引用
安裝三個Nancy應用程式套件——Nancy.Hosting.Aspnet;Nancy;Nancy.Viewengines.Razor;
至此,環境已經搭好,上代碼(註:必須在項目根目錄建立Views檔案夾,Nancy查詢檢視表預設從該檔案找起)
Nancy含Get、Post、Delete和Put等常用響應機制。
建立一個類(控制器)基於NancyModule類
public class Home:NancyModule { public Home() { Get["/"] = p => { return "ddd"; }; Get["/{Name}/{Id}"] = p => { return "Name:" + p.Name + "id:" + p.Id; }; } }
直接運行,則預設進入根目錄下 返回“ddd”,
在地址欄輸入相關資訊則指定返回輸入的參數,
此外,Nancy還支援Model類,現在類庫中建立一個新類
1 namespace Nancy.Data 2 { 3 /// <summary> 4 /// 使用者類 5 /// </summary> 6 public class SysUser 7 { 8 /// <summary> 9 /// 編號10 /// </summary>11 public string Id { get; set; }12 /// <summary>13 /// 使用者Id14 /// </summary>15 public string userId { get; set; }16 /// <summary>17 /// 使用者密碼18 /// </summary>19 public string userPwd { get; set; }20 }21 }
在相應控制器中調用該類:
1 //指定跳轉頁面 路徑為Views檔案夾下的User檔案中test.cshtml頁面2 Get["/test"] = p =>3 {4 SysUser user = new SysUser();5 user.userId = "test123";6 user.userPwd = "tser8888";7 return View["User/test.cshtml", user];8 };
在視圖中頁面獲得傳入的user參數:
<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title></title></head><body> <div> <p>使用者名稱:@Model.userId</p> <p>密碼:@Model.userPwd</p> </div></body></html>
運行結果:
最後,附上項目結構圖:
之後,還會繼續介紹Nancy的Post應用以及相應的其他應用等資訊。
簡潔的MVC思想架構——Nancy(環境配置與Get操作)