最近有个老项目须要作一个需求更迭,老项目是基于传统的webform项目的 为了更好的先后台交互,决定引入Nancyfx框架html
关于Nancyfx框架框架是啥就很少介绍了 总的来讲是一款轻量级的web框架,路由规则至关丰富web
Install-Package Nancy -Version 1.4.5mvc
Install-Package Nancy.Hosting.Aspnet -Version 1.4.1框架
因为是基于现有的webform系统,配置须要特别注意spa
在web.config中添加 location标签 path="nancy" 代表 处理nancyfx处理的路由路径 ,相似mvc系统中的 Area名称debug
<location path="nancy"> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpHandlers> <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/> </httpHandlers> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <validation validateIntegratedModeConfiguration="false"/> <handlers> <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/> </handlers> </system.webServer> </location>
好啦 这样就能够愉快的使用nancyfx啦code
新建Home类而且继承自NancyModuleorm
public class Home:NancyModule { public Home():base("nancy") { Get["/home"] = x => "hello"; Get["/cn"] = x => { return Response.AsText("呵呵,中文不乱码了!!", "text/html;charset=UTF-8");//中文不乱码了!! }; } }
而后就能够跑起来啦htm
访问地址 http://localhost:49799/nancy/home 返回helloblog
访问地址 http://localhost:49799/nancy/cn 返回 呵呵,中文不乱码了!