Home Multiple Errors in Razor -- Missing Include? -- Visual Studio for Mac - C# / MVC / Entity Framework
Reply: 1

Multiple Errors in Razor -- Missing Include? -- Visual Studio for Mac - C# / MVC / Entity Framework

danielc
1#
danielc Published in 2018-02-09 22:41:17Z

I am experimenting with Visual Studio on the Mac. I have a _layout.cshtml file that works fine in the Windows version of VS but I am getting multiple errors when executing it on the Mac.

The error is:

One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.

And it is flagging many common razor function such as

The type or namespace name 'HtmlString' could not be found (are you missing a using directive or an assembly reference?)

private HtmlString WriteOption(string Menu)

The name 'Styles' does not exist in the current context

@Styles.Render( "~/Content/css" )

The name 'Scripts' does not exist in the current context

@Scripts.Render( "~/bundles/modernizr" )

The name 'Session' does not exist in the current context

int CurrentCustomerId = (Session["CurrentCustomerId"] == null ? -1 : (int)Session["CurrentCustomerId"]);

I've tried numerous things such as

  • adding and removing Microsoft.AspNetCore
  • ensuring PreserveCompilationContext is set to true
  • adding Microsoft.AspNetCoreRazor packages
  • adding: @using HttpContext.Current;
  • adding: @using System.Web.Optimization;

  • rebooting, cleaning and rebuilding....

This is what my .csproj file looks like:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
    <PackageReference Include="jQuery" Version="3.3.1" />
    <PackageReference Include="bootstrap" Version="4.0.0" />
    <PackageReference Include="log4net" Version="2.0.8" />
    <PackageReference Include="FontAwesome" Version="4.7.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
    <PackageReference Include="EntityFramework" Version="6.2.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNet.Razor" Version="3.2.3" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Data\" />
    <Folder Include="Views\Customer\" />
    <Folder Include="Views\Contact\" />
    <Folder Include="Views\User\" />
    <Folder Include="wwwroot\img\" />
    <Folder Include="Helpers\" />
  </ItemGroup>
  <ItemGroup>
    <Compile Remove="Views\Customer\Index.cshtml.cs" />
  </ItemGroup>
  <ItemGroup>
    <Content Remove="Views\Home\About.cshtml" />
    <Content Remove="Views\Home\Contact.cshtml" />
    <Content Remove="wwwroot\images\banner1.svg" />
    <Content Remove="wwwroot\images\banner2.svg" />
    <Content Remove="wwwroot\images\banner3.svg" />
    <Content Remove="wwwroot\images\banner4.svg" />
  </ItemGroup>
</Project>

Here's the IDE vitals:

Visual Studio Community 2017 for Mac
Version 7.3.3 (build 12)
Installation UUID: 0237be3a-3db7-4f5c-af82-a9699df5a1f9
Runtime:
    Mono 5.8.0.108 (2017-10/9aa78573ee2) (64-bit)
    GTK+ 2.24.23 (Raleigh theme)

    Package version: 508000108

NuGet
Version: 4.3.1.4445

.NET Core
Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.0.5
SDK: /usr/local/share/dotnet/sdk/2.1.4/Sdks
SDK Version: 2.1.4
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.8.0/lib/mono/msbuild/15.0/bin/Sdks

Any suggestions?

Oli
2#
Oli Reply to 2018-02-10 00:21:34Z

There is a difference between "ASP.NET Web Application" and "ASP.NET Core Web Application". Your project is of the first type which uses .Net Framework and does not work on Mac. The "Core" type uses .Net Core and is supported on Mac.

The project types have some differences, like the absence of @Styles and @Scripts. More differences can be found here: http://www.mithunvp.com/difference-between-asp-net-mvc6-asp-net-mvc5/

If you would like to migrate your project, you can use this guide: https://docs.microsoft.com/en-us/aspnet/core/migration/mvc

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.406224 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO