Can’t create razor views in Class Library in .NetFramework

I get the following error in a .Net Framework project:

The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

This is unexpected, since I don’t have ‘AspNetCore’ written anywhere in my solution, and I am not trying to use a Core-dependency.

(For simplicity sake) I have two projects in my solution; a standard ASP.NET project and a class library.

The class library is created with the new project template:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net48</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
      <Content Include="Viewsweb.config" />
    </ItemGroup>
</Project>

The content of the class library is as such:

- Project.csproj
+ Controller
  - ProjectController
+ ViewModels
  - ProjectViewModel.cs
+ Views
  - ProjectView.cshtml
  - web.config

My ProjectView.cshtml is simpel as follow:
@model Project.ViewModels.ProjectViewModel

<p>I'm not even using the Model</p>

My ProjectViewModel.cs:

namespace Project.ViewModels
{
  public class ProjectViewModel
  {
     public string Title {get;set;}
  }
}

My web.config:

<?xml version="1.0"?>

<configuration>
    <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>
    </configSections>

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <pages pageBaseType="System.Web.Mvc.WebViewPage">
            <namespaces>
                <add namespace="System.Web.Mvc" />
                <add namespace="System.Web.Mvc.Ajax" />
                <add namespace="System.Web.Mvc.Html" />
                <add namespace="System.Web.Routing" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>

    <appSettings>
        <add key="webpages:Enabled" value="false" />
    </appSettings>

    <system.web>
        <httpRuntime targetFramework="4.8"/>
        <compilation targetFramework="4.8">
            <assemblies>
                <add assembly="System.Web.Mvc, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            </assemblies>
        </compilation>
    </system.web>

    <system.webServer>
        <handlers>
            <remove name="BlockViewHandler"/>
            <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
    </system.webServer>
</configuration>

I have a Directory.Build.props file:

<Project>
    <PropertyGroup>
        <LangVersion>8.0</LangVersion>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Castle.Core" Version="4.4.1" />
        <PackageReference Include="Castle.Windsor" Version="4.1.1" />
        <PackageReference Include="EntityFramework" Version="6.4.4" />
        <PackageReference Include="EPiServer.CMS.AspNet" Version="11.20.2" />
        <PackageReference Include="EPiServer.CMS" Version="11.20.2" />
        <PackageReference Include="EPiServer.CMS.AspNet" Version="11.20.2" />
        <PackageReference Include="EPiServer.CMS.Core" Version="11.20.2" />
        <PackageReference Include="EPiServer.CMS.TinyMce" Version="2.13.0" />
        <PackageReference Include="EPiServer.CMS.UI" Version="11.31.0" />
        <PackageReference Include="EPiServer.CMS.UI.AspNetIdentity" Version="11.31.0" />
        <PackageReference Include="EPiServer.CMS.UI.Core" Version="11.31.0" />
        <PackageReference Include="EPiServer.Framework" Version="11.20.2" />
        <PackageReference Include="EPiServer.Framework.AspNet" Version="11.20.2" />
        <PackageReference Include="EPiServer.Logging.Log4Net" Version="2.2.2" />
        <PackageReference Include="EPiServer.ServiceLocation.StructureMap" Version="2.0.3" />
        <PackageReference Include="EPiServer.XForms" Version="1.0.3" />
        <PackageReference Include="log4net" Version="2.0.12" />
        <PackageReference Include="Microsoft.AspNet.Identity.Core" Version="2.2.3" />
        <PackageReference Include="Microsoft.AspNet.Identity.EntityFramework" Version="2.2.3" />
        <PackageReference Include="Microsoft.AspNet.Identity.Owin" Version="2.2.3" />
        <PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
        <PackageReference Include="Microsoft.AspNet.Providers.Core" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNet.Razor" Version="3.2.7" />
        <PackageReference Include="Microsoft.AspNet.WebApi" Version="5.2.7" />
        <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
        <PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.7" />
        <PackageReference Include="Microsoft.AspNet.WebApi.Extensions.Compression.Server" Version="2.0.6" />
        <PackageReference Include="Microsoft.AspNet.WebApi.WebHost" Version="5.2.7" />
        <PackageReference Include="Microsoft.AspNet.WebPages" Version="3.2.7" />
        <PackageReference Include="Microsoft.Bcl" Version="1.1.10" />
        <PackageReference Include="Microsoft.Bcl.Build" Version="1.0.14" />
        <PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" Version="3.6.0" />
        <PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="1.3.6" />
        <PackageReference Include="Microsoft.Net.Compilers.Toolset">
            <Version>3.8.0</Version>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Net.Http" Version="2.2.29" />
        <PackageReference Include="Microsoft.Owin" Version="4.1.1" />
        <PackageReference Include="Microsoft.Owin.Host.SystemWeb" Version="4.1.1" />
        <PackageReference Include="Microsoft.Owin.Security" Version="4.1.1" />
        <PackageReference Include="Microsoft.Owin.Security.Cookies" Version="4.1.1" />
        <PackageReference Include="Microsoft.Owin.Security.OAuth" Version="4.1.1" />
        <PackageReference Include="Microsoft.Tpl.Dataflow" Version="4.5.24" />
        <PackageReference Include="Microsoft.VisualStudio.SlowCheetah">
            <Version>3.2.26</Version>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Web.Infrastructure" Version="1.0.0" />
        <PackageReference Include="Microsoft.Web.Xdt" Version="3.1.0" />
        <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
        <PackageReference Include="Owin" Version="1.0.0" />
        <PackageReference Include="StructureMap" Version="4.7.1" />
        <PackageReference Include="structuremap-signed" Version="3.1.6.191" />
        <PackageReference Include="structuremap.web" Version="4.0.0.315" />
        <PackageReference Include="structuremap.web-signed" Version="3.1.6.191" />
        <PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
        <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
        <PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
        <PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
        <PackageReference Include="System.Security.AccessControl" Version="4.7.0" />
        <PackageReference Include="System.Security.Cryptography.Xml" Version="4.7.0" />
        <PackageReference Include="System.Security.Permissions" Version="4.7.0" />
        <PackageReference Include="System.Security.Principal.Windows" Version="4.7.0" />
        <PackageReference Include="System.Threading.AccessControl" Version="4.7.0" />
        <PackageReference Include="System.ValueTuple" Version="4.5.0" />
    </ItemGroup>
</Project>

I get the error 14 times, and also this one time:

'_Views_ProjectView.ExecuteAsync()': no suitable method found to override

Does anyone have a clue to what could be wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is really a bug related to Intellisense in Visual Studio. When Microsoft.Net.Sdk is used in the project build then VS assumes that the Razor Views in the project should be compiled using aspnetcore for intellisense.

The errors are “false positives” and does not really occur at build time. If you run msbuild from the command line and/or use VS Code as IDE you’ll never see them (since they are not real and do not occur – it is a presumption made internally in VS that cause these afaik).

To continue using VS then you can simply change “Show output from” in the Output window to “Build only”. This will get rid of the errors. However it will not remove the red scribbled underlining in the cshtml but all intellisense, code completion etc. does work as normal. This is of course not a real fix which afaik would require Visual Studio to change how Razor views are handled by intellisense.

I’ve been told that it is unlikely that the VS team will prioritize fixing this. I will urge you to report it as a bug in Visual Studio.

If you’d like to change IDE to VS Code but depend on one or more VS Build targets such as WebApplication Webpublish you’ll either need to import these targets from f.ex a nuget or simply get rid of this build dependency.

Method 2

Something in your Directory.Build.props has core depedencies, such as Castle.Core. Can you load in AspNetCore or drop the .core dependencies?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x