首页 文章

安装ASP.NET MVC 4 Beta后抛出InvalidCastException

提问于
浏览
81

在使用ASP.NET MVC 3的计算机上安装ASP.NET MVC 4 Beta后,我收到以下异常 .

System.InvalidCastException未由用户代码处理Message = [A] System.Web.WebPages.Razor.Configuration.HostSection无法强制转换为[B] System.Web.WebPages.Razor.Configuration.HostSection . 类型A源自'System.Web.WebPages.Razor,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35',位于'默认'位置'C:\ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ System .Web.WebPages.Razor \ v4.0_1.0.0.0__31bf3856ad364e35 \ System.Web.WebPages.Razor.dll” . 类型B源自'System.Web.WebPages.Razor,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35',位于'默认'位置'C:\ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ System .Web.WebPages.Razor \ v4.0_2.0.0.0__31bf3856ad364e35 \ System.Web.WebPages.Razor.dll” . 源= System.Web.WebPages.Razor堆栈跟踪:在System.Web.WebPages.Razor.WebRazorHostFactory.GetRazorSection(字符串virtualPath)在System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(字符串virtualPath,字符串physicalPath)在的System.Web System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType上的System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode()处的System.Web.WebPages.Razor.RazorBuildProvider.CreateHost()处的.WebPages.Razor.RazorBuildProvider.GetHostFromConfig() ()System.Web.Compilation.BuildManager.CompildWebFile上System.Web.Compilation.BuildProvidersCompiler.PerformBuild()的System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()处的System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) System.Web.Compilation.BuildManager.GetVPathBuildResultInternal上的(VirtualPath virtualPath)(VirtualPath virtualPath,Boolean noBuild,Boolean allowCrossApp,Boolean allowBuildInPrecompile,Boolean在System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath)的System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext上下文,VirtualPath virtualPath,Boolean noBuild,Boolean allowCrossApp,Boolean allowBuildInPrecompile,Boolean throwIfNotFound,Boolean ensureIsUpToDate)中的throwIfNotFound,Boolean ensureIsUpToDate) ,HttpContext的上下文中,布尔allowCrossApp,布尔throwIfNotFound)在System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(字符串virtualPath)在System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext,字符串virtualPath)在System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext,List1位置,字符串名,字符串controllerName,字符串AREANAME,字符串cacheKey,字符串[]&searchedLocations)在System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String [] locations,String [] a reaLocations,字符串locationsPropertyName,字符串名,字符串controllerName,字符串cacheKeyPrefix,布尔useCache将,字符串[]&searchedLocations)在System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext,字符串的viewName,字符串masterName,布尔useCache将)在的System.Web .Mvc.ViewEngineCollection . <> c__DisplayClassc.b__b(IViewEngine e)上System.Web.Mvc.ViewEngineCollection.Find(Func2查找,布尔trackSearchedPaths)在System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext,字符串的viewName,串masterName)在System.Web.Mvc.ViewResult.FindView(controllerContext上下文)在System.Web.Mvc.ViewResultBase.ExecuteResult(controllerContext上下文)在System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(controllerContext controllerContext,的ActionResult的ActionResult)在系统System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter中的.Web.Mvc.ControllerActionInvoker . <> c__DisplayClass1c.b__19()(IResultFilter f System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext,IList1过滤器,ActionResult)中的System.Web.Mvc.ControllerActionInvoker . <> c__DisplayClass1c . <> c__DisplayClass1e.b__1b()的ilter,ResultExecutingContext preContext,Func1 continuation) actionRes)位于System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,String actionName)的System.Web.Mvc.Controller.ExecuteCore()at [project_namespace] .Controllers.GlobalizationController.ExecuteCore()in [file_path] at at位于System.Web.Mvc.MvcHandler的System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)的System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) . <> c__DisplayClass6 . <> c__DisplayClassb.b__5()位于System.Web的System.Web.Mvc.Async.AsyncResultWrapper . <> c__DisplayClass1.b__0()处于System.Web的System.Web.Mvc.Async.AsyncResultWrapper . <> c__DisplayClass81.b__7(IAsyncResult _) . System.Web.Mvc上System.Web.Mvc.SecurityUtil.b__0(Action f)的System.Web.Mvc.MvcHandler . <> c__DisplayClasse.b__d()处的Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.End() . System.Web.HttpApplication.CallHandlerExecutionStep上的System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult结果)中的System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)上的SecurityUtil.ProcessInApplicationTrust(Action操作) . System.Web.HttpApplication.ExecuteStep中的System.Web.HttpApplication.IExecutionStep.Execute()(IExecutionStep step,Boolean&completedSyn chronously)

