Search code examples
c#razorintellisense

Compilation errors and no intellisense in cshtml file


I have a winforms application and I am trying to add a cshtml view to it. As per some articles on the web I have tried following to enable visual studio and intellisense support:

  • Adding Microsoft.AspNetCore.Mvc.Core package
  • Adding Microsoft.AspNetCore.Mvc.Razor package
  • Setting custom tool RazorTemplatePreprocessor

Nothing has worked so far, syntax is not recognized and file generates a lot of compilation errors, such as

compilation errors

My cshtml code:

@using Hobbysta.Store.Orders
@using RazorLight
@inherits TemplatePage<Order>

<html>
    Testowy template, @Model.General.id_order
</html>

My csproj file

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <nullable>enable</nullable>
    <WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
    <Version>0.5.1</Version>
  </PropertyGroup>

  <PropertyGroup>
    <PreserveCompilationReferences>true</PreserveCompilationReferences>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
    <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <WarningLevel>5</WarningLevel>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="Store\Orders\Print\OrdersPrintPageTemplate.cshtml" />
  </ItemGroup>

  <ItemGroup>
    <Compile Include="Store\Orders\Print\OrdersPrintPageTemplate.cshtml">
      <Generator>RazorTemplatePreprocessor</Generator>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GrEmit" Version="3.3.7" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />
    <PackageReference Include="NLog" Version="4.7.6" />
    <PackageReference Include="NLog.Extensions.Logging" Version="1.6.5" />
    <PackageReference Include="RazorLight" Version="2.0.0-rc.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Hobbysta\Hobbysta.csproj" />
    <ProjectReference Include="..\Hobbysta.Data\Hobbysta.Data.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Compile Update="Properties\Resources.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="Properties\Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>

  <ItemGroup>
    <Folder Include="RuntimeImplementations\" />
  </ItemGroup>

</Project>

Solution

  • I solved compilation errors by setting build action to none and for intellisense I had to restart visual studio.