.net 核心 AspnetCore Razor 视图失败并显示 CompilationFailedException [英] .net core AspnetCore Razor views fail with CompilationFailedException

查看:32
本文介绍了.net 核心 AspnetCore Razor 视图失败并显示 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'.

这是一个令人困惑的消息,因为我的包引用如下,包括网络标准

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,这可能不适用,但仍要尝试)

<代码><编译调试="true" targetFramework="4.7.1" ><组件><add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/></组件></编译><httpRuntime targetFramework="4.7.1"/></system.web>

注意:

在 IIS 或 IIS Express 中托管应用程序时需要 web.config 文件.web.config 中的设置启用 ASP.NET Core 模块 来启动应用程序并配置其他 IIS 设置和模块.如果 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.

<代码><项目组><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

Install the latest version of the SDK

该线程中有许多其他解决方案 [ 1 ] [ 2 ] [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 Razor 视图失败并显示 CompilationFailedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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