这完全阻止了我,虽然我想保留测试版,我可能需要完全删除它才能摆脱异常 .

9 回答

  • 4

    另外,最后一步从发生错误的项目中清除bin文件夹 .

  • 3

    尝试在web.config中添加以下内容:

    <appSettings>
      <add key="webpages:Version" value="1.0.0.0"/>
    </appSettings>
    

    问题可能是由于多个构建提供程序已注册且最新获胜 .

  • 5

    我遇到了同样的问题,但是当将ASP.NET MVC 3迁移到ASP.NET MVC 4时 . 这样我开始讨论这个主题 . 我找到了问题的解决方案,但它的来源不同,它不是来自 WebPages 版本 .

    我遵循了官方指南(可以这么说)Upgrading an ASP.NET MVC 3 Project to ASP.NET MVC 4 .

    要解决此问题,您必须添加Web.Config(可能是以前添加的):

    <runtime><!-- Should be there by default, near end -->
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><!-- Should be there by default -->
        <dependentAssembly>..</dependentAssembly><!-- Should be there by default -->
            ... some other dependecy redirects ...
    
        <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages.Razor"
                publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
        </dependentAssembly>
    
            ... some other dependency redirects ...
        </assemblyBinding>
    </runtime>
    

    另外在注释中没有提到将System.Web.Helpers的引用从1.0.0.0更改为2.0.0.0(删除并添加新的) .

  • 34

    还有一个,相当晚,解决方案......我遇到了同样的问题 . 这里列出了所有修复程序(谢谢大家!!!)但无法通过错误 . 最后,在绝望中,我在Views目录中找到了一个web.config文件 . 在这个文件中,我发现了几个对System.Web.Mvc版本的引用,并将所有引用都升级到“4.0.0.0” . 更重要的是,我找到了以下行,并将“system.web.webPages.Razor”的版本号从“1.0.0.0”更新为“2.0.0.0”,突然之间,所有内容都再次运行 . 希望,这将有助于其他人 .

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
    

    史蒂夫G.

  • 32

    我找到了另一种方法让这个工作,沿着台湾的博客从MVC 2升级到MVC 3:http://blog.kkbruce.net/2011_04_01_archive.html(你实际上可以在IE10中使用Microsoft Translate查看英文版本,这是一个相当不错的翻译.. . )

    诀窍是找到与MVC3相关的所有版本(例如Razor,Mvc,Helper程序集,如果你参考你的引用并查看所引用程序集的所有较新版本,你会知道它们是什么)并从中更改它们1.0.0.0到2.0.0.0和3.0.0.0到4.0.0.0(因为公钥是相同的) . 您也可以尝试根据博客删除版本 .

    另一个重要的事情是这些设置在所有Web.Configs中 . 所以不仅在你的根目录下,而且在View,Area / / View等下面 .

    我觉得这个解决方案可以让你完全升级到MVC4而不是混合动力,但这对我来说无效 .

  • 0
    • 在Visual Studio中,在某个单独的文件夹中创建一个新的"throwaway" ASP.NET MVC 4应用程序 .

    • 使用新一次性应用程序中新创建的Views web.config文件替换Views文件夹中的web.config文件 .

    你会注意到:

    System.Web.WebPages.RazorVersion=1.0.0.0 更改为 Version=2.0.0.0 System.Web.MvcVersion=3.0.0.0 更改为 Version=4.0.0.0

  • 0

    如果您遇到此问题并且您已尝试过上述解决方案但这不起作用,请检查Views文件夹中的web.config . 可能有一些配置与App根文件夹中的Web.config文件冲突

  • 0

    你的一些代码在当前的MVC位上运行,而其他代码则被推送到更新的代码 . 在不理解代码的情况下,我无法告诉您具体位导致了什么问题 .

    话虽如此,我绝不会在 生产环境 开发机器上设置测试版 . 这些天创建虚拟机并在映像上运行测试版太容易了 . 在Windows 7中,如果需要,可以从金属引导图像而不是执行perf命中(使用开发工具测试版,你不能这样做) .

  • 101

    为了为了完整...

    如果您不使用它们,也可以完全关闭网页 .

    <appSettings>
        <add key="webpages:Enabled" value="false" />
    </appSettings>
    

    (我仍然不完全清楚剃刀视图和网页之间的关系,但似乎我的MVC应用程序与剃刀视图仍然适用于禁用网页 . )

相关问题