簡潔的MVC思想架構——Nancy(環境配置與Get操作)

來源:互聯網
上載者:User

標籤:style   blog   http   color   ar   os   使用   sp   檔案   

  1. Nancy官網——https://github.com/NancyFx/Nancy
  2. 概述:Nancy是一個開源的Web輕型架構核心符合MVC思想,有開發方便,路由簡單的特點,而且功能齊全
  3. 起步: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操作)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.