Ressources not working anymore

Last post 07-10-2006 6:08 AM by Adamski. 9 replies.

Sort Posts:

  • Ressources not working anymore

    04-06-2006, 10:06 AM
    • Member
      20 point Member
    • Crocmort
    • Member since 02-23-2006, 4:35 PM
    • Posts 5

    I tried to convert a VS 2005 Web site Project to a Web Application project following this tutorial:

    http://webproject.scottgu.com/CSharp/migration2/migration2.aspx

    But I get a LOT of error of the type:

    Error 47 The name 'Resources' does not exist in the current context

    when I use something like this "Resources.Relays.SelEventsWeb.ComTradeErrorCommandLine" witch worked fine before.

  • Re: Ressources not working anymore

    04-06-2006, 10:47 AM
    • Star
      14,608 point Star
    • ScottGu
    • Member since 06-05-2002, 8:36 PM
    • Redmond, WA
    • Posts 2,004
    • AspNetTeam
      Moderator

    I forgot to add an appendix to cover resources in the conversion tutorial -- I will get that added later tonight.

    Basically, if you are using app_globalresource files, then you need to access the resources in a late-bound way within your project.  For example:

    Label3.Text = (String)GetGlobalResourceObject("Strings", "GlobalString");

    Alternatively, you can add .resx files directly to your project (not within the app_globalresources directory, but rather just in the project, and access them in a strongly typed way -- just like with normal class library projects).  If your resource files are only being used within your code-behind and class files, then this is what I'd recommend.

    One of the things we are looking to add for the final release is a strongly-typed proxy class that will also allow you to access app_globalresources resources like you did before in web-site projects.  This isn't implemented in the RC, though, so you'd need to use the late-bound API above.

    Hope this helps,

    Scott

     

  • Re: Ressources not working anymore

    04-06-2006, 11:14 AM
    • Participant
      1,171 point Participant
    • BradleyB
    • Member since 11-06-2002, 3:53 PM
    • Posts 227

    If you’re using App_Global resources to store your resource strings then you'll need to go through the Global Resource Object using GetGlobalResourceObject.

    Label3.Text = (String)GetGlobalResourceObject( "Strings", "GlobalString");

    Here’s what’s going on:  ASP.NET 2.0 will dynamically generate types for any resources stored in App_GlobalResources.  These type will be provided as references to any pages, controls, etc that ASP.Net 2.0 is compiling.  Because Web Site projects in VS05 leverage ASP.NET 2.0 dynamic compilation they have access to the types.

     

    Web Application Projects use the same compilation model as VS03, where the code behind files and other classes are compiled by VS and propped to ~/bin before the ASP.NET 2.0 compilation begins.  Because the code behind files are compiled first they don’t have access early bound access to the dynamic types generated by ASP.NET.  Instead in the code behind and other classes you’ll have to use late binding through the Global Resource Object.

     

    The same problem happens for the profile object.  Check out Appendix 2: Migrating Code that works with the ASP.NET 2.0 Profile Object in this tutorial http://webproject.scottgu.com/CSharp/Migration2/Migration2.aspx

     

    In much the same way you could create a proxy object that provides early bound access to your resources then uses GetGlobalResourceObject to get the actual string.

     

    Before we release the final version we’ll  provide a tool that creates this proxy class for you.

     

    As an alterative to using App_GlobalResources you can store resources in .resx files are part of your projects properties.  This is the same technique that WinForms uses.  Web Application Projects support accessing resources in the same way.

    Label3.Text = Resources.MoreStrings.GlobalKey;

    The one problem with this technique is that the class generated by ResXFileCodeGenerator in VS generates the class as private.  That’s fine for all the code behind files and other support classes because their all in the same assembly.  However, because the .aspx pages and .ascx controls are later compiled by ASP.NET they can’t access private classes in the code behind assembly.

     

    There are a few techniques you can use to get around this problem if you want to use this technique to storing resources.

     

    There is a custom tool named ResXFilePublicCodeGenerator that will generate the class as public.

    http://altinoren.com/resxfilepubliccodegenerator.htm

     

    Some users have provided work arounds using resgen in a build action:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=91371&SiteID=1

     

    The other problem with storing resources in this fashion is that they are not accessible to expression builders in the IDE.  This can be solved with a custom expression builder.

     

    The technique you use will depend on your scenario: 

    1. If you’re migrating a VS03 application I’d continue to use the project Resource properties. 
    2. If you’re building a new ASP.NET 2.0 application I’d consider using App_GlobalResources with a proxy class to access the resources in the code behind files.  
    3. If you’re building a component that is redistributed into other web applications I’d consider using project resource properties of VS.

     Admittedly, accessing resources and profiles is a bit confusing.  We’re working to provide better support within VS but the key to understand the compilation model and when each file within the ASP.NET is compiled.

     

    If you’re using Web Site projects that shipped in VS05 then you’re using dynamic compilation and the entire site is compiled by ASP.NET.

     

    If you’re using Web Application Projects then the code behind files and other support classes are compiled by VS and propped to ~/bin.  Then on a per request basis .aspx pages, .ascx controls, etc… along with all the App_* folders are compiled by ASP.NET on the fly.

     

    I apologize for the long winded answer but I wanted to provide some context and alternatives.

     

    Hope this helps,

    Brad.

     

  • Re: Ressources not working anymore

    04-06-2006, 11:44 AM
    • Member
      20 point Member
    • Crocmort
    • Member since 02-23-2006, 4:35 PM
    • Posts 5

    Thanks for the quick replies!

    I'll probably wait for the final release then (for the proxy object).  Our application is using the global ressources only for the code class in app_code...  I tried to do what you asked and managed to make everything compile again but then had some runtime errors.

    When I finally got to get my login page to run, my themes were not working anymore too.

  • Re: Ressources not working anymore

    04-06-2006, 3:41 PM
    • Member
      515 point Member
    • jnapier
    • Member since 09-09-2002, 7:44 PM
    • Posts 101
    I'd like to put in a vote for the resource proxy class.  Hopefully it will make it into the final release.  I was looking forward to stongly typed resources in ASP.Net 2.0 and was dissapointed to find out that they can not work in the Web Application Project like they do in the Web Project model.
  • Re: Ressources not working anymore

    04-09-2006, 8:35 AM
    • Member
      5 point Member
    • dsimunic
    • Member since 04-09-2006, 12:24 PM
    • Posts 1
    I suppose you can also leave your resx files in App_GlobalResources to make them available in expression builders, and then alias the autogenerated file with a using statement in a codebehind file.

    For example:

    If your project is in the namespace AppProject and resx file is called Strings.resx, WAP will auto-generate  App_GlobalResources\Strings.Designer.cs file. The class in the file will be called  AppProject.App_GlobalResources.Strings. Resource references in your aspx pages will continue to work normally: <%$ Resources: Strings,HelloWorld %>.

    In your codebehind file, you can address this resource via: AppProject.App_GlobalResources.Strings.HelloWorld. Or you can add using Resources = AppProject.App_GlobalResources; at the top of your codebehind, and then reference strings via Resources.Strings.HelloWorld in the code.

    This way you don't need to change the code migrated from web projects, and when the proxy class becomes available, just remove the using statement.


  • Re: Ressources not working anymore

    04-10-2006, 3:05 AM
    • Member
      115 point Member
    • Cine
    • Member since 03-13-2006, 7:16 AM
    • Posts 23
    Hi, I do not understand this :(

    We have a class like this:
    Namespace Resources {
        public class ResourceUtil
        {
          private static ResourceManager global              = Resources.resources.global.ResourceManager;
    ...
        }
    }

    which encapsulates all of this. So in practice we already have the proxy object, but I cannot figure out how to get the ResourceManager for the resx files now.
  • Re: Ressources not working anymore

    06-30-2006, 10:17 AM
    • Member
      220 point Member
    • Adamski
    • Member since 05-09-2006, 5:33 AM
    • Posts 52
    ScottGu:

    I forgot to add an appendix to cover resources in the conversion tutorial -- I will get that added later tonight.

    Basically, if you are using app_globalresource files, then you need to access the resources in a late-bound way within your project.  For example:

    Label3.Text = (String)GetGlobalResourceObject("Strings", "GlobalString");

    Alternatively, you can add .resx files directly to your project (not within the app_globalresources directory, but rather just in the project, and access them in a strongly typed way -- just like with normal class library projects).  If your resource files are only being used within your code-behind and class files, then this is what I'd recommend.



    I wonder if someone can clear up the resx file thing... I dont understand how this needs to be set up. I am migrating a Web Site project to a WAP, and now dealing with "xxx does not exist in the current context" when accessing controls in the aspx from the code-behind file.

    Thanks
  • Re: Ressources not working anymore

    07-07-2006, 3:50 PM
    • Participant
      1,171 point Participant
    • BradleyB
    • Member since 11-06-2002, 3:53 PM
    • Posts 227

    Does "xxx not exist ..." refer to resources or to controls from the .aspx page.  If it's a control reference then I suspect you'll need to run the "Convert to WebApplication" command from the context menu of the file or folder in solution explorer.  This command will convert the .aspx file adding a .designer file with the field declarations as a partial class of the codebehind file.

    Hope this helps,
    Brad.

  • Re: Ressources not working anymore

    07-10-2006, 6:08 AM
    • Member
      220 point Member
    • Adamski
    • Member since 05-09-2006, 5:33 AM
    • Posts 52
    The problem was resolved. The offending page had a custom control on it, and copying the declarations from the designer.cs file to the code-behind solved the problem. I suspect a bug somewhere...
Page 1 of 1 (10 items)