Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an MVC 2 application to a Nancy site in IIS 7

In IIS 7, I have created a web site using a Nancy project. Then, I added an MVC 2 application to the site using the alias api. I am able to visit defined routes in the Nancy project perfectly. However, when I visit /api, I get the following error:

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

It seems that the MVC 2 application is trying to use the NancyHttpRequestHandler to process the request. I say this because routes that are not defined in the Nancy application display a 404 page.

I have tried several things:

  1. To Web.config of the MVC 2 application, I added the following to the <system.web/> block:

    <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. To Web.config of Nancy application, I added the following to the <system.web/> block:

    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*" />
      <remove verb="*" path="api/*" />
    </httpHandlers>
    
  3. I have also tried toying with the settings in the <system.webServer/> and <system.serviceModel/> blocks in both applications.

How can I get the MVC 2 application to behave properly when it is embedded in the Nancy site in IIS 7? Any guidance would be greatly appreciated.

like image 710
Andrew Ashbacher Avatar asked Aug 15 '12 16:08

Andrew Ashbacher


1 Answers

You had the right idea -- you need to block inheritance of the NancyFx specific configuration sections to the child MVC sites.

In your root (NancyFx) site, create a <location/> tag with your normal configuration. Your NancyFx web.config structure will look something like below. (I added comments to try to keep you out of trouble if you decide to upgrade your MVC2 site to MVC3.)

<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>
like image 95
codeprogression Avatar answered Oct 10 '22 19:10

codeprogression