How to get resource strings from ASP.NET markup?

Hi I have an assembly called like X.Common.DLL. There is some resources files for multilanguage app. Let's say it Language.resx Language.en-US.resx....etc....

I have a web application which contains this above dll as reference...

So how can I use this resources file in my web applications markup side?

Text="<%$ Resources:Class, ResourceKey %>" is not valid because of "Class" name is in another assembly...


You can easily create a wrapper class that does something like this

public class ResourceWrapper
{
     private ResourceManager resourceManager;

     public ResourceWrapper()
     {
         resourceManager = new ResourceManager("Namespace.Common", Assembly.Load("x.common"))
     }

     public string String(string resourceKey)
     {
         return ResourceManager.GetString(resourceKey);
     }
 }     

Finding the correct name for the first param to new ResourceManager(...) can be a bit tricky sometimes. To make it easier for yourself you can call like this:

Assembly.Load("x.common").GetManifestResourceNames() and check the returned results.

If you create a static wrapper, you can make the resource calling code as simple as this:

<%= Resource.String("MyResourceKey") %>

You should reference the other assembly in web.config to expose its content in web forms. http://msdn.microsoft.com/en-us/library/ms164642.aspx

Edit : more detailed answer due to comments under : You should complete the pages/namespaces section of the webconfig like this :

<pages>
    <namespaces>
        ...
        <add namespace="My.Fully.Qualified.Namespace"/>
    </namespaces>   
</pages>

Of course the assembly which provides the namespaces should also be referenced (project references, web.config's section)

Then you should be able to write things like "<%= MyResx.MyEntry %>

链接地址: http://www.djcxy.com/p/4058.html

上一篇: 100%身高的圣杯布局

下一篇: 如何从ASP.NET标记获取资源字符串?