Nancyfx框架在傳統Webform項目中的應用

最近有個老項目須要作一個需求更迭,老項目是基於傳統的webform項目的 爲了更好的先後臺交互,決定引入Nancyfx框架html

關於Nancyfx框架框架是啥就很少介紹了 總的來講是一款輕量級的web框架,路由規則至關豐富web

第一步:引用相關Nacnyfx依賴

Install-Package Nancy -Version 1.4.5mvc

Install-Package Nancy.Hosting.Aspnet -Version 1.4.1框架

第二步 Nancyfx 路由配置

因爲是基於現有的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開發後臺接口

 好啦 這樣就能夠愉快的使用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    返回    呵呵,中文不亂碼了!

相關文章
相關標籤/搜索