The following error occurs in a project upgraded from Asp.net MVC 3 to Asp.net MVC 4:
[A] system. web. webpages. razor. configuration. hostsection cannot be cast to [B] system. web. webpages. razor. configuration. hostsection. type A originates from 'System. web. webpages. razor, version = 1.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35 'in the context 'default' at location 'C: \ windows \ Microsoft. net \ Assembly \ gac_msil \ system. web. webpages. razor \ v4.0 _ 1.0.0.0 _ 31bf3856ad364e35 \ system. web. webpages. razor. dll '. type B originates from 'System. web. webpages. razor, version = 1.0.0.0, culture = neutral, publickeytoken = null' in the context 'default' at location 'C: \ windows \ Microsoft. net \ framework \ v4.0.30319 \ temporary ASP. net files \ Root \ 7c2060f6 \ 6145b34e \ Assembly \ dl3 \ 9036b7c7 \ fc8023c0_73fecb01 \ system. web. webpages. razor. dll '.
This is because the localProgramConflicts with the GAC Assembly because a configuration node is added to Web. config in Web pages 1.0. Web pages 1.0 in Web. config, I have found a new configuration node ~ /Views/Web. config. Instead of Web. config.
<Configuration>
<System. Web. webpages. Razor/> <! -- Pay attention to the common system. Web peer->
<System. Web/>
...
</Configuration>
Modify ~ /Views/Web. config is as follows:
<? XML version = "1.0" ?> <Configuration> <configsections> <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> </configsections> <system. Web. webpages. Razor> "System. Web. MVC. mvcwebrazorhostfactory, system. Web. MVC, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" /> <Pages pagebasetype ="System. Web. MVC. webviewpage" > <Namespaces> <add Namespace = "System. Web. MVC" /> <Add Namespace = "System. Web. MVC. Ajax" /> <Add Namespace = "System. Web. MVC. html" /> <Add Namespace = "System. Web. Optimization" /> <Add Namespace = "System. Web. Routing" /> </Namespaces> </pages> </system. Web. webpages. Razor> <shortettings> <add key = "Webpages: enabled" Value = "False" /> </Appsettings> <system. Web> "*" Verb = "*" Type = "System. Web. httpnotfoundhandler" /> </Httphandlers> <! -- Enabling request Validation In View pages wocould cause validation to occur after the input has already been processed by the Controller. Default MVC performs request validation before a controller processes the input. To change This Behavior apply the validateinputattribute to a controller or action. --> <pages validaterequest = "False" Pageparserfiltertype = "System. Web. MVC. viewtypeparserfilter, system. Web. MVC, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" Pagebasetype = "System. Web. MVC. viewpage, system. Web. MVC, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" Usercontrolbasetype = "System. Web. MVC. viewusercontrol, system. Web. MVC, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" > <Controls> <add Assembly ="System. Web. MVC, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" Namespace = "System. Web. MVC" Tagprefix = "MVC" /> </Controls> </pages> </system. Web> <system. webserver> <validation validateintegratedmodeconfiguration = "False" /> <Handlers> <remove name = "Blockviewhandler" /> <Add name = "Blockviewhandler" Path = "*" Verb = "*" Precondition = "Integratedmode" Type ="System. Web. httpnotfoundhandler" /> </Handlers> </system. webserver> </configuration>
Then modify ~ /Web. config file
<System. Web>
<Compilation DEBUG = "true" targetframework = "4.0">
<Assemblies>
<Add Assembly = "system. Web. Export actions, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Add Assembly = "system. Web. helpers, version = 2.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Add Assembly = "system. Web. Routing, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Add Assembly = "system. Web. MVC, version = 4.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
<Add Assembly = "system. Web. webpages, version = 2.0.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/>
</Assemblies>
</Compilation>
<Authentication mode = "forms"> </authentication>
<Pages>
<Namespaces>
<Add namespace = "system. Web. helpers"/>
<Add namespace = "system. Web. MVC"/>
<Add namespace = "system. Web. MVC. Ajax"/>
<Add namespace = "system. Web. MVC. html"/>
<Add namespace = "system. Web. Routing"/>
<Add namespace = "system. Web. webpages"/>
</Namespaces>
</Pages>
</System. Web>
For more information, see ASP. Net mvc3 series tutorials-how to debug the project into the mvc3 source code.