升级到asp.net core 2.0后,Taghelper无法渲染

时间:2018-01-29 23:42:48

标签: c# asp.net-mvc asp.net-core-2.0

我已将我的应用程序升级到asp.net core 2.0,现在我的taghelpers没有渲染。我的代码没有变化。下面是我的_ViewImport.cshtml和labeltaghelper.cs。同时在构建时我收到警告

' Microsoft.AspNet.Mvc.TagHelpers 6.0.0-rc1-final'使用' .NETFramework,Version = v4.6.1'进行了恢复。而不是项目目标框架' .NETCoreApp,Version = v2.0'

_ViewImport.cshtml

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, IntegraPay.RegistrationApplication

LabelTagHelper.cs

using IntegraPay.Domain.SObjects;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace IntegraPay.RegistrationApplication.TagHelpers
{
    [HtmlTargetElement("label", Attributes = FieldContentMetaData)]
    public class LabelTagHelper : TagHelper
    {
        private const string FieldContentMetaData = "tag-FieldMetaData";

        [HtmlAttributeName(FieldContentMetaData)]
        public WebFormFieldContent FieldContent { get; set; }
        private TagBuilder AnchorBuilder { get; set; } = new TagBuilder("a");
        private TagBuilder asteriskBuilder { get; set; } = new TagBuilder("span");
        private void CreateAsteriskIcon()
        {
            asteriskBuilder.Attributes.Add("class", "asteriskStyle");
            asteriskBuilder.InnerHtml.Append("*");
        }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (FieldContent.Required__c)
            {
                CreateAsteriskIcon();
                output.PreElement.AppendHtml(asteriskBuilder);
            }
            if (FieldContent.Visible__c)
            {
                output.Content.SetHtmlContent(FieldContent.Label_Override__c);
                if (!string.IsNullOrEmpty(FieldContent.Field_Description__c))
                {
                    GenerateAnchorToolTipTag();
                    output.Content.AppendHtml(" ");
                    output.Content.AppendHtml(AnchorBuilder);
                }
            }
            else
            {
                output.Attributes.Add("class", "ControlIsVisible");
            }
            base.Process(context, output);
        }

        private void GenerateAnchorToolTipTag()
        {
            if (!string.IsNullOrEmpty(FieldContent.Field_Description__c))
            {
                AnchorBuilder.Attributes.Add("data-toggle", "tooltip");
                AnchorBuilder.Attributes.Add("data-placement", "right");
                AnchorBuilder.Attributes.Add("data-original-title", FieldContent.Field_Description__c);
                string iclass = "ip fa fa-question-circle-o";
                AnchorBuilder.InnerHtml.AppendHtml($"<i class='{iclass}' aria-hidden='true'></i>");
            }
        }
    }
}

的csproj

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <DebugType>portable</DebugType>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <AssemblyName>Integrapay.RegistrationApplication</AssemblyName>
    <OutputType>Exe</OutputType>
    <PackageId>Integrapay.RegistrationApplication</PackageId>
    <RuntimeFrameworkVersion>1.1.2</RuntimeFrameworkVersion>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
    <SignAssembly>False</SignAssembly>
  </PropertyGroup>

  <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <ItemGroup>
      <DocFile Include="bin\$(Configuration)\$(TargetFramework)\Integrapay.RegistrationApplication.xml" />
    </ItemGroup>
    <Copy SourceFiles="@(DocFile)" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
  </Target>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DocumentationFile>bin\Debug\netcoreapp1.1\Integrapay.RegistrationApplication.xml</DocumentationFile>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DocumentationFile>bin\Release\netcoreapp1.1\Integrapay.RegistrationApplication.xml</DocumentationFile>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="bower_components\bootstrap-fileinput\**" />
    <Content Remove="bower_components\bootstrap-fileinput\**" />
    <EmbeddedResource Remove="bower_components\bootstrap-fileinput\**" />
    <None Remove="bower_components\bootstrap-fileinput\**" />
  </ItemGroup>


  <ItemGroup>
    <Compile Remove="Controllers\LoginController.cs" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="wwwroot\docs\ui\custom.css" />
    <Content Include="wwwroot\docs\ui\index.html" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="bower_components\" />
    <Folder Include="wwwroot\css\" />
  </ItemGroup>

  <ItemGroup>
    <None Update="wwwroot\**\*;Views\**\*;bin\Debug\netcoreapp1.1\Integrapay.RegistrationApplication.xml">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </None>
  </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>
    <PackageReference Include="Microsoft.AspNet.Mvc.TagHelpers" Version="6.0.0-rc1-final" />
    <!--<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />-->
    <PackageReference Include="DeveloperForce.Force" Version="1.3.2" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration.Tools" Version="1.0.0-preview2-final" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.0.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.5" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" 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.Framework.Configuration" Version="1.0.0-beta8" />
    <PackageReference Include="Microsoft.NETCore.Portable.Compatibility" Version="1.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.1" />
    <PackageReference Include="MailKit" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
    <PackageReference Include="Swashbuckle" Version="5.6.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="1.1.0" />
    <PackageReference Include="System.Runtime" Version="4.3.0" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Update="Microsoft.NETCore.App" Version="2.0.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\IntegraPay.Domain\IntegraPay.Domain.csproj" />
  </ItemGroup>
</Project>

2 个答案:

答案 0 :(得分:0)

.csproj文件中,您正在使用预览NuGet包Microsoft.AspNet.Mvc.TagHelpers。它已被Microsoft.AspNetCore.Mvc.TagHelpers取代。

  

您的项目中还有其他预发布包。我建议检查所有依赖项并首先尝试使用最新版本,然后在遇到任何冲突警告时适当降级。

答案 1 :(得分:0)

这是命名空间名称问题。 IntegraPay中的P需要小写。

   @addTagHelper *, IntegraPay.RegistrationApplication

@addTagHelper *, Integrapay.RegistrationApplication