简体   繁体   English

无法在.NetFramework 的 Class 库中创建 razor 视图

[英]Can't create razor views in Class Library in .NetFramework

I get the following error in a.Net Framework project:我在 a.Net Framework 项目中收到以下错误:

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.这是出乎意料的,因为我的解决方案中的任何地方都没有写“AspNetCore”,而且我也没有尝试使用核心依赖项。

(For simplicity sake) I have two projects in my solution; (为简单起见)我的解决方案中有两个项目; a standard ASP.NET project and a class library.一个标准的 ASP.NET 项目和一个 class 库。

The class library is created with the new project template:使用新项目模板创建 class 库:

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

The content of the class library is as such: class 库的内容如下:

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

My ProjectView.cshtml is simpel as follow:我的ProjectView.cshtml如下:

@model Project.ViewModels.ProjectViewModel

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

My ProjectViewModel.cs :我的ProjectViewModel.cs

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

My web.config :我的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:我有一个Directory.Build.props文件:

<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:我收到错误 14 次,还有一次:

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

Does anyone have a clue to what could be wrong?有谁知道可能出了什么问题?

Something in your Directory.Build.props has core depedencies, such as Castle.Core.您的 Directory.Build.props 中的某些内容具有核心依赖关系,例如 Castle.Core。 Can you load in AspNetCore or drop the.core dependencies?您可以加载 AspNetCore 或删除.core 依赖项吗?

This is really a bug related to Intellisense in Visual Studio.这确实是与 Visual Studio 中的 Intellisense 相关的错误。 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.在项目构建中使用 Microsoft.Net.Sdk 时,VS 假定项目中的 Razor 视图应使用 aspnetcore 编译以进行智能感知。

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).如果您从命令行运行 msbuild 和/或使用 VS 代码作为 IDE 您将永远看不到它们(因为它们不是真实的并且不会发生 - 这是导致这些 afaik 的 VS 内部的假设)。

To continue using VS then you can simply change "Show output from" in the Output window to "Build only".要继续使用 VS,您只需将 Output window 中的“Show output from”更改为“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.但是,它不会删除 cshtml 中的红色潦草下划线,但所有智能感知、代码完成等都可以正常工作。 This is of course not a real fix which afaik would require Visual Studio to change how Razor views are handled by intellisense.这当然不是真正的修复,afaik 需要 Visual Studio 更改智能感知处理 Razor 视图的方式。

I've been told that it is unlikely that the VS team will prioritize fixing this.有人告诉我,VS 团队不太可能优先解决这个问题。 I will urge you to report it as a bug in Visual Studio.我会敦促您将其报告为 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.如果您想将 IDE 更改为 VS Code 但依赖于一个或多个 VS 构建目标(例如 WebApplication Webpublish),您需要从 f.ex 导入这些目标 nuget 或干脆摆脱此构建依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM