1. Background:

I host my BlogEngine.NET (this blog) as a sub application under a main ASP.NET MVC 3 web application (zizhujy.com), it runs well until one day I tried to enter the administrator control panel – the following error showed on page:

unable to cast object of type 'asp._page_admin_default_cshtml' to type 'system.web.ihttphandler'.

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.InvalidCastException: Unable to cast object of type 'ASP._Page_admin_default_cshtml' to type 'System.Web.IHttpHandler'.
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:

[InvalidCastException: Unable to cast object of type 'ASP._Page_admin_default_cshtml' to type 'System.Web.IHttpHandler'.]
   System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath, VirtualPathFactoryManager virtualPathFactoryManager) +75
   System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context) +321
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +270

2. Cause:

The error only happens when try to view the pages that associated with .cshtml files. Seems that the BlogEngine.NET application inherited the razor settings from its parent ASP.NET MVC web application.

3. Solution:

Step 1. Open the web.config file under the BlogEngine.NET web root folder.

Step 2. Add below xml snipet under the <configuration /> node.

  <!-- Very important, to clear the main application's settings. Else the cshtml file can't be opened. -->
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <clear/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>  

Fixed: unable to cast object of type 'asp._page_admin_default_cshtml' to type 'system.web.ihttphandler' error

Step 3. Make sure these dll files exist in the “bin” folder under BlogEngine.NET web root.

  • System.Web.Helpers.dll
  • System.Web.Mvc.dll
  • System.Web.Razor.dll
  • System.Web.WebPages.dll
  • System.Web.WebPages.Razor.dll

Make sure these dll files exist in the “bin” folder under BlogEngine.NET web root.

Step 4. If you still can’t view your administrator control panels after step 3, then the following steps would save you. First, in your parent web application’s web.config under the root folder, delete the Razor related settings:

<configSections>
  <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">

  <br />&#160;&#160;&#160; &lt;section name=&quot;host&quot; type=&quot;System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirePermission=&quot;false&quot;/&gt; 

  <br />&#160;&#160;&#160; &lt;section name=&quot;pages&quot; type=&quot;System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot; requirePermission=&quot;false&quot;/&gt; 

  <br />&#160; &lt;/sectionGroup&gt; 

  <br />&lt;/configSections&gt;</strike></p>

<system.web.webPages.razor>
  <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  <br />&#160; &lt;pages pageBaseType=&quot;System.Web.Mvc.WebViewPage&quot;&gt; 

  <br />&#160;&#160;&#160; &lt;namespaces&gt; 

  <br />&#160;&#160;&#160;&#160;&#160; &lt;add namespace=&quot;System.Web.Mvc&quot;/&gt; 

  <br />&#160;&#160;&#160;&#160;&#160; &lt;add namespace=&quot;System.Web.Mvc.Ajax&quot;/&gt; 

  <br />&#160;&#160;&#160;&#160;&#160; &lt;add namespace=&quot;System.Web.Mvc.Html&quot;/&gt; 

  <br />&#160;&#160;&#160;&#160;&#160; &lt;add namespace=&quot;System.Web.Routing&quot;/&gt; 

  <br />&#160;&#160;&#160; &lt;/namespaces&gt; 

  <br />&#160; &lt;/pages&gt; 

  <br />&lt;/system.web.webPages.razor&gt;</strike> 

<br /></p>

Step 5. Then you can view the administrator control panels definitely! If you met problems with the cshtml files in your parent application, then you can put the deleted settings to your ~/Views/web.config file, and make mark it build as content to make it be copied to publish folder.


Fixed: unable to cast object of type 'asp._page_admin_default_cshtml' to type 'system.web.ihttphandler' error

Now you are set to view the administrator control panels without breaking the parent application!

Now you are set to view the administrator control panels!