custom resource manager in ASP.NET 2.0

Discussion in 'ASP .Net' started by Martin Bischoff, Aug 31, 2005.

  1. Hi,

    is it possible to implement custom resource managers for ASP.NET 2.0 so
    that strings can be read from a database (for example)?

    Ideally, it should be possible to configure the custom resource manager
    in web.config, so that it replaces the default resource manager. Using
    the <%$ ... %> syntax should read the strings via the custom resource
    manager, e.g. when used like this:


    <asp:Label ID="Label1" runat="server"
    Text="<%$ Resources:Resource, STRING_ID %>"></asp:Label>


    Thanks for any help,
    Martin
     
    Martin Bischoff, Aug 31, 2005
    #1
    1. Advertising

  2. Martin,

    I have done this in one of my project. I created an extended resource
    manager which gets the strings file from the xml rather getting it from the
    compiled resource, so it is possible, that you can extend the asp.net
    resource manager according to your requirement.

    --
    Wajahat Abbas
    http://www.dotnetpakistan.com
    http://www.wajahatabbas.com


    "Martin Bischoff" wrote:

    > Hi,
    >
    > is it possible to implement custom resource managers for ASP.NET 2.0 so
    > that strings can be read from a database (for example)?
    >
    > Ideally, it should be possible to configure the custom resource manager
    > in web.config, so that it replaces the default resource manager. Using
    > the <%$ ... %> syntax should read the strings via the custom resource
    > manager, e.g. when used like this:
    >
    >
    > <asp:Label ID="Label1" runat="server"
    > Text="<%$ Resources:Resource, STRING_ID %>"></asp:Label>
    >
    >
    > Thanks for any help,
    > Martin
    >
     
    =?Utf-8?B?V2FqYWhhdCBBYmJhcw==?=, Aug 31, 2005
    #2
    1. Advertising

  3. Hi Martin,

    Welcome to ASP.NET newsgroup.
    Regarding on the asp.net 2.0 resource manager question you mentioned,
    based on my understanding, this is not available in the comming 2.0 versino
    of the ASP.NET. Since the <%$ %> expression are translated to the actual
    code/statements at compile time, this translation is fixed in the compiler,
    we won't be able to customize it with our own type. Currently I think we
    may consider use the <%# %> databinding experssion with out own custom
    class or function to retrieve the resource as we like.

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | Date: Wed, 31 Aug 2005 09:19:53 +0200
    | From: Martin Bischoff <>
    | User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
    | X-Accept-Language: en-us, en
    | MIME-Version: 1.0
    | Subject: custom resource manager in ASP.NET 2.0
    | Content-Type: text/plain; charset=ISO-8859-1; format=flowed
    | Content-Transfer-Encoding: 7bit
    | Message-ID: <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | NNTP-Posting-Host: 194.209.202.1
    | Lines: 1
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet:121351
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Hi,
    |
    | is it possible to implement custom resource managers for ASP.NET 2.0 so
    | that strings can be read from a database (for example)?
    |
    | Ideally, it should be possible to configure the custom resource manager
    | in web.config, so that it replaces the default resource manager. Using
    | the <%$ ... %> syntax should read the strings via the custom resource
    | manager, e.g. when used like this:
    |
    |
    | <asp:Label ID="Label1" runat="server"
    | Text="<%$ Resources:Resource, STRING_ID %>"></asp:Label>
    |
    |
    | Thanks for any help,
    | Martin
    |
     
    Steven Cheng[MSFT], Aug 31, 2005
    #3
  4. Martin Bischoff

    Brock Allen Guest

    Yes, these can be customized. They're called expression builders. I posted
    in the past month an example, but here's another:

    using System.Web.Compilation;
    using System.CodeDom;

    public class AppSettExBld : ExpressionBuilder
    {
    public static string GetVal(string param)
    {
    return System.Web.Configuration.WebConfigurationManager.AppSettings[param];
    }
    public override System.CodeDom.CodeExpression GetCodeExpression(BoundPropertyEntry
    entry, object parsedData, ExpressionBuilderContext context)
    {
    CodeMethodInvokeExpression ex = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(AppSettExBld)),
    "GetVal", new CodePrimitiveExpression(entry.Expression.ToString().Trim()));
    return ex;
    }
    }


    The method you should customize is GetVal. This sample simply reads from
    <appSettings> but you could read from a database. To configure you need to
    modify the <compilation> section in web.config:

    <compilation debug="true" defaultLanguage="C#">
    <expressionBuilders>
    <add expressionPrefix="AppSet" type="AppSettExBld, __code"/>
    </expressionBuilders>
    </compilation>


    And now you can use <%$ AppSett:SomeKeyToTheGetValMethod %> in your ASPX
    pages.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi,
    >
    > is it possible to implement custom resource managers for ASP.NET 2.0
    > so that strings can be read from a database (for example)?
    >
    > Ideally, it should be possible to configure the custom resource
    > manager in web.config, so that it replaces the default resource
    > manager. Using the <%$ ... %> syntax should read the strings via the
    > custom resource manager, e.g. when used like this:
    >
    > <asp:Label ID="Label1" runat="server"
    > Text="<%$ Resources:Resource, STRING_ID %>"></asp:Label>
    > Thanks for any help,
    > Martin
     
    Brock Allen, Sep 1, 2005
    #4
  5. Thanks a lot for both Wajahat and Brock's informative suggestions.

    Seems I've really missed such a great feature. I'll keep this in mind and
    have some further investigate in the
    asp.ne whidbey.

    Thanks again for your contribution.

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | Message-ID: <>
    | From: Brock Allen <>
    | Subject: Re: custom resource manager in ASP.NET 2.0
    | References: <>
    | Mime-Version: 1.0
    | Content-Transfer-Encoding: 8bit
    | Content-Type: text/plain; charset=iso-8859-1; format=flowed
    | X-Newsreader: JetBrains Omea Reader 469.1
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | Date: Wed, 31 Aug 2005 21:19:41 -0700
    | NNTP-Posting-Host: 12.162.27.25
    | Lines: 1
    | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
    | Xref: TK2MSFTNGXA01.phx.gbl
    microsoft.public.dotnet.framework.aspnet:121612
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    | Yes, these can be customized. They're called expression builders. I
    posted
    | in the past month an example, but here's another:
    |
    | using System.Web.Compilation;
    | using System.CodeDom;
    |
    | public class AppSettExBld : ExpressionBuilder
    | {
    | public static string GetVal(string param)
    | {
    | return
    System.Web.Configuration.WebConfigurationManager.AppSettings[param];
    | }
    | public override System.CodeDom.CodeExpression
    GetCodeExpression(BoundPropertyEntry
    | entry, object parsedData, ExpressionBuilderContext context)
    | {
    | CodeMethodInvokeExpression ex = new CodeMethodInvokeExpression(new
    CodeTypeReferenceExpression(typeof(AppSettExBld)),
    | "GetVal", new
    CodePrimitiveExpression(entry.Expression.ToString().Trim()));
    | return ex;
    | }
    | }
    |
    |
    | The method you should customize is GetVal. This sample simply reads from
    | <appSettings> but you could read from a database. To configure you need
    to
    | modify the <compilation> section in web.config:
    |
    | <compilation debug="true" defaultLanguage="C#">
    | <expressionBuilders>
    | <add expressionPrefix="AppSet" type="AppSettExBld, __code"/>
    | </expressionBuilders>
    | </compilation>
    |
    |
    | And now you can use <%$ AppSett:SomeKeyToTheGetValMethod %> in your ASPX
    | pages.
    |
    | -Brock
    | DevelopMentor
    | http://staff.develop.com/ballen
    |
    |
    |
    | > Hi,
    | >
    | > is it possible to implement custom resource managers for ASP.NET 2.0
    | > so that strings can be read from a database (for example)?
    | >
    | > Ideally, it should be possible to configure the custom resource
    | > manager in web.config, so that it replaces the default resource
    | > manager. Using the <%$ ... %> syntax should read the strings via the
    | > custom resource manager, e.g. when used like this:
    | >
    | > <asp:Label ID="Label1" runat="server"
    | > Text="<%$ Resources:Resource, STRING_ID %>"></asp:Label>
    | > Thanks for any help,
    | > Martin
    |

    |
    |
     
    Steven Cheng[MSFT], Sep 1, 2005
    #5
  6. Thanks to everyone. In the meantime I have also found out about expression
    builders, and these allow me to do what I want.

    I have also read about ResourceProviderFactory, IResourceProvider and
    IResourceReader. But I did not find a way to configure ASP.NET to use my
    custom resource manager instead of the default one (at least for strings).

    Regards,
    Martin

    Brock Allen wrote:
    > Yes, these can be customized. They're called expression builders. I
    > posted in the past month an example, but here's another:
    >
    > using System.Web.Compilation;
    > using System.CodeDom;
    >
    > public class AppSettExBld : ExpressionBuilder
    > {
    > public static string GetVal(string param)
    > {
    > return
    > System.Web.Configuration.WebConfigurationManager.AppSettings[param];
    > }
    > public override System.CodeDom.CodeExpression
    > GetCodeExpression(BoundPropertyEntry entry, object parsedData,
    > ExpressionBuilderContext context)
    > {
    > CodeMethodInvokeExpression ex = new
    > CodeMethodInvokeExpression(new
    > CodeTypeReferenceExpression(typeof(AppSettExBld)), "GetVal", new
    > CodePrimitiveExpression(entry.Expression.ToString().Trim()));
    > return ex;
    > }
    > }
    >
    >
    > The method you should customize is GetVal. This sample simply reads from
    > <appSettings> but you could read from a database. To configure you need
    > to modify the <compilation> section in web.config:
    >
    > <compilation debug="true" defaultLanguage="C#">
    > <expressionBuilders>
    > <add expressionPrefix="AppSet" type="AppSettExBld,
    > __code"/>
    > </expressionBuilders>
    > </compilation>
    >
    >
    > And now you can use <%$ AppSett:SomeKeyToTheGetValMethod %> in your ASPX
    > pages.
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    >> Hi,
    >>
    >> is it possible to implement custom resource managers for ASP.NET 2.0
    >> so that strings can be read from a database (for example)?
    >>
    >> Ideally, it should be possible to configure the custom resource
    >> manager in web.config, so that it replaces the default resource
    >> manager. Using the <%$ ... %> syntax should read the strings via the
    >> custom resource manager, e.g. when used like this:
    >>
    >> <asp:Label ID="Label1" runat="server"
    >> Text="<%$ Resources:Resource, STRING_ID %>"></asp:Label>
    >> Thanks for any help,
    >> Martin

    >
    >
    >
    >
     
    Martin Bischoff, Sep 1, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB

    Resource Manager Question

    JezB, May 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    310
  2. gh0st54
    Replies:
    0
    Views:
    337
    gh0st54
    Jan 29, 2005
  3. Dirc Khan-Evans
    Replies:
    1
    Views:
    899
    Karl Seguin
    Oct 17, 2005
  4. padma
    Replies:
    0
    Views:
    351
    padma
    Oct 3, 2007
  5. Metalone
    Replies:
    0
    Views:
    346
    Metalone
    Jan 6, 2010
Loading...

Share This Page