Question

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?

Was it helpful?

Solution

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" />.

OTHER TIPS

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.

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.

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.

@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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top