16

I am trying to launch MVC5 website on my Linux box using Mono and XSP4. It works with no views however when I try to render something it gives me errors.

Here is my test code. Note that I didn't change anything, this is basically a blank site without EF or any other libraries. Just bare-bone MVC5 + Razor.

public ActionResult Index()
{
    // return Content("'sall good"); // works
    return View();
}

System.InvalidOperationException

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

Description: HTTP 500.Error processing request.

Details: Non-web exception. Exception origin (name of application or object): System.Web.WebPages.Razor.

Stacktrace

at System.Web.WebPages.Razor.WebRazorHostFactory.CreateFactory (System.String typeName) [0x00000] in <filename unknown>:0

at System.Collections.Concurrent.ConcurrentDictionary`2+<GetOrAdd>c__AnonStorey3[System.String,System.Func`1[System.Web.WebPages.Razor.WebRazorHostFactory]].<>m__0 () [0x00000] in <filename unknown>:0 

at (wrapper delegate-invoke) System.Func`1<System.Collections.Generic.KeyValuePair`2<string, System.Func`1<System.Web.WebPages.Razor.WebRazorHostFactory>>>:invoke_TResult__this__ ()

...

I've tried to change Version=5.0.0.0 to 4.0.0.0 and 3.0.0.0 etc but nothing works. I still get the same error just now it's about 4.0.0.0.

Is there any hope?

Stan
  • 25,744
  • 53
  • 164
  • 242
  • Have you tried: sudo apt-get install asp.net-examples – David Crowell Apr 21 '14 at 17:32
  • @DavidCrowell No, AFAIK they are pointless because they are 3+ years old. They show how MVC2 works. I get mono from git and compile from sources since official site is lagging behind. And mono in aptitude repositories is also very old version. – Stan Apr 21 '14 at 17:35
  • Do you have log4net set up? TypeFactory (referenced in WebRazorHost) uses log4net. – David Crowell Apr 21 '14 at 17:46
  • there are people that have contributed pull requests to mono to make MVC5 work, you should compile mono with those pull requests included to see if they help, and if yes, add a "+1" on the pull request (which will hopefully speed up the review process) – knocte Apr 23 '14 at 09:34
  • @knocte I couldn't find that pull-request. I think it was deleted. This week I tried to test it again using latest mono from git but seems like it wasn't touched at all. Here is my testing project btw: https://github.com/stanislavromanov/MVC5Mono – Stan Jun 22 '14 at 21:10
  • pull requests cannot be deleted, it is here: https://github.com/mono/mono/pull/888 – knocte Jun 22 '14 at 22:10

6 Answers6

32

The problem is in Web.config that is inside /Views folder. system.web.webPages.razor host parameter must match exactly with MVC version.

For example if you have MVC 5.2.2.0 then it should look like <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />.

Stan
  • 25,744
  • 53
  • 164
  • 242
  • 1
    there are several versions of this question and answer all over stack overflow .. this one is the best though, as you actually point out the location -- so people can compare the outer one to the one in view in case there is an override on version in the outer config – roberto tomás Apr 25 '20 at 14:08
  • 2
    Thank you! In my case, VSMac updated `Microsoft.AspNet.Mvc` package to `5.2.7` but didn't touch the `Web.config` file under `Views/` folder which had `5.2.3` as the version. – ogirginc Jun 13 '20 at 14:05
6

The MVC version of your project must match the version of MvcWebRazorHostFactory, it must be the same. I had to change the version to 5.2.3.0 (in the Views/Web.config file).

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0

If you don't know which version of MVC you are using in your project, then go to Project -> Edit references -> then on the right side, under "Selected references" scroll down to System.Web.Mvc where should should see which version you are using. This is how i found out in Xamarin Studio Community.

tedi
  • 6,350
  • 5
  • 52
  • 67
1

I received the same error after I updated my packages and ran the code.

Error: System.InvalidOperationException Could not locate Razor Host Factory type: System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35

Change the updated version for System.Web.Mvc in Web.config file under "Views" folder

system.web.webPages.razor

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

I changed my version of System.Web.Mvc from 5.2.3.0 to 5.2.7.0 and the code executed.

0

This one is work for me. I changed Web.config in the Views folder, Mvc version as 5.2.3.0 according to my Mvc version. Eg. <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

I changed my Version from 5.2.0.0 to 5.2.3.0.

Thili77
  • 1,061
  • 12
  • 20
0

@stan solution helped me to solve the issue.

I am using Visual Studio on Mac M1 chip.

In my case, I fixed the following things inside views/web.config file

<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

<pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <controls>
    <add assembly="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
  </controls>
</pages>

Update the versions as per References > Packages, check the version there, and update it.

Rebuild Project/Solution

Sajjad Ali Khan
  • 1,735
  • 2
  • 20
  • 17
-1

It's just like Stan says. Check your version of the System.Web.MVC doing "right click > Properties" at your Reference folder (in your project). Then, update that line in your /views/web.config.

bastelflp
  • 9,362
  • 7
  • 32
  • 67