Table of Contents
一、新建一个控制台应用程序(Console Application)
二、通过NuGet添加我们需要的Nancy包
三、打开Program.cs,开始写代码了
四、新建一个Modules文件夹,用来存放我们的Modules
五、建一个Views文件夹,用于存放视图
Home Web Front-end HTML Tutorial Nancy之基于Nancy.Hosting.Self的小Demo_html/css_WEB-ITnose

Nancy之基于Nancy.Hosting.Self的小Demo_html/css_WEB-ITnose

Jun 21, 2016 am 08:58 AM

今天来做个基于Nancy.Hosting.Self的小Demo。

关于Self Hosting Nancy,官方文档的介绍如下

https://github.com/NancyFx/Nancy/wiki/Self-Hosting-Nancy

文档具体的内容我就不一一翻译了,主要是演示从头到尾的一个过程,然后看看Nancy.Hosting.Self的源码

一、新建一个控制台应用程序(Console Application)

二、通过NuGet添加我们需要的Nancy包

这里我们可以直接添加Nancy.Hosting.Self,添加这个会顺带添加Nancy。

到这里我们的基本工作就KO了。

三、打开Program.cs,开始写代码了

 1     class Program 2     { 3         static void Main(string[] args) 4         { 5             using (var nancySelfHost = new NancyHost(new Uri("http://localhost:8888/"))) 6             { 7                 nancySelfHost.Start(); 8                 Console.WriteLine("NancySelfHost已启动。。"); 9                 try10                 {11                     Console.WriteLine("正在启动 http://localhost:8888/ ");12                     System.Diagnostics.Process.Start("http://localhost:8888/");13                     Console.WriteLine("成功启动 http://localhost:8888/ ");14                 }15                 catch (Exception)16                 {17                 }18                 Console.Read();19             }20             Console.WriteLine("http://localhost:8888 已经停止 \n NancySelfHost已关闭。。");            21         }22     }Program.cs
Copy after login

这里实例化了一个新的NancyHosting,并直接用Process.Start打开了一个网页。

这样做是为了省时省力偷下懒,不用在启动程序之后再手动去打开浏览器去输入http://localhost:8888

如果不熟悉Process,可以看一下这个

https://msdn.microsoft.com/en-us/library/e8zac0ca(v=vs.110).aspx

四、新建一个Modules文件夹,用来存放我们的Modules

在Modules文件夹新建一个HomeModule.cs

1     public class HomeModule:NancyModule2     {3         public HomeModule()4         {5             Get["/"] = _ => "I'm from Nancy.Hosting.Self!";6         }7     } HomeModule.cs
Copy after login

运行一下,看看效果

正是我们要的结果。下面来看看视图有没有问题。

五、建一个Views文件夹,用于存放视图

新建Home文件夹,新建index.html,这里我们就不用Razor了,Nancy支持多种视图引擎!!这个很不错。

 1 <!DOCTYPE html> 2 <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <meta charset="utf-8" /> 5     <title>NancyDemo</title> 6 </head> 7 <body> 8     <p style="font-size:xx-large">SelfHostingDemo</p>     9 </body>10 </html>index.html
Copy after login

同时对HomeModule.cs进行修改

 1     public class HomeModule:NancyModule 2     { 3         public HomeModule() 4         { 5             Get["/"] = _ => 6             { 7                 return View["index"]; 8             };  9 10         }11     }HomeModule.cs
Copy after login

运行试试。oh no~~ 出错了。。。

为什么会出现错误呢?不应该啊!!

既然有错误,就要排除错误,看看它说那里有问题:

Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'index'Currently available view engine extensions: sshtml,html,htmLocations inspected: views/Home/index-zh-CN,views/Home/index,Home/index-zh-CN,Home/index,views/index-zh-CN,views/index,index-zh-CN,indexRoot path: D:\GithubCode\Demos\NancyDemoWithSelfHosting\SelfHostingDemo\SelfHostingDemo\bin\DebugIf you were expecting raw data back, make sure you set the 'Accept'-header of the request to correct format, for example 'application/json'提示的居然是没有找到视图!!再细细看一下就会发现问题了。Root path!!!!视图应该在放到Debug目录下,这里建的是控制台应用程序,不是web应用程序。所以就把Views文件夹搬家到Debug下面。运行看看,OK,成功了<strong>六、把这个demo放到linux下看看</strong>在 /var/www/ 下面新建一个文件夹 mkdir nancydemo将程序bin目录下的文件传到 /var/www/nancydemo 中,ls看一下里面的内容执行 mono SelfHostingDemo.exe看看效果,OK!到这里,已经完成了一个简单的Demo了。趁着时间还早,看看Nancy.Hosting.Self的内部实现,源码地址:https://github.com/NancyFx/Nancy/tree/master/src/Nancy.Hosting.Self还记得否?我们的Program.cs中有用到这个类----NancyHost
Copy after login
var nancySelfHost = new NancyHost(new Uri("http://localhost:8888/"))
Copy after login
细细看看这个类里面有什么东西。https://github.com/NancyFx/Nancy/blob/master/src/Nancy.Hosting.Self/NancyHost.cs
Copy after login

有六个重载,其实这六个重载都是为了初始化 NancyHost ,有三个是用了默认配置,有三个是用了自定义配置。

我们用到的NancyHost是采用的默认配置,参数就是一个可变的Uri数组。

然后看看Start 方法

主要是监听我们的请求,这个监听过程主要用到了HttpListener,还有异步回调。

里面的 Task.Factory.StartNew 可以看看msdn的介绍

https://msdn.microsoft.com/en-us/library/dd321439(v=vs.110).aspx

持续降温。。注意保暖。。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

See all articles