Friday, March 21, 2014

Solving System.InvalidCastException was unhandled by user code Message=[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast ......

Recently, while upgrading an Asp.Net MVC 4 project someone created to an Asp.Net MVC 5 project, I ran across a great error. The error was this:

It was also bright and red!


Server Error in '/' Application.

[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=2.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_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\bc28c2e0\1b833c84\assembly\dl3\efd960b2\2bccde4f_7f44cf01\System.Web.WebPages.Razor.dll'.

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: [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=2.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_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\bc28c2e0\1b833c84\assembly\dl3\efd960b2\2bccde4f_7f44cf01\System.Web.WebPages.Razor.dll'.

This error made me VERY unhappy.
However, the solution was VERY simple.
I just needed to tell the application which version to use.
Here are the steps I took.


1. Open the web.config file.
2. Scroll until you find the <assemblybinding> tag under <runtime>.
3. Add in the following dependent assembly information for the dll that is causing you problems. In my case, it was as follows:

<dependentAssembly>

<assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="2.0.0.0" newVersion="3.0.0.0"/>

</dependentAssembly>

4. Rebuild your project!

That is it! You should now see the fantastic work you have created after you build and Ctrl-F5. For all of you people out there still making projects with MVC 4 - JUST STOP! Back away slowly! MVC 5 is your friend!

Smooches,
Kila Morton

6 comments:

Anonymous said...

awesome! thanks

TheDouceHouse said...

I checked about 10 different solutions before I found this. Yours worked!!! Thanks.

Zheng junxai5 said...

zhengjx20160726
louis vuitton handbags
timberland outlet
gucci outlet
beats by dre outlet
coach outlet store online clearances
michael kors outlet
louis vuitton bags
louis vuitton purses
nike air max
louis vuitton outlet
michael kors outlet online
celine handbags
christian louboutin wedges
kate spade outlet
michael kors outlet
michael kors canada outlet
michael kors outlet clearance
michael kors outlet
jordan concords
coach outlet clearance
michael kors outlet online
air max 95
ralph lauren outlet
supra footwear
kate spade handbags
adidas superstar trainers
louboutin shoes
toms outlet
coach outlet
louis vuitton bags
michael kors outlet
ray ban sunglasses outlet
discount jordans
adidas nmd
coach outlet
coach outlet
louis vuitton purses
jordan retro 8
kobe 8
ralph lauren

fattoma said...

https://decor-ksa.com/laser-railings/
https://decor-ksa.com/iron-railings/
https://decor-ksa.com/stainless-steel-handrail/
http://mchaabaty.com/factory/
http://mchaabaty.com/new/
http://mchaabaty.com/marble/

dong dong23 said...

coach factory outlet
gucci handbags
louis vuitton outlet
ugg outlet online
adidas yeezy boost 350
coach factory outlet
oakley sunglasses outlet
coach outlet online
adidas superstar
toms outlet
2016.12.26chenlixiang

tinakmoffatas said...

Because of this there is a bricksite.com/ demand for such apps Click here which could help the customer manage his/her QuickShortcutMaker Install Download For Android Free | QuickShortcutMaker APK functional attributes on the Quick Shortcut Maker gadget conveniently. To do this, QuickShortcutMaker the most effective app is the Quick Shortcut Maker.