10

I am using Nancy and Razor together to build my web app. I am able to serve up Razor views, and they display totally fine. However, Visual Studio 2015 reports errors on almost every line in my .cshtml file, despite there being no run-time errors at all:

The specific errors that I have seen are:

  • Predefined type 'System.Boolean' is not defined or imported
  • Predefined type 'System.String' is not defined or imported
  • The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I believe that I have configured my app.config correctly, as per the documentation:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
    <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
  </configSections>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <add key="webPages:Enabled" value="false" />
  </appSettings>
  <system.web.webPages.razor>
    <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase">
      <namespaces>
        <add namespace="Nancy.ViewEngines.Razor" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  <razor disableAutoIncludeModelNamespace="false">
    <assemblies>
      <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <add assembly="mscorlib , Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </assemblies>
  </razor>
  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add assembly="mscorlib , Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

I believe however that the documentation might be a little out of date. It makes references to an ASP.NET MVC installer which I don't believe exists any more; I believe this is now built in to Visual Studio 2015.

I have tried writing all assemblies into /bin, as per this SO question, but this had no effect. I guess that something is missing from the Nancy documentation, and if someone would be kind enough to tell me what that is, I'll contribute it.

Community
  • 1
  • 1
Steve Rukuts
  • 9,167
  • 3
  • 50
  • 72
  • You'll probably want to specify what version of Visual Studio you're using. I remember having the same issue the one time I tried using the full version of Razor with Nancy. I think the problem was something like Nancy isn't a full-version of it, so there is a Nancy-specific version of the Razor engine. – krillgar Feb 16 '16 at 15:41
  • If you have `` in your web.config, I'm not sure you also need to specify that your view inherits from it too. Can you remove that line and just use a standard `@model ...` line? – DavidG Feb 16 '16 at 15:42
  • @DavidG - when I specify `@model`, it says that the "model" symbol does not exist. An exception is then thrown when I try to view a page using that template. – Steve Rukuts Feb 16 '16 at 16:08
  • From the docs you linked, it also says you need to use `@Model...` and not `@model...`. Not used Nancy before so can't be more helpful. – DavidG Feb 16 '16 at 16:10
  • @DavidG - thanks for giving it a shot. I thought you meant to specify `@model MyApp.MyModel` - that would normally work in MVC5. The docs are actually saying that the model contents are available from `@Model`, which is what I am doing in my templates. Your post did send me down some avenues of inquiry that I hadn't looked down before though so that's still helpful. – Steve Rukuts Feb 16 '16 at 16:15
  • Also, it's worth looking in the `Views\web.config` file as that might be overriding anything you do in the main config. – DavidG Feb 16 '16 at 16:17
  • I don't have a web.config file in my Views directory. I tried copying one there but that didn't help either. I also tried calling it "app.config" as my application is actually a .exe with a built-in web server but this didn't help. – Steve Rukuts Feb 16 '16 at 16:23

0 Answers0