错误RZ3007:目标标签名称不能为空格或空白。

8

升级到.NET Core 2.0后,我的1.1 Web应用程序无法运行。我收到下面指示的错误消息。检查输出后,我发现引用了RZ3007错误,我相信这与Razor编译器有关。视图未编译。

Home/Index.cshtml:

@model LayoutViewModel
<div></div>

Views/Shared/_Layout.cshtml

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - GLCoderConfiguration</title>

    <environment names="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/css/site.css?" />
    </environment>
    <environment names="Staging,Production">
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/site.min.css?" asp-append-version="true" />

   </environment>
</head>
<body>
    <nav class="navbar navbar-default navbar-form " >

        <div class="navbar-header" style="margin-top:10px;margin-bottom:15px;">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">SCM - NCMain</a>
        </div>


        <div class="collapse navbar-collapse vcenter" id="bs-example-navbar-collapse-1" style="margin-top:10px;margin-bottom:0px;">

      </div>
    </nav>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; 2017 - England Logisitics - SCM</p>
        </footer>
    </div>

    <environment names="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    </environment>
    <environment names="Staging,Production">
        <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha384-K+ctZQ+LL8q6tP7I94W+qzQsfRV2a+AfHIi9k8z8l9ggpc8X+Ytst4yBo/hH+8Fk">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa">
        </script>
        <script src="~/js/site.min.js" asp-append-version="true"></script>


    </environment>

    @RenderSection("Scripts", required: false)
  </body>
</html>

我曾经有几个视图组件(LoginStatus和MenuItems),但为了让它正常工作,我已将它们删除。

运行输出:

    Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
(0,0): Error RZ3007: Targeted tag name cannot be null or whitespace.
   at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(String relativePath)
   at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CreateCacheEntry(String normalizedPath)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
   at Microsoft.AspNetCore.Mvc.ViewResult.<ExecuteResultAsync>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeResultAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResultFilterAsync>d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Session.SessionMiddleware.<Invoke>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Session.SessionMiddleware.<Invoke>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

.CSProj

    <Project Sdk="Microsoft.NET.Sdk.Web">

      <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
        <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
        <MvcCompileViews>true</MvcCompileViews>
        <PreserveCompilationContext>true</PreserveCompilationContext>
        <AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
        <StartupObject>GLCoderConfiguration.Program</StartupObject>
      </PropertyGroup>

      <ItemGroup>
        <PackageReference Include="FluentValidation" Version="7.1.1" />
        <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
        <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
        <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
        <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
        <PackageReference Include="Microsoft.NETCore.Platforms" Version="2.0.0" />
        <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
        <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
        <PackageReference Include="NETStandard.Library" Version="2.0.0" />
        <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0" />
      </ItemGroup>

      <ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>
      <ItemGroup>
        <ProjectReference Include="..\..\SCMEnumerations\SCMEnumerations.csproj" />
        <ProjectReference Include="..\..\TMainModel\TMainModel.csproj" />
        <ProjectReference Include="..\..\ViewHelperFunctions\ViewExtensions.csproj" />
      </ItemGroup>

    </Project>
8个回答

3
这个方法对我很有效:
  • 删除 objbin 文件夹
  • 运行 dotnet restore
  • 运行 dotnet build

1
这对我也起作用了。dotnet --version 2.1.200 - Paul Knopf

1

我最终按照Microsoft提供的步骤操作。我彻底卸载了Visual Studio并重新安装了它。然后我创建了一个新的Web项目,将其设置为目标2.0,然后复制了非工作解决方案中的文件,事情自动开始运转。我仍然需要确保身份验证正确配置,但事情开始变得顺利。


2
嗨@dmcquay,我也遇到了这个问题,不得不重新创建我的项目来解决这个问题,这是我们不应该做的...最近我又发现了这个问题,似乎没有任何理由...在查看我的git repo并比较上一次成功构建之间的更改时!你有没有学到关于这个错误的任何其他信息?我花了太多时间处理这些类型的错误,我的股票标准点网核心开发环境。 - David van Dugteren

0
*.csproj中删除下面的行对我有用。 <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" /> 在阅读升级文档中的Review your Razor View Compilation setting部分后,我添加了它。结果证明这不是一个好主意!

0

当我从Visual Studio 2019发布时,我勾选了在目标位置删除附加文件的发布选项,这解决了这个问题。

enter image description here


0
尝试将 环境名称 替换为 环境包含
<environment include="Staging,Production">
  <strong>HostingEnvironment.EnvironmentName is Staging or Production</strong>
</environment>

根据文档

include属性(在ASP.NET Core 2.0中)与ASP.NET Core 1.0中的names属性具有类似的行为。

更新: 我尝试使用像你的.csproj中的包自己迁移一些项目(从1.1到2.0),以便重现你的错误,但不幸的是没有成功。不确定,但可能是由于预编译引起的。

有时我会遇到CompilationFailedException,但它就像CS0246,并显示导致问题的行。但是你的RZ3007(0,0)看起来像是Razor本身出了问题。也许你应该在Github上开一个新的问题。
不确定是否有帮助,但也尝试安装Microsoft.AspNetCore.Razor.Language NuGet包,因为如果你阅读这个讨论关于Razor的更改,它似乎是必需的。

如果您还没有使用Visual Studio 2017的15.3版本,请尝试更新。此外,请确保SDK已更新至2.0版本。


1
尝试了一下,没有成功。基于文档,我认为 "names" 属性仍然有效。 - dmcquay
@dmcquay 我明白了,很奇怪。请将您的 *.csproj 文件内容更新到您的问题中。 - user2771704

0
在 pre-netcore-2.0 版本中,在 Startup 类中的 public void ConfigureServices(IServiceCollection services) 方法中需要一个解决方法:
services.AddMvc()
        .AddApplicationPart(typeof(SomeController).GetTypeInfo().Assembly)
        .AddRazorOptions(options =>
        {
            // By docs this should work out of the box, but due to a bug this workaround is needed currently:
            foreach (var name in typeof(Program).GetTypeInfo().Assembly.GetReferencedAssemblies())
                    options.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(Assembly.Load(name).Location));
        })
        ;

但现在不再是这样了。以下就足够了:

services.AddMvc()
        .AddApplicationPart(typeof(SomeController).GetTypeInfo().Assembly);

保留这个变通方法会导致所提到的RZ3007错误。

这个答案中的TowerController从哪里来? - Paul Smith
抱歉,我更改了措辞为“SomeController”,意思是任何控制器。 - Peter Gulyas

0

我的 *csproj 文件中有这个:

  <ItemGroup>
   <Reference Include="System.Private.Xml">
     <HintPath>C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.0\System.Private.Xml.dll</HintPath>
   </Reference>
 </ItemGroup>

不知道它从哪里来,但是移除它解决了问题。


0
在我的情况下,在.csproj中禁用Razor设置是可行的:
<PropertyGroup>   
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接