.net core AspnetCore Razor views fail with CompilationFailedException

自作多情 提交于 2020-03-01 07:11:13

问题


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>

I'm targeting .netcore 2.1

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?}");
        });
    }

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


回答1:


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.

Summarised potential solutions

  • Reference the assembly in the web.config (as you've explicitly tagged Kestrel, this likely won't apply, but try anyway)

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

Note:

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

  • Add the following to your .csproj.

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

  • Update Visual Studio, and tooling, and try to create a new project

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

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

  • Install the latest version of the SDK

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

Hopefully something there will solve the problem for you.

Edit: 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.




回答2:


are you using Razor class library

I do not face any issues with VS 2017, for creating Asp.net mvc app targeting to .netcore2.1 using its default project template. some of the package references might give you issues if their dependencies have version conflict with each other, in your solution.

use the below reference,

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.0" />

Microsoft.AspNetCore.Mvc, Microsoft.AspNetCore.Server.Kestrel and your .netcore2.1 MVC app "Microsoft.AspNetCore.App" should not have any version conflict and it also applies to their dependencies.

  • "2.1.0" is the default version in VS 2017 when creating Razor class library and it works well with .netcore2.1 mvc apps.

if you are choosing preferred version of Mvc package, make sure to have compatible versions of Server.Kestrel and references of .netcore2.1 app.



来源:https://stackoverflow.com/questions/51803486/net-core-aspnetcore-razor-views-fail-with-compilationfailedexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!