Error with System.Web.Webpages.Razor.dll

Asked

Viewed 2,145 times

2

I tried to install the package install-package microsoft.aspnet.mvc.en for javascript in Portuguese and my application started not running anymore, giving error where everything points out that it is with Razor, I tried everything, until remove the reference and addThere again, but the problem persists. Follow the error:

[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\c18d8738\e9228a3c\assembly\dl3\a1b5a49f\18a5dcd1_85f4d001\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\c18d8738\e9228a3c\assembly\dl3\a1b5a49f\18a5dcd1_85f4d001\System.Web.WebPages.Razor.dll'.

Source Error: 

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.

Stack Trace: 


[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\c18d8738\e9228a3c\assembly\dl3\a1b5a49f\18a5dcd1_85f4d001\System.Web.WebPages.Razor.dll'.]
   System.Web.WebPages.Razor.WebRazorHostFactory.GetRazorSection(String virtualPath) +151
   System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(String virtualPath, String physicalPath) +210
   System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(String virtualPath) +38
   System.Web.WebPages.Razor.RazorBuildProvider.GetHostFromConfig() +51
   System.Web.WebPages.Razor.RazorBuildProvider.CreateHost() +57
   System.Web.WebPages.Razor.RazorBuildProvider.get_Host() +56
   System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode() +77
   System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType() +54
   System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) +59
   System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() +209
   System.Web.Compilation.BuildProvidersCompiler.PerformBuild() +30
   System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +9973313
   System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +299
   System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +103
   System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +165
   System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath) +10
   System.Web.Compilation.BuildManager.GetCompiledType(String virtualPath) +28
   System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetCompiledType(String virtualPath) +38
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +155
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
   System.Web.Mvc.Async.c__DisplayClass2b.b__1c() +321
   System.Web.Mvc.Async.c__DisplayClass21.b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651796
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

  • Did you start this project on MVC4? The version of Razor pointed out is like the 2. I need you in your question the content of Web.Config which is inside the directory Views.

  • started with this <package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />

  • 2

    It’s very outdated your project. I’ll try an answer.

  • Hello! Dear friend, I am leaving here my comment as I went through a problem similar to this post. since I migrated the project from VS2015 to VS2017, I made the change in "nail" according to this indication of the post (System.Web.Webpages.Razor, Version=2.0.0.0,) putting to 3.0.0.0 and it worked perfectly. Hug!

  • Hi Alessandro! I did the same too, unfortunately Visual Studio has some problems that is solved only on the gambiarra. But anyway thanks for your comment.

2 answers

1

You are using packages of mixed versions of MVC. Apparently your project was started in version 4, and now you want to use packages of version 5.

Make a update complete your project, opening the Package Manager Console and typing the following:

Update-Package

Make a backup of your project instead.

Another thing that may be cause for conflict is the . NET Framework version which, for MVC5, needs to be the 4.5.

Right-clicking on the project, select Properties. Inside the tab Application see the field Target Framework. If you are in version 4, switch to 4.5.

  • Cigado is now giving the following error: Exception Details: System.IO.Filenotfoundexception: Could not load file or Assembly 'System.Web.Razor, Version=3.0.0.0, Culture=neutral, Publickeytoken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

  • Make a Clean, then a Rebuild solution. Also check the references if none of the packages is marked with a yellow icon.

  • Yes, I did that. Even after the package update I closed Visual Studio and opened again and made a Clean. But the problem of the last message persists. The references are all correct, with no warning icon. I also noticed the packages that have been updated to Razor <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />, could not be the cause of this message of not locating a system file?

  • No, now must be the Framework version. I updated the answer for you.

  • It is already in 4.5, has the option for 4.5.1, I made a copy and changed to this version and continues with the error: Exception Details: System.IO.Filenotfoundexception: Could not load file or Assembly 'System.Web.Razor, Version=3.0.0.0, Culture=neutral, Publickeytoken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

  • Have you checked the References? The System.Web.Razor appears there?

  • Yes, it is linked to both the system.web.Razor and the system.web.webpages.Razor, but one thing I noticed is that even removing and adding again the system.web.webpages.razo, it is not in the list, it only appears the system.web.Razor.

  • 1

    You’ll need to reinstall Razor: Update-Package Microsoft.AspNet.WebPages -reinstall.

  • Cigado, he went back to the initial error, I noticed that in my web.config of View continues stating that the version of Razor is 2.0.0.0 even after all that we did. The error is the same as at the beginning: Type A originates from 'System.Web.Webpages.Razor, Version=2.0.0.0, Culture=neutral, Publickeytoken=31bf3856ad364e35' in the context 'Default' at Location

  • Cigado, I managed to solve the problem. I commented everywhere where I reported version 2.0.0.0 and manually informed the version you asked to update and it worked. There would be another way for him to update Razor and modify it automatically without me having to do it manually?

  • 1

    Not automatically. What exists is this guide that Microsoft has built. Check in this tutorial if nothing was missing.

  • 1

    Thank you Gypsy!

Show 7 more comments

0

You can simply change the version in the "nail" the Web.config file inside the View folder;

type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, 
 System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, 
 PublicKeyToken=31BF3856AD364E35">

SUBSTITUTE FOR:

type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,
 System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, 
 PublicKeyToken=31BF3856AD364E35">

Take the test.

  • Hello Diego, about this old problem, I already tried it too. However all my Helpers are as not existing in Visual Studio.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.