How to receive ASP.NET LocalResources resx string - c#

I have in my ASP.NET Web Forms Application resx file localized in App_LocalResources\fr.aspx.resx with key value: hello_world.
Now I want to use it on my ASPxLabel on class Page.aspx, so I simply:
<dx:ASPxLabel ID="0" runat="server" Text="<%$ Resources:fr, hello_world %>"></dx:ASPxLabel>
but I have error: Parser Error Message: The resource object with key 'msg' was not found.
I am sure that fr.aspx.resx is publicated, but still got Parser Error. What I am doing wrong?

First of all check the name of your resource file. It has to follow the following naming convension
pageName.extension.language.resx
If you aspx page name Page.aspx, then the you should have resource files with the following name.(You can add resource files for further language and culture)
Page.aspx.fr.resx
Check whether you follow this convension or not.
To you this resource in the aspx page you have to follow the convension as below
<%$Resources:Class,ResourceID%>
where Class is the name of resource file, if it is compiled as class, otherwise this can be ignored.
ResourceId is the key in the resource file.
In your code you are setting the resource as below. Check whether the resource file has a codebehind with the class name fr.
Text="<%$ Resources:fr, hello_world %>">
For more details you can visit the following link
http://msdn.microsoft.com/en-us/library/ms227427(v=vs.90).aspx

Related

The type or namespace name 'Resource' could not be found

I have a resource.resx file where i have listed the variables in key value pair. In "cs" part of my project the resources is recognized.
But in "razor" part in my "cshtml" file i get error. "The type or namespace name 'Resource' could not be found". What is the correct way of using resource in "razor" code?
#using Resource
....
...
<td>Name </td>
Go to your c# code. Click on Resource class (go to class definition - you need just press F12 when your cursor stays on class name). And check namespace of this class. Put this namespace in #using section.
But I recommend you to put definition of this namespace in pages by default. You can read there how to do it.
I just created new MVC project. Added Resource.resx file with String1 property. Build it. All I had to was calling #Resource.String1 in a view, without using #using directive or adding a namespace.
<h2>#Resource.String1</h2>

What is the cause of compilation error in my code

I am running asp.net, C#4.0 application in VS2010. I get the following compilation error:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
Source Error:
Line 1: using System;
Line 2: using System.Collections.Generic;
Line 3: using System.Linq;
My page attribute is :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"
EnableEventValidation="false" Inherits="jsPlumb_jsPlumb_Default2" %>
my c#codebehind class:
namespace CompanyDisplay.jsPlumb.jsPlumb.jsPlumb
{
public class jsPlumb_jsPlumb_Default2 : System.Web.UI.Page
{
}
}
i want namespace to present for running some webservice applications.
Try to do this :
add batch=”false” in the compilation tag of your applications web.config file as shown below
compilation debug=”true” batch=”false”
clean solution
delete the temporary files folder in Visual Studio(the folder which it shows in the exception)
do an iisreset and the rebuild my code again
I got the ANSWER ; I should give the inherit attribute as;
inherits="CompanyDisplay.jsPlumb.jsPlumb.jsPlumb.jsPlumb_jsPlumb_Default2"
This error shows up when code-behind class name doesn't match "inherits" attribute on your aspx page. Maybe you tried to rename something?
Furthermore, I've seen this error, when you try to view a new page, but didn't compile the source, so even though your aspx exists, it can't fight the corresponding code-behind class.
Edit:
You have a completely messed up naming conventions, no surprise you're having troubles to match class names properly.
Sorry, but this is just preposterous!
inherits="CompanyDisplay.jsPlumb.jsPlumb.jsPlumb.jsPlumb_jsPlumb_Default2"
Fix your conventions and maybe you find yourself having source files like this:
CodeBehind="Default.aspx.cs" Inherits="MyNamespace.jsPlumb.Default"
Don't do it like jsPlumb.jsPlumb.jsPlumb....That's just a nightmare!

Why doesn't my ScriptReference find the Embedded Resource?

I have an AJAX control project that has a .js file which is configured as an embedded resource.
My main web application references this project, and when I try to load up the control I get this error:
Assembly does not contain a Web resource with name 'MyFile.js'.
Here is my implementation of getScriptReferences:
public IEnumerable GetScriptReferences()
{
// create reference to the JS
ScriptReference jsReference = new ScriptReference();
jsReference.Assembly = "MyNamespace";
jsReference.Name = "MyNamespace.MyFile.js";
return new ScriptReference[] { jsReference };
}
I'm not quite sure what I'm missing. I've tried changing the Name parameter to be just the file name, the namespace and file name, the namespace, assembly, and file name...and I"m not having any luck. Any suggestions are appreciated.
You have to define the web resource in code on the assembly that contains your embedded resource. Typically you would do this in an AssemblyInfo.vb or .cs file.
[assembly: System.Web.UI.WebResource(
"MyNamespace.MyFile.js",
"text/javascript", PerformSubstitution = true)]
See this article if you need some more help.
Did you make sure to add an entry for the Javascript file into your AssemblyInfo.cs? Something like:
[assembly: WebResource("MyNamespace.MyFile.js", "text/javascript")]
Otherwise, the assembly won't allow access to the resource.

