get resource from ASP.NET App_GlobalResources in separate DLL Library
I have a solution with one web project and one class library. In web project I have App_GlobalResources folder with resource f开发者_StackOverflow社区ile. In code used in web project I can access resourcel like this: string r = Resources.res.ResourceString123;
How to get this resource string from class library?
I also use a separate assembly for my web project and class libraries. I required access to my App_GlobalResources in the web project for my ModelBinders. I access the resource is via this method (see: A Beginner's Guide to ASP.NET Application Folders).
HttpContext.GetGlobalResourceObject( string classKey , string resourceKey )
Once caveat is that you will need access to the HttpContext, which I have via the controllerContext in my ModelBinder.
My ModelBinder code reads as follows:
var errorMessage = controllerContext.HttpContext.GetGlobalResourceObject( "DefaultModelBinder" , "PropertyValueRequired" ) as string;
The above allows me to have a consistent error message across ASP.NET's default error messages and my ModelBinders, even though I have them in separate assemblies.
In .net 2.0, you will need to add the InternalsVisibleTo attribute to the assembly & set it to the namespace which needs to access the file:
MSDN: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx
Another options could be to create a class in the class library which can return the strings you need...
HTH.
If using visual studio 2008 you can set the access modifier on the Resource designer to public
in the class library.
精彩评论