標籤:size view 視圖 應用 src 應用程式 net cat nullable
Nancy簡介
Nancy是一個輕量級的獨立的架構,下面是官網的一些介紹:
- Nancy 是一個輕量級用於構建基於 HTTP 的 Web 服務,基於 .NET 和 Mono 平台,架構的目標是保持儘可能多的方式,並提供一個super-duper-happy-path所有互動。
- Nancy 設計用於處理
DELETE, GET, HEAD, OPTIONS, POST, PUT和 PATCH 等要求方法,並提供簡單優雅的 DSL 以返迴響應。讓你有更多時間專註於你的代碼和程式。
官方地址:http://nancyfx.org GitHub:https://github.com/NancyFx/Nancy
Nancy 在前兩天 發布了一個 v2.0.0-barneyrubble 版本,支援ASP.NET Core 1.0。
如何在 ASP.NET Core 中使用 Nancy 架構,下面我們就來學習。
建立ASP.NET Core Nancy項目
宿主使用 ASP.NET Core Host Kestrel
首先建立一個ASP.NET Core 應用程式,下一步選擇空的模板。
下面來添加引用。
首先添加 Microsoft.AspNetCore.Owin
Install-Package Microsoft.AspNetCore.Owin
然後添加 Nancy
Install-Package Nancy -Pre
添加好以後我們就可以來編寫代碼。
在 Startup.cs 添加 Nancy.Owin 引用,並在Configure 方法加入如下代碼:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseOwin(x => x.UseNancy()); }
然後我們來添加Module,可以理解為是 Nancy的 Controller 。
這裡添加一個HomeModule,我們添加一個類 HomeModule.cs ,然後繼承 NancyModule。
public class HomeModule:NancyModule { public HomeModule() { Get("/", r => "Nancy running on ASP.NET Core LineZero"); Get("/{name}", r => "簡單的路由模板,路由參數:"+r.name); Get("/404", r => HttpStatusCode.NotFound); } }
然後運行程式,這裡我們使用 Kestrel 運行。
http://localhost:5000
http://localhost:5000/linezero
http://localhost:5000/404
這個是Nancy內建的404,我們也可以自己處理。
本篇主要講解Nancy 在 ASP.NET Core中的使用,更多Nancy的介紹及使用,可以參考我之前的一些文章,Nancy 是的一個非常輕量型的架構。
如果你覺得本文對你有協助,請點擊“推薦”,謝謝。
參考頁面:
http://www.yuanjiaocheng.net/CSharp/Csharp-list-fanx.html
http://www.yuanjiaocheng.net/CSharp/csharp-nullable.html
http://www.yuanjiaocheng.net/mvc/create-first-mvc.html
http://www.yuanjiaocheng.net/CSharp/csharp-extension-method.html
http://www.yuanjiaocheng.net/mvc/mvc-helper-textbox.html
http://www.yuanjiaocheng.net/ASPNET-CORE/core-login-logout.html
http://www.yuanjiaocheng.net/ASPNET-CORE/core-views.html
http://www.yuanjiaocheng.net/CSharp/first.html
http://www.yuanjiaocheng.net/ASPNET-CORE/core-razor-taghelpers.html
http://www.yuanjiaocheng.net/mvc/mvc-helper-TextArea.html
http://www.yuanjiaocheng.net/mvc/create-layout-view-in-mvc.html
ASP.NET Core開發-使用Nancy架構