Nancy 寄宿OWin
一:创建一个空项目
二:安装模板
Install-Package Nancy Install-Package Nancy.Owin Install-Package Microsoft.Owin install-package Nancy.ViewEngines.Razor
三:建好的项目图
四:RazorConfig类的内容及作用
using System.Collections.Generic; using Nancy.ViewEngines.Razor; namespace NancyMvc.Config { /// <summary> /// Razor配置,如果你需要使用 cshtml,这个配置比较重要,当然,也可以在这儿加入其它的类 /// </summary> public class RazorConfig: IRazorConfiguration { /// <summary> /// 需加载的程序集列表 /// </summary> /// <returns></returns> public IEnumerable<string> GetAssemblyNames() { //加了这句,才能处理 cshtml yield return "System.Web.Razor"; } /// <summary> /// 需要添加到cshtml中的名字空间 /// </summary> /// <returns></returns> public IEnumerable<string> GetDefaultNamespaces() { yield return "System.Web.Razor"; } /// <summary> /// 是否自动引用model名字空间 /// </summary> public bool AutoIncludeModelNamespace { get { return true; } } } }
五:SiteRootPath 内容及作用,不可少,不然项目有错误
using System; using Nancy; namespace NancyMvc.Config { /// <summary> /// 提供网站物理路径的类 /// </summary> public class SiteRootPath : IRootPathProvider { /************************************************************** * TinyFox Owin Server 默认情况下 * 网站是放在 TinyFox 进程所在文件夹下的site/wwwroot中的 * ---------------------------------------------------------- * 如果你把 NancyFx 的 Views 页放在其它的地方,应该作相应修改 *******************************************************************/ /// <summary> /// 网站根文件夹物理路径(for tinyfox) /// </summary> // static readonly string _RootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "site", "wwwroot"); // (for Jexus Web Server or Host in ASP.NET),如果自定义root后需要使用 static readonly string _RootPath = AppDomain.CurrentDomain.GetData(".appPath").ToString(); /// <summary> /// 获取网站或WEB应用的根文件夹的物理路径 /// </summary> /// <returns></returns> public string GetRootPath() { return _RootPath; } } }
六:MainModule的内容与作用
using System; using System.Collections.Generic; using System.Drawing; using NancyMvc.Filter; namespace NancyMvc.Controller { public class MainModule: BaseNancyModule { /// <summary> /// 构造函数 /// </summary> public MainModule() { //在构造函数中进行路由配置 Get["/home"] = UserFont; } private dynamic UserFont(dynamic arg) { //显示cshtml页 return View["Home/index"]; } } }
七:BaseNancyModule 的内容与作用
using System; using System.Collections.Generic; using System.Linq; using Nancy; namespace NancyMvc.Filter { public class BaseNancyModule: NancyModule { public BaseNancyModule() { //请求之前 Before += context => { return null; }; //请求之后 After += context => { context.Response.ContentType = "text/html;charset=utf-8"; }; } } }
八:MyStatusHandler
using Nancy; using Nancy.ErrorHandling; using Nancy.ViewEngines; namespace NancyMvc.Handler { /// <summary> /// 自定义http status处理类 /// </summary> public class MyStatusHandler : IStatusCodeHandler { private IViewRenderer viewRenderer; public MyStatusHandler(IViewRenderer viewRenderer) { this.viewRenderer = viewRenderer; } /// <summary> /// 具体处理过程 /// </summary> /// <param name="statusCode"></param> /// <param name="context"></param> public void Handle(HttpStatusCode statusCode, NancyContext context) { var response = viewRenderer.RenderView(context, "Status/404"); response.StatusCode = statusCode == HttpStatusCode.NotFound ? HttpStatusCode.OK : statusCode; context.Response = response; } /// <summary> /// 当前状态是否需要自己处理 /// </summary> /// <param name="statusCode"></param> /// <param name="context"></param> /// <returns></returns> public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) { //return false; return (statusCode == HttpStatusCode.NotFound //|| statusCode == HttpStatusCode.ServiceUnavailable //|| statusCode == HttpStatusCode.InternalServerError ); } } }
九:Adapter
using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Owin.Builder; namespace NancyMvc { class Adapter { static Func<IDictionary<string, object>, Task> _owinApp; /// <summary> /// 默认构造函数 /// </summary> public Adapter() { var builder = new AppBuilder(); var startup = new Startup(); startup.Configuration(builder); _owinApp = builder.Build(); } /// <summary> /// *** JWS所需要的关键函数 *** /// </summary> /// <param name="env">新请求的环境字典,具体内容参见OWIN标准</param> /// <returns>返回一个正在运行或已经完成的任务</returns> public Task OwinMain(IDictionary<string, object> env) { //如果为空 if (_owinApp == null) return null; //将请求交给Microsoft.Owin处理 return _owinApp(env); } } }
十:Startup
using Owin; namespace NancyMvc { public class Startup { public void Configuration(IAppBuilder builder) { builder.UseNancy(); } } }
十一:配置properties
启动外部程序是tinyfox的包 下载地址http://linuxdot.net/down/tinyfox-2.3.tar.gz)
命令行参数中的 -p 8083是你配置的端口号 -root 后面的是你当前项目的位置,不需要配到bin
本文章主要参考来源 http://www.cnblogs.com/gaobing/p/4969581.html