I am trying to achieve globalization in a .NET class library. In a .NET web application this seems to work fine. I can add multiple resource files under the App_GlobalResources.
e.g.
LocalizedText.resx
LocalizedText.fr.resx
However resource files are handled differently in Class Libraries. I add a resource file by
1) Opening the Properties for the Class Library
2) Clicking on the Resources Tab
3) Clicking on the link to create a “default resources file”
With this model it seems to only want to allow one default resources file. I can rename files and seem to get around this “one file” limitation but if I produce resource files with the same names as above there appears to be no code generated for the “LocalizedText.fr.resx” file. If I reference a string in code like so…
myControl.Text = Properties.LocalizedText.MyLocalizedText;
It references the LocalizedText.resx file for the value (ignoring the fact that I have the culture set to French). I’m guessing that whatever auto-generates the code for the designer file sees that there is already a “LocalizedText” class and doesn’t generate the necessary code.
Is there not a way (equivalent to the web application project) that I can use multiple resource files in a Class Library, named differently for each culture, and be able to easily access this in code (trusting .NET to switch appropriately depending on the culture info)?
Many Thanks
First: create a folder in your project named 'MyFolder'.
Second: add a resource file named 'MyResourceFile' to the folder.
Third: where you want access the resource values, import:
using System.Reflection;
using System.Resources;
using MyProject.MyFolder.MyResourceFile;
and create a property that can access the your resource. Like this:
public static ResourceManager oResourceManager = new ResourceManager("MyProject.MyFolder.MyResourceFile", typeof(MyResourceFile).Assembly);
Fourth: Get the data from your property:
oResourceManager.GetString("ResourceKey", System.Globalization.CultureInfo.CurrentCulture);
Related
Using Visual Studio 2015 on an ASP.NET project. Everything works fine except now I have a block of code (A common AD utility function) that I want all the pages to be able to access. The original language (inherited project) is VB.NET, but it should work just fine either way.
I created a class file, named it CommonADFunctions.vb. The file uses System.DirectoryServices, and there's a reference in the project to that assembly. However, there are two problems:
I can't reference the new class to call the functions from any of the page code files. This includes attempts to instantiate a new variable as the class type.
When I move the new .VB file into App_Code, the reference to System.DirectoryServices breaks, and it refuses to build.
This may seem like a trivial ask, but what am I missing?
Turns out this was a simple property on the file itself I created a new Class File, and somehow the Build Action got set to Content rather than Compile. Setting this to Compile allowed the object to be referenced and instantiated properly. It doesn't fix the IDE intelliSense from APP_Code for DirectoryServices references in the code, but it compiles and runs, which is what I needed.
EDIT: Received external help which pointed this out.
Without code and screenshots to illustrate I can only recommend a few things to try.
I can't reference the new class to call the functions from any of the page code files. This includes attempts to instantiate a new variable as the class type.
The methods in the class have Private or Friend scope.
Its a static class (a Module in VB.Net) and you do not need to instantiate it.
You are somehow referencing an old DLL which doesn't have the methods. Check the reference to this AD project and make sure its "Referenced" via Project.
When I move the new .VB file into App_Code, the reference to System.DirectoryServices breaks, and it refuses to build.
This sounds like a corrupt project file. Clean the solution, delete the .suo file, delete the obj & bin folders.
Then move the CommonADFunctions.vb to the App_Code folder and add the DLL reference to System.DirectoryServices. Or create the CommonADFunctions.vb file in App_Code to start with.
Can anyone tell how to access resource file in class library asp.net mvc4? I'm building a generic Class Library that I will use it in all my projects. I tried with following code:
HttpContext.GetGlobalResourceObject();
Can anyone tell any other method is there to access the resource file in class library?
Edit based on the comments, including best practices :
To create a Resource file in MVC4 :
In the solution explorer, right click on your project.
Then, you click on "Add", then on "Add ASP.Net folder", and then click on App_GlobalResources.
On the new folder, you right click. Then you add a new Resource File.
You open then this Resource file and can add new resources.
The lefter column is where you set the keys, and the righter one is for the values you have to insert inside it.
Then; it is really easy, you just have to write the following parts of code to access the values you want.
On the c# side, it is :
Resources.NameOfYourResFile.NameOfYourResKey
On the ASP side, assuming that you're using razor, it is :
#Resources.NameOfYourResFile.NameOfYourResKey
Example :
If you have created a a file named "Global.resx", and created the key "ApplicationTitle", which has "My super website" as value, you just have to write the following line of code to put it for example into a string :
string siteTitle = Resources.Global.ApplicationTitle;
and that's it
When you create your resource you have to set the access level from internal (by default) to public. After this you simply can access your resources (if you reference the assembly) by the name of the resource and the static properties generated into them.
I would suggest you to create a new library for Resources
Once you are done making resources key value pairs then add the reference of your Resource Library in to your project. and then you are able to access the resource library in this way
Namespace.ReourceLibraryName.Key
just include System.Web.dll in your class library project and access the resource file just like you would have accessed in the MVC project
HttpContext.GetGlobalResourceObject();
when you add a refrence to this class library project inside your MVC project and run the project this code will be executed in the context of the MVC project and you will get the correct instance of HttpContext.
Note:- directly using HttpContext like this would create problems when you will try to test this application
You can add your resource file any where in your class library, but I think your question is how to use it, If your resource files name is Res1.resx and there is a key in it called message then you can call
Res1.message
from your classes in that library. But you I am sure you would like to use it event from out side the library for that you need to make your resource file public, please refer Visual Studio - Resx File default 'internal' to 'public'
please do let me know if it is the one you need or some thing else?
I have a ClassLibrary with a C# Class and a folder with resx files.
I have to read a specific Resx file by culture within the C# class.
I tried the following code (both GetLocalResourceObject and GetGlobalResourceObject), but I get this error: 'The relative virtual path is not allowed here'.
HttpContext.GetLocalResourceObject("Folder.ResxFileName", "ResxKey", new CultureInfo("it-IT"))
How do I have to set the Resx file path to get it working?
(The ClassLibrary dll is referenced in a website project)
You've just to use the ResourceManager exposed by the class library (with the namespace 'ClassWithResx'):
ClassWithResx.Resources.ResourceManager.GetString("ResxKey", System.Globalization.CultureInfo.GetCultureInfo("en-US"));
this works supposing you've a Resources.resx and Resources.en-US.resx (or whatelse) file in the root of your class library, and you've to mark with the custom tool the access modifier of the resource strings to "Public".
I have a couple of web applications whose source code is missing. The project is compiled to a dll and is hosted on a IIS.
I have couple of questions to make.
What is the best way to recreate the
project from the dll file??
We are planning to change the
database server, and the database
connection strings are specified in
the Global.asax ( I mean the public
class Global : HttpApplication ). Is
there a way I can subclass this
Global and override the connection
strings? If yes, how can I make the
IIS refer to the new dll
Thank you all for any suggestions!!
For first part, use decompiler tools such as Reflector/ILSpy/dotPeek to convert IL code from DLL to higher level language such as C#. However, tools cannot get back comments, local variable names and project structure. You need to manually organize the code into files and project structures. From aspx files, you have to figure out the code-behind classes and then link up the source of the class into a correct named code-behind file - for example, if default.aspx says that it inherits from MyApp.Default then create file default.aspx.xs and put the source code for the class into that.
For second part, you can create a new class derived from Global and modify Global.asax to use that class - you need to put the assembly containing new class in bin folder and overwrite inherits clause in asax file to point to the type name of new class. You anyway need to inspect the code of your current Global class (using tools sighted above) to see if you can override connection strings by sub-classing.
Probably the best tool available to reverse engineer a dll into code is .NET Reflector. Unfortunately, the latest version is no longer free, but it is worth the money.
I am not sure exactly if this still applies if you can already reverse engineer your source code. However, I would recommend moving your connection strings outside of your project into web.config as a best practice. This way you can make the change in the future without changing any code.
I have a large directory structure with JavaScript, images, etc. that depend on each other. I would like to encapsulate it all into a DLL so I only have to reference one thing and not have multiple copies of all these files across projects.
Because the files depend on each other, I'm thinking I can create an IHttpModule that registers a route to accept URLs such as /MyEmbeddedDir/subdir/file.js. Anything in MyEmbeddedDir would then be handled by a custom IHttpHandler that does the correct mapping. Each web application would then need to reference the DLL and add the module and handler to web.config. Does this seem reasonable?
Also, is there an easier way to embed/reference the files than to set the build action to embedded resource and add [assembly: WebResource(...)] to each file (there are dozens!)? Thanks!
Edit: If I'm not using WebResource.axd then I shouldn't need to add [assembly: WebResource(...)]
Yes, having a single container is a great way to manage large number of files (and no, SQLite won't help here! ;).
We have a product, named SolFS, which is a virtual file system, that lets you keep your data in custom storage (resource DLL is one of the options) and provides file API for accessing the files. We even implemented asynchronous pluggable protocol for IE (on the client side, but the task is very similar to yours). SolFS includes a manager application that lets you easily create container files and import files into container.
I ended up going with the IHTTPModule (register route) and IHTTPHandler (obtain embedded resource). The route is configurable in web.config in case it conflicts with existing content.