【问题标题】:Add an MVC 2 application to a Nancy site in IIS 7在 IIS 7 中将 MVC 2 应用程序添加到 Nancy 站点
【发布时间】:2012-08-15 16:25:47
【问题描述】:

在 IIS 7 中,我使用 Nancy 项目创建了一个网站。然后,我使用别名api 向站点添加了一个MVC 2 应用程序。我能够完美地访问 Nancy 项目中定义的路线。但是,当我访问/api 时,出现以下错误:

Could not load type 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Could not load type 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[HttpException (0x80004005): Could not load type 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler'.]
   System.Web.Compilation.BuildManager.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) +11588073
   System.Web.Configuration.HandlerFactoryCache.GetTypeWithAssert(String type) +47
   System.Web.Configuration.HandlerFactoryCache.GetHandlerType(String type) +18
   System.Web.Configuration.HandlerFactoryCache..ctor(String type) +27
   System.Web.HttpApplication.GetFactory(String type) +95
   System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +352
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375

似乎 MVC 2 应用程序正在尝试使用 NancyHttpRequestHandler 来处理请求。我这样说是因为 Nancy 应用程序中未定义的路由会显示 404 页面。

我已经尝试了几件事:

  1. 对于 MVC 2 应用程序的 Web.config,我在 <system.web/> 块中添加了以下内容:

    <httpHandlers>
      <add verb="*" path="*.mvc" validate="false" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </httpHandlers>
    
  2. 对于 Nancy 应用程序的 Web.config,我在 &lt;system.web/&gt; 块中添加了以下内容:

    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
      <remove verb="*" path="api/*" />
    </httpHandlers>
    
  3. 我还尝试在两个应用程序中使用 &lt;system.webServer/&gt;&lt;system.serviceModel/&gt; 块中的设置。

当 MVC 2 应用程序嵌入在 IIS 7 的 Nancy 站点中时,如何让它正常运行?任何指导将不胜感激。

【问题讨论】:

    标签: asp.net-mvc iis asp.net-mvc-2 iis-7 nancy


    【解决方案1】:

    您的想法是正确的——您需要阻止 NancyFx 特定配置部分对子 MVC 站点的继承。

    在您的根 (NancyFx) 站点中,使用您的正常配置创建一个 &lt;location/&gt; 标记。您的 NancyFx web.config 结构将如下所示。 (如果您决定将 MVC2 站点升级到 MVC3,我添加了 cmets 来避免您遇到麻烦。)

    <configuration>
      <configSections/>
      <!-- FYI... configSections cannot be moved into the location tag. If you plan
           to upgrade to MVC3 and use the Razor view engine, those configSection 
           declarations need to live here. If you upgrade to MVC3 and use the Razor 
           view engine, you will need to remove the Razor configSections from the 
           views/web.config files any child MVC3 project. -->
      <system.web /> <!-- site-wide system.web settings -->
      <system.webServer /> <!-- site-wide system.webServer settings -->
    
      <!-- Put the NancyFx specific configuration here -->
      <location path="." inheritInChildApplications="false"> 
      <!-- The inheritInChildApplications attribute is the magic sauce! :) -->
        <connectionStrings /> 
        <!-- If the connectionStrings are shared by the child site, 
             you could move them out to the main configuration. But they 
             cannot exist in both sections of this file. -->
        <appSettings />
        <system.web>
          <httpHandlers>
            <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
          </httpHandlers>
        </system.web>
        <system.webServer>
          <handlers>
            <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
          </handlers>
        </system.webServer>
      </location>
    </configuration>
    

    【讨论】:

    • 太棒了!刚刚测试了这个,它就像一个魅力。如果我能说服我的团队将我们过去两周的所有 MVC3 工作迁移到 Nancy 就好了。 :) 谢谢你提供的详情。干杯!
    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2016-06-28
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    相关资源
    最近更新 更多