在不同的项目中,将资源(Resx)文件作为链接项目在不同的解决方案中共享。

问题描述 投票:1回答:1

我有一个名为'MyApplication'的解决方案,有两个项目。

项目A (namespace MyApplication.ProjectA) {.Net Core 2.1}项目B (namespace MyApplication.ProjectB) {.Net Core 2.1 Test Project}。

ProjectA包含一个名为'Messages.resx'的资源文件。

我有Messages.resx 添加为链接 在ProjectB中,每当我从ProjectB中的Messages.resx中读取值时,它都会抛出 System.Resources.MissingManifestResourceException异常.我不想在ProjectB里面直接引用ProjectA。而且添加第三个库项目,里面有Resource文件,在ProjectA和ProjectB中都引用它,这不是一个选项。

c# .net asp.net-core embedded-resource
1个回答
0
投票

例如,我有两个mvc项目。WebApplication1和WebApplication3。

我在WebApplication3中有一个资源文件(MyData.resx)。

enter image description here

MyData.resx:

enter image description here

现在,根据你的需求,我在WebApplication1中添加MyData.resx作为链接。

enter image description here

然后,我可以从WebApplication1控制器中的.rexs文件中获取数据,如下所示。

 public IActionResult Index()
    {
        var rm = new ResourceManager("WebApplication1.Resources.MyData", Assembly.GetExecutingAssembly());
        string username = rm.GetString("Username");
        return View();
    }

结果:

enter image description here

注意:资源管理器中的baseName应该是当前项目的命名空间。

© www.soinside.com 2019 - 2024. All rights reserved.