ASP.NET runtime error : Ambiguous Match found

Recently, my team converted ASP.NET project from .NET 1.1 to .NET 2.0. Everything is pretty good so far except for one web page.
This is the error message I got when I tried to open this page:
Server Error in '/' Application.
Parser Error Description: An error
occurred during the parsing of a
resource required to service this
request. Please review the following
specific parse error details and
modify your source file appropriately.
Parser Error Message: Ambiguous match
found.
Source Error:
Line 1: <%# Control Language="c#"
AutoEventWireup="false"
Codebehind="Template.ascx.cs"
Inherits="eReq.Web.WebControls.Template.Template"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"
%> Line 2: Line 3:
function
ExpandCollapse_Template(inBtn,
inSection, inSectionID) {
Source File:
/WebControls/Template/Template.ascx
Line: 1
-------------------------------------------------------------------------------- Version Information: Microsoft .NET
Framework Version:2.0.50727.3053;
ASP.NET Version:2.0.50727.3053
I tried renaming class and renaming filename but it didn't work.
Anyone have any idea on this?
It may appeared because of different names of components? for example Button1 and button1, it compiles as casesensitive, but executed as caseinsensitive.
In your ASCX file, go through each and every control and change its id. For example,
<asp:TextBox id="foo" />
change it to
<asp:TextBox id="foo1" >
You've probably got a control with an ID that matches a property in your ascx file, so when the compiler is trying to make instance variables its colliding.
I've the same problem and it solved and the solution is in check your code behind and you will find a couple of Controls with the same name:
protected Button Home;
protected System.Web.UI.HtmlControls.HtmlAnchor home;
you have to erase one line or comment it.
The selected answer seems to be the right one.
In my case i found that im using a variable in the codebehind with the same name as a control in the aspx file, just with different case usage.
I'd trawl your web.config for 1.1 and 2.0 references to the same DLL. In most cases that I have gotten this it was System.Web.Extensions.
Also check the #registers in Pages if that fails.
Good luck (it is not a fun bug to find!)
Dan
Same Name Id in aspx file and property inside aspx.cs file
when .aspx page and behind aspx.cs class contains Same property this kind of problem occur. When I am searching the solution for this problem.. not found any useful content. finally I solved the problem by renaming private property name to different inside aspx.cs class attached image as screenshot.
if anyone still facing the problem you may try
Screenshot 1
Screenshot 2
This is due to what can only be described as a defect in System.Web.UI.Util.GetNonPrivateFieldType(Type classType, String fieldName) that allows UI (.aspx/.ascx) fields to compile as case-insensitive but attempts to retrieve them as case-sensitive during the intial parse.
A potential remedy for the time being is to catch it at compile-time with an ms-build task.

How to use ResourceManager in a "website" mode?

I am trying here to do a manual translation for the application I am working with. (There is already a working LocalizationModule but it's working dodgy, so I can't use <asp:Localize /> tags.
Normally with ResourceManager you are supposed to be using it as Namespace.Folder.Resourcename (in an application). Currently I am translating an existing asp.net "website" (not web application so no namespace here....).
The resources are located into a folder name "Locales/resources" which contains "fr-ca.resx" and "en-us.resx".
So I used a code with something like this :
public static string T(string search)
{
System.Resources.ResourceManager resMan = new System.Resources.ResourceManager( "Locales", System.Reflection.Assembly.GetExecutingAssembly(), null );
var text = resMan.GetString(search, System.Threading.Thread.CurrentThread.CurrentCulture);
if (text == null)
return "null";
else if (text == string.Empty)
return "empty";
else
return text;
}
and inside the page I have something like this <%= Locale.T("T_HOME") %>
When I refresh I have this :
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"Locales.resources" was correctly
embedded or linked into assembly
"App_Code.9yopn1f7" at compile time,
or that all the satellite assemblies
required are loadable and fully
signed. Description: An unhandled
exception occurred during the
execution of the current web request.
Please review the stack trace for more
information about the error and where
it originated in the code.
Exception Details:
System.Resources.MissingManifestResourceException:
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"Locales.resources" was correctly
embedded or linked into assembly
"App_Code.9yopn1f7" at compile time,
or that all the satellite assemblies
required are loadable and fully
signed.
Source Error:
Line 14:
System.Resources.ResourceManager
resMan = new
System.Resources.ResourceManager(
"Locales",
System.Reflection.Assembly.GetExecutingAssembly(),
null ); Line 15: Line 16: var
text = resMan.GetString(search,
System.Threading.Thread.CurrentThread.CurrentCulture);
Line 17: Line 18: if (text == null)
Source File:
c:\inetpub\vhosts\galerieocarre.com\subdomains\dev\httpdocs\App_Code\Locale.cs
Line: 16
I even tried to load the resource with Locales.fr-ca or only fr-ca nothing quite work here.
Marvin Smit's solution is great if you do not have access to the HTTPContext
const string ASSEMBLY_NAME = "App_GlobalResources";
const string RESOURCE_NAME = "Resources.MetaTagResource";
const string RESOURCE_MANAGER = "ResourceManager";
Assembly assembly = Assembly.Load(ASSEMBLY_NAME);
Type type = assembly.GetType(RESOURCE_NAME);
PropertyInfo propertyInfo = type.GetProperty(RESOURCE_MANAGER);
ResourceManager resourceManager = propertyInfo.GetValue(null, new object[] { }) as ResourceManager;
resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
But if you have access to the HTTPContext just use HttpContext.GetGlobalResourceObject
string title = HttpContext.GetGlobalResourceObject("MetaTagResource", "Title").ToString();
string keywords = HttpContext.GetGlobalResourceObject("MetaTagResource", "keywords").ToString();
string description = HttpContext.GetGlobalResourceObject("MetaTagResource", "Description").ToString();
Maybe you are just looking for The System.Web.Page's base GetLocalResourceObject(),?
see http://msdn.microsoft.com/en-us/library/ms153597.aspx
When using resources in ASP.Net, you will see a specialized compiler being used (custom Tool property) on the .resx files (ResXCodeGen)
Bottom line of this is that your resources are compiled into a class. The name of this class is the name you should use when you want to address the resources in there.
The name of the class is generated as follows:
{project namespace}{folder structure}{filename}.resx (.resx is NOT part of the generated name)
Since you mentioned your resx file is located in a directory name "Locales/resources", the name you would have to use is (assuming the resx file is called 'locales.resx')
"locales.resources.locales"
The addition of the language and locale, like the "uk-en" is added by the resource manager, loading the appropriate assembly based on the Culture that is specified when creating an instance of the ResourceManager. If none is given, the Thread.CurrentCulture is used. You should not use the language/locale extensions yourself, leave it to the resource manager to deal with that.
If you are not sure how the resources class will end up, you can always look at the MSIL or use reflector to determine the name of the resource class in the actual deployed assembly.
;-- Added after comments where placed -------
I also looked at some other code i had arround;
Have you tried the Reflection on App_GlobalResources approach?
1: You load the "App_GlobalResources" library while in the context of the website (in a HttpHandler, Aspx, Ascx, etc). =>
Assembly.Load("App_GlobalResources");
2: Walk through the available types in there and grab the "ResourceManager" property from each type.=>
PropertyInfo pi = type.GetProperty("ResourceManager");
resManager = pi.GetValue(null, new object[] { }) as ResourceManager;
3: If it had one, get the ResourceSet you want => resManager.GetResourceSet(englishCulture, true, true);
Hope this helps.
this is not c# but you can easily convert it into it
I have in the app_localResources directory with 2 files
myPage.aspx.resx
myPage.aspx.fr-ca.resx
and I use it like this in my .aspx page
<asp:Label ID="lAddress1" runat="server" Text="<%$ Resources: lAddress1 %>"></asp:Label>
this is how I manage it on mine
'using LCID from
Public Enum elang
En = &H1009 'en-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=1009&OS=Windows%202003%20%20Service%20Pack%201
Fr = &HC0C 'fr-CA http://www.microsoft.com/resources/msdn/goglobal/default.mspx?submitted=0C0C&OS=Windows%202003%20%20Service%20Pack%201
End Enum
'strongly typed value in session
Private _lang As elang = elang.En
Public Property lang() As elang
Get
Return _lang
End Get
Set(ByVal value As elang)
_lang = value
End Set
End Property
and in every page I got
Protected Overrides Sub InitializeCulture()
If Not Me.IsPostBack Then
Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo(Sess.lang)
Threading.Thread.CurrentThread.CurrentCulture = Globalization.CultureInfo.CreateSpecificCulture(Threading.Thread.CurrentThread.CurrentUICulture.Name)
End If
MyBase.InitializeCulture()
End Sub
when I want to use the resourcemanager I create a global resources file at the root level the I use it like this
Dim rm = New System.Resources.ResourceManager("Resources.MyPage", Reflection.Assembly.Load("App_GlobalResources"))
Edit
In case of a Web Site project, read this walkthrough of Microsoft.
I'm afraid you can't use the ResourceMagager in a Website project, because it requires a namespace / assembly where the resources are located.

Categories

Resources