program tip

ASP.NET MVC 4 베타를 설치 한 후 InvalidCastException이 throw됩니다.

radiobox 2020. 9. 24. 07:42
반응형

ASP.NET MVC 4 베타를 설치 한 후 InvalidCastException이 throw됩니다.


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는 'C : \ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ System'위치의 컨텍스트 'Default'에있는 'System.Web.WebPages.Razor, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35'에서 시작됩니다. .Web.WebPages.Razor \ v4.0_1.0.0.0__31bf3856ad364e35 \ System.Web.WebPages.Razor.dll '. 유형 B는 'C : \ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ System'위치의 컨텍스트 'Default'에있는 'System.Web.WebPages.Razor, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35'에서 시작됩니다. .Web.WebPages.Razor \ v4.0_2.0.0.0__31bf3856ad364e35 \ System.Web.WebPages.Razor.dll '. Source = System.Web.WebPages.Razor

StackTrace :

System.Web.WebPages.Razor.WebRazorHostFactory.GetRazorSection (String virtualPath) at System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig (String virtualPath, String physicalPath) at System.Web.WebPages.Razor.RazorBuildProvider.GetHostFromConfig () at System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType ()의 System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode ()의 System.Web.WebPages.Razor.RazorBuildProvider.CreateHost ()의 System.Web.Compilation.BuildProvider. System.Web.Compilation.BuildProviders의 System.Web.Compilation.BuildProvidersCompiler.PerformBuild ()의 System.Web.Compilation.BuildManager.CompileWebFile (VirtualPath virtualPath)의 System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders ()의 GetCompilerTypeFromBuildProvider (BuildProvider buildProvider) BuildManager.GetVPathBuildResultInternal (VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildAppWithNoAssert (HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean throwIfNotPreFound, Boolean, Boolean system.Web.Compilation.BuildManager.GetVirtualPathObjectFactory (VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists (String virtualPath) at System.Web.Compilation. System.Web.Mvc.VirtualPathProviderViewEngine의 Web.Mvc.BuildManagerViewEngine.FileExists (ControllerContext controllerContext, String virtualPath)System.Web.Mvc.VirtualPathProviderViewEngine.GetPath (ControllerContext controllerContext, String [] 위치, String []의 GetPathFromGeneralName (ControllerContext controllerContext, List`1 위치, 문자열 이름, 문자열 컨트롤러 이름, 문자열 영역 이름, 문자열 cacheKey, 문자열 [] & searchedLocations) System.Web의 System.Web.Mvc.VirtualPathProviderViewEngine.FindView (ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)의 areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String [] & searchedLocations) .Mvc.ViewEngineCollection. <> c__DisplayClassc.b__b (IViewEngine e) at System.Web.Mvc.ViewEngineCollection.Find (Func`2 lookup, Boolean trackSearchedPaths) at System.Web.Mvc.ViewEngineCollection.System.Web.Mvc.ViewResultBase.ExecuteResult (ControllerContext context)의 System.Web.Mvc.ViewResult.FindView (ControllerContext context)에서 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult ()의 FindView (ControllerContext controllerContext, String viewName, String masterName) System.Web.Mvc.ControllerActionInvoker. <> c__DisplayClass1c.b__19 ()의 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter (IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)의 ControllerContext controllerContext, ActionResult actionResult) .ControllerActionInvoker. <> c__DisplayClass1c. <> c__DisplayClass1e.b__1b () at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters (ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction (ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore () at [project_namespace] .Controllers.GlobalizationController.ExecuteCore () at [file_path] at System.Web.Mvc.ControllerBase.Execute (RequestContext) requestContext) at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute (RequestContext requestContext) at System.Web.Mvc.MvcHandler. <> c__DisplayClass6. <> c__DisplayClassb.b__5 () at System.Web.Mvc .Async.AsyncResultWrapper. <> c__DisplayClass1.b__0 () at System.Web.Mvc.Async.AsyncResultWrapper. <> c__DisplayClass8`1.b__7 (IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End () System.Web.Mvc.MvcHandler. <> c__DisplayClasse.b__d () at System.Web.Mvc.SecurityUtil.b__0 (Action f) at System.Web.Mvc.SecurityUtil.System.Web.Mvc.MvcHandler.EndProcessRequest (IAsyncResult asyncResult)의 ProcessInApplicationTrust (Action action) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest (IAsyncResult result) at System.Web.HttpApplication.CallHandlerExecutionStep.System. System.Web.HttpApplication.ExecuteStep (IExecutionStep 단계, Boolean & completedSynchronously)의 Web.HttpApplication.IExecutionStep.Execute ()

이것은 나를 완전히 차단하고 베타를 유지하고 싶지만 예외를 제거하기 위해 완전히 제거해야 할 수도 있습니다.


web.config에 다음을 추가해보십시오.

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

이 문제는 여러 빌드 공급자가 등록되고 최신 우승자가 있기 때문일 수 있습니다.


나는 같은 문제가 있었지만 ASP.NET MVC 3을 ASP.NET MVC 4로 마이그레이션 할 때이 주제를 다루었습니다. 문제에 대한 해결책을 찾았지만 소스가 다르며 WebPages버전 이 아닙니다 .

나는 공식 가이드 (즉 , ASP.NET MVC 3 프로젝트를 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으로 변경 (새 항목을 삭제하고 추가)하도록 언급되지 않았습니다.


One more, rather late, solution ... I ran into this same problem. Did all the fixes listed here (Thank You All!!!) but could not get past the error. Finally, in desperation, I found a web.config file in the Views directory. In this file, I found several references to the version of System.Web.Mvc and bumped all of them up to "4.0.0.0". More importantly, I found the following lines and updated the version numbers for "system.web.webPages.Razor" from "1.0.0.0" to "2.0.0.0" and suddenly, everything was working once again. Hopefully, this will help someone else.

<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>

Steve G.


I found another way of getting this to work, along the line to a Taiwanese's blog on upgrade from MVC 2 to MVC 3: http://blog.kkbruce.net/2011_04_01_archive.html (You can actually use Microsoft Translate in IE10 to see the English version and it is a fairly good translation...)

The trick is to find all the versions that relate to MVC3 (such as Razor, Mvc, Helper assemblies, you will know what they are if you go to your references and check out all the newer versions of your referenced assembly) and change them from 1.0.0.0 to 2.0.0.0 and from 3.0.0.0 to 4.0.0.0 (since the public keys are the same). You can also try removing the versions as per the blog.

Another important thing is that these settings are in all the Web.Configs. So not only under you root, but also under View, Area/{Area}/View, etc.

I feel this solution can let you upgrade to the MVC4 completely instead of having a hybrid, which didn't work for me anyways.


  1. In Visual Studio, create a new "throwaway" ASP.NET MVC 4 Application in a separate folder somewhere.
  2. Replace the web.config file in your Views folder with the freshly created Views web.config file from the new throwaway application.

You will notice:

System.Web.WebPages.Razor changes from Version=1.0.0.0 to Version=2.0.0.0 System.Web.Mvc changes from Version=3.0.0.0 to Version=4.0.0.0


If you are experiencing this issue and you have tried the solutions above and this not work, pls check the web.config in your Views folder. There may be some configurations their that is conflicting the Web.config file in your App root folder


Some of your code is running on the current MVC bits, while others have been pushed out to the newer. Without understanding the code, I can't tell you what specific bit is causing the issue.

Having said that, I would never set up a beta on a production development machine. It is too easy to create a VM these days and run the beta on an image. In Windows 7+, you can boot the image off the metal and not take the perf hit, if you need that (with dev tool betas, you oft do).


For the sake of completeness...

You can also just turn off webpages altogether if you aren't using them.

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

(I'm still not entirely clear on the relationship between razor views and webpages, but it seems my MVC app with razor views still works with webpages disabled.)


Also as last step clear your bin folder from the project where the error is coming from.

참고URL : https://stackoverflow.com/questions/9403477/invalidcastexception-is-thrown-after-installing-asp-net-mvc-4-beta

반응형