.net核心AspnetCore剃刀视图因CompilationFailedException而失败 [英] .net core AspnetCore Razor views fail with CompilationFailedException

查看:321
本文介绍了.net核心AspnetCore剃刀视图因CompilationFailedException而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i try and view my Razor pages i get the following

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'.

这是一个令人困惑的消息,因为我的软件包引用在下面,并且包括
netstandard

This is a confusing message as my package references are below and include netstandard

 <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

I'm targeting .netcore 2.1

我的startup.cs是

my startup.cs is

 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还是陌生的,并且不确定如何调试,感谢任何帮助

I have repeatedly cleared and restored the package cache and have replicated this under VS2017 and under dotnetcli any pointers much appreciated. my current best guess is that there is a conflicting dependency but i am still new to .netcore and am unsure how to debug, any help appreciated

推荐答案

官方存储库中有一个问题,其中一个问题这个话题。在撰写本文时,该问题仍然悬而未决,但是您似乎可以尝试几种潜在的解决方案。但是,似乎有多种原因(目前尚不确定)导致此问题,因此,我建议您尝试所有这些问题。我将在其中包括其中一些项目,因此,这不是一个仅链接的答案,但我认为完全阅读该问题将是明智的。

There is an issue on the official repository that has a lengthy discussion on this topic. At the time of writing, that issue is still open, but there seems to be several potential solutions that you can try. However, it looks like there are multiple, currently not well-defined, causes to this problem, so I'd encourage you to try them all. I'll include some of those items in here, so it's not a link-only answer, but I think it would be wise to read that issue in its entirely.


  • 引用web.config中的程序集(如您已为Kestrel明确标记的那样,这可能不适用,但

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

注意:


在IIS或IIS Express中托管应用程序时,需要一个web.config文件。 web.config中的设置启用 ASP.NET核心模块,以启动应用程序并配置其他IIS设置和模块。如果不存在web.config文件,并且项目文件包含< Project Sdk = Microsoft.NET.Sdk.Web> ,则发布项目会创建一个发布输出(发布文件夹)中的web.config文件。

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).

来源


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

  • Add the following to your .csproj.

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


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

从陈述问题的方式出发,我假设您的项目是全新的,并且创建一个新项目

From the way your question is stated, I've assumed your project is brand new, and that creating a new project is an option.


  • 更改 global.json 定位到dotnet SDK的 2.1.2 版本,而不是 2.0.3

  • Change global.json to target version 2.1.2 of the dotnet SDK, instead of 2.0.3.

安装最新版的SDK

该线程中还有许多其他解决方案 [1] [2] < a href = https://github.com/dotnet/standard/issues/542#issuecomment-389349783 rel = nofollow noreferrer> [3 ]

There are a number of other solutions in that thread [ 1 ] [ 2 ] [ 3 ]

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

Hopefully something there will solve the problem for you.

编辑:我注意到您对 app.UseMvc()进行了两次调用。我怀疑它有什么作用,因为我想这些调用只是设置状态,而不必调用两次。

I notice you've made two calls to app.UseMvc(). I doubt it has anything to do it, as I imagine those calls simply set state, but there's no need to call it twice.

这篇关于.net核心AspnetCore剃刀视图因CompilationFailedException而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