gpt4 book ai didi

c# - .net core AspnetCore Razor View 因 CompilationFailedException 而失败

转载 作者:太空狗 更新时间:2023-10-30 01:29:27 25 4
gpt4 key购买 nike

当我尝试查看我的 Razor 页面时,我得到以下信息

fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLFVN3H0G8MT", Request id "0HLFVN3H0G8MT:00000001": An unhandled exception was thrown by the application.
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
jhhodq42.4nm(4,41): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
jhhodq42.4nm(5,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

这是一条令人困惑的消息,因为我的包引用在下面并且包括 网络标准

 <ItemGroup>   
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.1.1" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="Newtonsoft.json" Version="11.0.2" />
</ItemGroup>

我的目标是 .netcore 2.1

我的startup.cs是

 public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
loggerFactory.AddDebug();

app.UseMvc();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

我反复清除和恢复包缓存,并在 VS2017 和 dotnetcli 下复制了它,非常感谢任何指针。我目前最好的猜测是存在相互冲突的依赖关系,但我仍然是 .netcore 的新手并且不确定如何调试,感谢任何帮助

最佳答案

an issue在对这个主题进行了长时间讨论的官方存储库上。在撰写本文时,该问题仍未解决,但您似乎可以尝试几种可能的解决方案。但是,看起来有多种(目前尚未明确定义)导致此问题的原因,因此我鼓励您尝试所有这些。我将在此处包括其中一些项目,因此它不是仅链接的答案,但我认为完整阅读该问题是明智的。

总结潜在的解决方案

  • 在 web.config 中引用程序集(因为您已经明确标记了 Kestrel,这可能不适用,但无论如何都要尝试)

<system.web>
<compilation debug="true" targetFramework="4.7.1" >
<assemblies>
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.7.1" />
</system.web>

注意:

A web.config file is required when hosting the app in IIS or IIS Express. Settings in web.config enable the ASP.NET Core Module to launch the app and configure other IIS settings and modules. If the web.config file isn't present and the project file includes <Project Sdk="Microsoft.NET.Sdk.Web">, publishing the project creates a web.config file in the published output (the publish folder).

Source

  • 将以下内容添加到您的 .csproj .

<ItemGroup>
<Reference Include="netstandard" />
</ItemGroup>

  • 更新 Visual Studio 和工具,并尝试创建一个新项目

根据您提出问题的方式,我假设您的项目是全新的,并且可以选择创建一个新项目。

  • 更改 global.json到目标版本 2.1.2 dotnet SDK,而不是 2.0.3 .

  • 安装最新版本的SDK

  • 该线程中还有许多其他解决方案 [ 1 ] [ 2 ] [ 3 ]

希望那里能为您解决问题。

编辑:我注意到你打了两次电话给 app.UseMvc() .我怀疑它有什么作用,因为我想这些调用只是设置状态,但没有必要调用它两次。

关于c# - .net core AspnetCore Razor View 因 CompilationFailedException 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51803486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com