You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨Visual Studio解决方案共享常量:是否需创建类库项目?

Absolutely, this is exactly the scenario where a dedicated class library shines—it’s designed for sharing code across independent projects/solutions, and it’ll completely eliminate the duplicate constant definitions you’re dealing with. Here’s how to implement it smoothly, even with your solutions kept separate:

Step 1: Create the Constants Class Library

  1. In Visual Studio, create a new Class Library project. Choose a framework compatible with both your ASP.NET Web API and client web app:
    • If your API is on .NET Framework and the client is .NET Core/.NET 5+, go with .NET Standard 2.0 (it works with almost all modern .NET frameworks).
    • If both projects are on .NET 6+, use the same .NET version for the class library.
  2. Add a static class to hold your constants—follow standard C# naming conventions for constants (all caps with underscores):
    public static class AppConstants
    {
        public const decimal BUYING_RATE = 1.23m;
        public const int THRESHOLD_LIMIT = 1000;
        // Add all your shared constants here
    }
    
  3. Build the library to generate the DLL file (you’ll find it in the bin/Debug or bin/Release folder of the class library project).

Step 2: Add the Library to Your Two Projects

Since your projects are in separate solutions, you have a couple of straightforward options to reference the library:

  • Direct DLL Reference:
    1. Copy the built YourConstantsLibrary.dll (and .pdb file if you need debugging support) to a dedicated folder in each project (e.g., Libs/SharedConstants).
    2. In Visual Studio, right-click your API/client project → Add → Reference → Browse → select the copied DLL.
  • Local NuGet Package (Better for Updates):
    1. Right-click the constants class library project in Visual Studio → Pack (this generates a .nupkg file in the bin folder).
    2. Create a local folder to act as your private NuGet feed (e.g., C:/LocalNuGetFeed), and copy the .nupkg file there.
    3. In each solution, go to Tools → NuGet Package Manager → Package Manager Settings → Package Sources → add your local folder as a new source.
    4. Now you can install the constants package via the NuGet Package Manager in both projects, just like any other package. This makes updating constants much easier—repack the library and update the package in each project when changes are needed.

Key Considerations

  • Framework Compatibility: Double-check that the class library’s framework works with both your API and client project to avoid runtime errors.
  • Versioning: If you plan to update constants over time, add version numbers to your class library (and NuGet package) so you can track which version each project is using.
  • Keep It Focused: Only put shared constants in this library—don’t add unrelated classes or dependencies. This keeps the library lightweight and avoids introducing unnecessary dependencies to your API and client.

This approach ensures consistency across both projects, eliminates duplicate code, and fits perfectly with your constraint of separate solutions.

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:55:40