Nancyfx框架在传统Webform项目中的应用

最近有个老项目需要做一个需求更迭,老项目是基于传统的webform项目的 为了更好的前后台交互,决定引入Nancyfx框架

关于Nancyfx框架框架是啥就不多介绍了 总的来说是一款轻量级的web框架,路由规则相当丰富

第一步:引用相关Nacnyfx依赖

Install-Package Nancy -Version 1.4.5

Install-Package Nancy.Hosting.Aspnet -Version 1.4.1

第二步 Nancyfx 路由配置

由于是基于现有的webform系统,配置需要特别注意

在web.config中添加 location标签 path="nancy" 表明 处理nancyfx处理的路由路径 ,类似mvc系统中的 Area名称

  <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开发后台接口

 好啦 这样就可以愉快的使用nancyfx啦

 新建Home类并且继承自NancyModule

 public class Home:NancyModule
    {
        public Home():base("nancy")
        {
            Get["/home"] = x => "hello";
            Get["/cn"] = x =>
            {
                return Response.AsText("呵呵,中文不乱码了!!", "text/html;charset=UTF-8");//中文不乱码了!!
            };
        }
    }

然后就可以跑起来啦

访问地址  http://localhost:49799/nancy/home   返回hello

访问地址  http://localhost:49799/nancy/cn    返回    呵呵,中文不乱码了!

原文地址:https://www.cnblogs.com/yushuo/p/10276375.html