Razor Host Factory error
Asked Answered
C

9

37

I get a MvcWebRazorHostFactory error trying to run my app, but it's not an MVC app at all. I have the following web packages installed via nuget:

Microsoft ASP.NET Razor
Microsoft ASP.NET Web API 2.1
Microsoft ASP.NET Web Pages

My app is angularjs front end using razor views (.cshtml). I don't understand why I keep getting this error.

An exception of type 'System.InvalidOperationException' occurred in System.Web.WebPages.Razor.dll but was not handled in user code

Additional information: Could not locate Razor Host Factory type: System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35

And here is the razor section in my web.config:

<system.web.webPages.razor>
<host factoryType="System.Web.WebPages.Razor.WebRazorHostFactory, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.WebPages.WebPage">
  <namespaces>
    <add namespace="System.Web.Configuration" />
    <add namespace="System.Web.Optimization" />
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>

Calgary answered 10/2, 2014 at 23:55 Comment(0)
P
46

I resolved this issue by setting System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc Version to 5.2.0.0 and it worked, finally! Why 5.2.0.0? Same version as NuGet Microsoft ASP.NET project.

So, the line in /Views/Web.Config should be:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
Pedagogics answered 7/7, 2014 at 17:13 Comment(6)
This is not working for me. Getting Could not locate Razor Host Factory type: System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35Onestep
Ok.. Turns out NuGet Microsoft ASP.NET version is now 5.2.2.0. so after changing the said version to 5.2.2.0 it's working.Onestep
Set the version to the version of System.Web.Mvc that is being used.Secondhand
This is in the Web.config that is inside /Views directory right?Javed
This is now: <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />Christophe
This is the best answer mainly because of the "Why" part. Just check your AspNet.Mvc package version, use that and it will work.Dreda
U
16

Several of the answers point in the right direction, but I was not sure where in the Microsoft Visual Studio for Mac interface I could find the appropriate version number to use for the version part of the factoryType attribute.

For the benefit of other VS for Mac users, the answer is to simply right-click on the Microsoft.AspNet.Mvc package in the Packages list in the solution explorer.

version number display

In my case, the package version was "5.2.6" after a package update, so I set the version part of the factoryType attribute to Version=5.2.6.0. This solved the problem.

Ubiety answered 31/7, 2018 at 12:59 Comment(0)
E
15

For the benefit of anyone developing ASP.NET MVC 5 web apps using Xamarin Studio v5.5.3 on OSX, deleting the /Views/Web.config did not work for me and created other errors.

To fix it, open /Views/Web.config and change all instances of 5.2.0.0 to 5.2.2.0

More details here

Extravert answered 3/11, 2014 at 4:40 Comment(1)
Thank you! Changing "3.0.0.0" to "5.2.2.0" works on Monodevelop.Mandler
O
4

Changed the version of MvcWebRazorHostFactory to the same version of System.Web.Mvc which resolved my issue.

Oberammergau answered 5/5, 2018 at 18:18 Comment(0)
C
3

So apparently some package I got from nuget adds a web.config to the Views folder. In this web.config there were settings for the MvcWebRazorHostFactory. I think this occurred when I added a View to my project using the dialog box.

The solution is to remove the web.config from the Views folder.

Calgary answered 11/2, 2014 at 14:0 Comment(2)
can you elaborate? I'm having the same issue. Did you remove some lines from your Views/web.config? Did you change version numbers? thanks!Zoroaster
I removed it completely.Calgary
J
2

I started getting this error in my Razor .cshtml pages in an MVC project following some NuGet package management / upgrading.

I didn't want to remove the Web.config file completely from the Views folder because I had customised it width some <add namespace="..." /> elements. But I noticed that the Version=... in the <host factoryType="..." /> didn't match my version of System.Web.Mvc (checked version in Object Browser).

Altering this version number to match that of my System.Web.Mvc assembly, and restarting Visual Studio, fixed the problem for me (as suggested by Daniel)

Jeaz answered 6/11, 2014 at 11:46 Comment(0)
T
1

This happened to me today. I just made sure the version number for the web.config in the views folder matches the version number for the web.config of the solution.

Hope this helps.

Trap answered 24/10, 2018 at 13:32 Comment(0)
T
1

For MacBook users having the Visual Studio 2019, you simply go to the Web.config file and update version of the System.Web.Mvc to that which is in your Nuget packages folder.

Toil answered 30/5, 2020 at 21:9 Comment(0)
A
0
  1. Inside your Web project, open "References" tree node.
    1. Look for "Assemblies" folder and open it.
    2. Look for System.Web.Mvc, right click on it, and select "Properties"

A dialog will prompt, read carefully the package full name field and look for the version number. For example, I have this one:

System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

So, in the Web.config (inside Views folder) replace the version number!

In my case, it originally was:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Now I have (look at the Version value):

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

Hope it helps!

Actinon answered 14/5, 2019 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.