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'.
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.
[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
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.
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>
Step 3. 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:
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.
Now you are set to view the administrator control panels without breaking the parent application!