2010-05-02 32 views
1

我有一個Web項目和一個類庫的解決方案。在web項目中,我有App_GlobalResources文件夾和資源文件。在Web項目中使用的代碼中,我可以像這樣訪問resourcel: string r = Resources.res.ResourceString123;從單獨的DLL庫中的ASP.NET App_GlobalResources獲取資源

如何從類庫中獲取此資源字符串?

+0

首先你要確保全球資源文件公開可用 https://holyhoehle.wordpress.com/2010/02/20/making-global-resources-public/ – 2017-10-18 16:38:43

+0

然後在你的視圖中使用YourProjectName。 App_GlobalResources文件;」然後是「Content.msg」,我的密鑰爲「msg」的全局資源中有Content.resx文件 – 2017-10-18 16:39:00

回答

0

如果使用visual studio 2008,則可以在類庫中的Resource designerpublic之間設置訪問修飾符。

0

我也爲我的web項目和類庫使用單獨的程序集。我需要訪問我的ModelBinders的Web項目中的App_GlobalResources。我通過這種方法訪問資源(請參閱:A Beginner's Guide to ASP.NET Application Folders)。

HttpContext.GetGlobalResourceObject(string classKey , string resourceKey) 

一旦需要注意的是,你將需要訪問的HttpContext,我在我的模型綁定器通過controllerContext有。

我ModelBinder的代碼如下:上述

var errorMessage = controllerContext.HttpContext.GetGlobalResourceObject("DefaultModelBinder" , "PropertyValueRequired") as string; 

讓我有整個ASP.NET的默認錯誤消息和我ModelBinders一致的錯誤消息,即使我讓他們在不同的組件。