编程语言
首页 > 编程语言> > c#-.net核心AspnetCore剃刀视图因CompilationFailedException而失败

c#-.net核心AspnetCore剃刀视图因CompilationFailedException而失败

作者:互联网

当我尝试查看我的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”>
    <装配体>
      < add assembly =“ netstandard,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51” />
    < /程序集>
  < / compilation>
  < httpRuntime targetFramework =“ 4.7.1” />
< /system.web\u0026gt; 注意:

A web.config file is required when hosting the app in IIS or IIS Express. Settings in web.config enable the 07001 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()进行了两次调用.我怀疑它有什么作用,因为我想这些调用只是设置状态,但是没有必要调用两次.

标签:razor,net-core,kestrel-http-server,asp-net,c
来源: https://codeday.me/bug/20191108/2009438.html