As a rule, start by building a simple Hello Word project as a course development
- Create a new, basic ASP. NET WEB Application project with Visual Studio, named website. Mono's currently compatible. NET version is 4.5, so use the. NET Framework 4.5 here
- Install the following development packages with NuGet
- Nancy
- Nancy.Viewengines.Razor: Using the Razor view engine
- Microsoft.Owin.Host.SystemWeb: Using katana as a web host for Owin
- Nancy.owin
- The
- mimics the standard ASP. NET MVC project to build the directory so that it looks more like a. Nets MVC program. Add two MVC View page (Razor) files, and the page file code is similar to the ASP.
-
- _viewstart.cshtml code
@{Layout = "shared/_layout.cshtml";}
- _layout.cshtml code
<! DOCTYPE Html>,
<title> @ViewBag. Title</title>
<meta charset= " Utf-8 "/>
<meta name=" viewport "content=" Width=device-width, initial-scale=1.0,
<body>,
@RenderBody ()
</div>
</body>
- In the root directory, create a new Owin Startup class with the following code
Public class startup{ publicvoid Configuration (Iappbuilder app) { app. Usenancy (); }}
- Add Class Homeviewmodel in Models
Public class homeviewmodel{ publicstringgetset;}
- Controller Add Class Indexcontroller, inherited from Nancymodule, this class is the director. Unlike the ASP. NET Webapi, Nancy defines Get,post,put in the constructor, Delete routing path and method.
usingNancy;usingnancy.security;usingWebsite.models; Public classindexcontroller:nancymodule{ PublicIndexcontroller () {get["/"] = _ ~ =Index (); get["/index"] = _ ~ =Index (); } Private DynamicIndex () { This. Viewbag.title ="Hello Word"; varModel =NewHomeviewmodel () {Name="Helle word,carl!" }; returnview["Home/index", model]; }}
- Add the index.cshtml view file under home to display the model's Name property value on the page
<div> @Model .name</div>
- Configuring Webconfig, configuring support for Katana web hosting
<!-katana-asp.net Owin Host supports--><appsettings> <add key= "owin:handleallrequests" value= "true"/ ></appSettings>
Nancyfx Cross-platform Development Summary (ii) Hello World