首页 > 解决方案 > How to create solution with multiple version support in mvc

问题描述

I am creating one project in Asp.net MVC 4.7.1 latest version and want to reuse that solution as class library dll in other projects also. But other projects where we are using this dll not support latest version (4.6). When am trying to use the class library in other version solutions it throws below error.

(0): error CS1705: Assembly 'ProjectName(dll name), Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I need to create a solution with multiple version support while using it is as a dll.

标签: c#asp.net-mvcversionclass-library

解决方案


您需要在项目中定位多个框架;

  1. 右键单击项目名称,然后单击“编辑 .cproj 文件”
  2. 将 ( s ) 添加到目标框架标签,使其变为
<TargetFrameworks>...</TargetFrameworks>
  1. 指定您的目标框架(请参阅所有版本的文档):
<TargetFrameworks>net472;net48;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2</TargetFrameworks>
  1. 如果您的库将有任何引用,则需要手动指定每个版本的所有引用:
<ItemGroup Condition=" '$(TargetFramework)' == 'net471' ">
  <Reference Include="System.Net" />
</ItemGroup>

或为多个版本指定参考:

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' || '$(TargetFramework)' == 'netcoreapp2.1' || '$(TargetFramework)' == 'netcoreapp2.2' ">
  <PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="1.0.0" />
</ItemGroup>

或为所有目标框架指定包引用:

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Localization" Version="1.0.0" />
</ItemGroup>

您可以指定所需的最低版本,框架将安装最新的兼容版本。例如,下面我们将本地化包最小版本指定为 1.0.0,但是当它将安装在 .Net Core 2.2 上时,它将安装最新的兼容版本,有关版本控制的更多详细信息,请参阅版本范围和通配符

  1. 最后但并非最不重要的; 在您的代码中,您需要检查需要对每个版本使用兼容代码行的目标框架特定情况。

例如,要获取文化信息,您需要为不同的 .Net Core 版本指定不同的代码:

#if NETCOREAPP1_0
    var culture = new CultureInfo("en-US");
#else
    var culture = CultureInfo.GetCultureInfo("en-US");
#endif

    _logger.LogInformation($"{culture.Name}");

如果您使用的是 Visual Studio,您将看到所有目标框架的下拉导航,您可以使用它在目标框架之间切换并检查您的代码兼容性。


推荐阅读