Razor Host Factory error

2019-02-02 21:18发布

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>

7条回答
SAY GOODBYE
2楼-- · 2019-02-02 21:31

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.

查看更多
太酷不给撩
3楼-- · 2019-02-02 21:33

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)

查看更多
该账号已被封号
4楼-- · 2019-02-02 21:36

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.

查看更多
一夜七次
5楼-- · 2019-02-02 21:43

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

查看更多
Fickle 薄情
6楼-- · 2019-02-02 21:51

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.

查看更多
Summer. ? 凉城
7楼-- · 2019-02-02 21:53

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

查看更多
登录 后发表回答