簡體   English   中英

Class 部分頁面的庫標記幫助程序在 ASP.NET 核心 3.1 Razor 頁面應用程序中不起作用

[英]Class Library Tag Helper for Partial Pages not working in ASP.NET Core 3.1 Razor Pages application

作為我在 ASP.NET Core 3.1 Razor 頁面 web 應用程序上工作的一部分,我創建了幾個自定義標簽助手。 一旦我以我想要和預期的方式完成所有這些工作(作為 ASP.NET 核心 3.1 應用程序的一部分),我將標記幫助程序移動到 Razor Class 庫(自定義標准幫助程序 in.1),因此可以使用.NET 中的自定義標准幫助程序其他應用。

這就是我在使用 PartialTagHelper class 渲染部分頁面時遇到問題的地方:

TypeLoadException:無法從程序集“Microsoft.AspNetCore.Mvc.ViewFeatures,版本=3.1.3.0,文化=中性,PublicKeyToken=adb9793829ddae60”加載類型“Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.IViewBufferScope”。

PartialTagHelper class 的構造函數需要此錯誤中記錄的 IViewBufferScope 參數,並通過依賴注入傳遞到自定義標記幫助程序代碼中。

在 ASP.NET Core 3.1 Razor 頁面中,自定義標記幫助程序代碼需要對 Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers 命名空間的“使用”引用。

在 Razor 庫中,自定義標記幫助程序代碼需要對 Microsoft.AspNetCore.Mvc.ViewFeatures.Internal 命名空間的“使用”引用。

我還嘗試使用 .NET Standard 2.0 和 2.1 以及 .NET Core 3.1 Class 庫。 在所有這些情況下,Class 庫需要引用 Microsoft.AspNetCore.Mvc.ViewFeatures 2.2.0 版和 Microsoft.AspNetCore.Razor 2.2.0 版才能進行編譯。

因此,錯誤聽起來像 ASP.NET Core 3.1 Razor 頁面正在注入 3.1.3 Microsoft.AspNetCore.Mvc.ViewFeatures 程序集,這不包含來自正確程序集的 IViewBufferScope 參數。

有沒有辦法解決這個問題?

謝謝

A little over a week ago (on 5/20/2020), I found out how to create a package that would work for the ASP.NET Core 3.1 Razor pages, but had a different package that would work for lower version levels (for example, .NET 框架 4.8 Razor 頁面應用程序)。 Let me first explain how I initially got things to work and then I will explain how I now have a single package that I can use for either the .NET 4.8 Razor Pages or the .NET Core 3.1 Razor Pages.

Since I was originally able to get the TagHelper for Partial Pages to work as part of my ASP.NET Core 3.1 Razor Pages application, I compared the csproj file from that web application to my ASP.NET Core 3.1 Class Library csproj file.

那是當我注意到 ASP.NET Core 3.1 Razor Pages csproj 文件開始於:

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

但是各種 Class 庫項目沒有使用以“.Web”結尾的 SDK。

So, I updated the ASP.NET Core Class Library csproj file to the same Project Sdk value as the ASP.NET Core 3.1 Razor Pages application.

當我保存 csproj 文件時,它會自動將 Output 類型(在項目屬性應用程序選項卡上)更新為控制台應用程序。 我將其更改為 Class 庫,現在我可以使用 Partial Page Tag Helper 沒有任何錯誤,所以我的問題部分解決了。

這是我的 Class 庫的 csproj 文件:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
        <IsPackable>true</IsPackable>;
        <Version>1.0.5</Version>
        <OutputType>Library</OutputType>
    <PropertyGroup>
</Project>

This reason I say partially solved my problem is that further changes are required to have a single package for the ASP.NET Core 3.1 Razor Pages as well as lower version Razor Pages web applications like .NET Framework 4.8 Razor Pages or even .NET Core 2.2 Razor Pages. 需要做幾件事。

首先,將PropertyGroup中的TargetFramework條目替換為TargetFrameworks並添加netstandard2.0:

<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>

接下來,為 .NET 標准 2.0 TFM(目標框架)所需的 PackageReferences 添加條件 ItemGroup:

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.2.0" />
</ItemGroup>

最后,所有的 Class 庫代碼都是完全一樣的,以支持 ASP.NET Core 3.1 和 .NET Standard 2.0,除了一個 using 語句在自定義 Tag Helper 中添加條件編譯檢查如下:

#if NETCOREAPP3_1
using Microsoft.AspNetCore.Mvc.ViewFeatures.Buffers;
#else
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
#endif

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM