How to create resource manager in ASP.NET - c#

I would like to create resource manager on my page and use some data stored in my resource files. (default.aspx.resx and default.aspx.en.resx)
The code looks like this:
System.Resources.ResourceManager myResourceManager = System.Resources.ResourceManager.CreateFileBasedResourceManager("resource",
Server.MapPath("App_LocalResources") + Path.DirectorySeparatorChar, null);
if (User.Identity.IsAuthenticated)
{
Welcome.Text = myResourceManager.GetString("LoggedInWelcomeText");
}
else
{
Welcome.Text = myResourceManager.GetString("LoggedOutWelcomeText");
}
but when i compile and run it on my local server i get this type of error:
Could not find any resources appropriate for the specified culture (or the neutral culture) on disk.
baseName: resource locationInfo: fileName: resource.resources
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) on disk.
baseName: resource locationInfo: fileName: resource.resources
Source Error:
Line 89: else
Line 90: {
Line 91: Welcome.Text = myResourceManager.GetString("LoggedOutWelcomeText");
Line 92: }
Line 93:
can you please assist me with this issue?

Use GetLocalResourceObject() which is part of a System.Web.Page's base.
If you wish to access global resources use GetGlobalResourceObject().
keep it simple ;-)

According to MSDN, CreateFileBasedResourceManager is expecting compiled resource files. Usually, one puts .resx files in App_LocalResources. Do you have files in this directory named resource.XXX.resources (where XXX is the current culture id) in this directory.
I don't have a VS handy in order to check this in an ASP.NET project, but the usual way to handle resource strings is to go the project properties, Resources tab and create strings there. This generates a strongly-typed wrapper around your resources, avoiding the need to explicitly create your own ResourceManager.

Related

Where are situated all SharePoint server error codes descriptions?

During execution the code belowe Microsoft.SharePoint.Client.ServerException with ServerErrorCode = -2147024894 has been thrown. But I don't know any information about this code. May be you know where I can find the correspondence between SP ServerErrorCodes and their descriptions ?
private SP.Folder GetFolderByRelativeUrl(string folderRelativePath)
{
var folder = _spClientContext.SpContext.Web.GetFolderByServerRelativeUrl(folderRelativePath);
_spClientContext.SpContext.Load(folder);
_spClientContext.SpContext.ExecuteQuery(); // here exception is thrown
return folder;
}
MSDN has no information about codes' descriptions.
UPDATED:
Full description
Microsoft.SharePoint.Client.ServerException: File Not Found.
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
...
Finally, by chance I've found.
SharePoint server erros codes
Many common error codes are included in the ClientErrorCodes class:
CSOM: Microsoft.SharePoint.Client.ClientErrorCodes
JSOM: SP.ClientErrorCodes

Missing manifest error when trying to retrieve resource via resource manager

I'm having a few troubles with getting resources dynamically as I get "Missing manifest" errors. I looked up a few possible causes and did what was written there but so far nothing worked.
Currently I have this situation:
The resx file I'm trying to access is: "Resources/Messages.resx" (thus in a nonstandard folder.
The code I'm using is this:
ResourceManager resourceManager = new ResourceManager("Resources.Messages", this.GetType().Assembly);
resourceManager.GetString("ResourceText" + MessageType + "Subject")
with messageType being a string. On the second line I get the error message.
The status of the resource file is this:
Build Aciton: Embedded Resource
Custom Tool: PublicResXFileCodeGenerator
Access Modifier Public
The resfile has 2 variants: Messages.res and Messages.de.resx with the same names for each row and also the same general properties (the "GetString" also definitively tries to access the correct name).
So my question is what I'm doing wrong there and what can I do to correct this problem?
Found the problem. For new ResourceManager not only the namespace of the resourcesfiles has to be given but also the default namespace. Thus if the application has a default namespace of: MyApplication.MyServerApp then instead of "Resources.Messages" one must put in: "MyApplication.MyServerApp.Resources.Messages" leading to the following functioning sourcecode:
ResourceManager resourceManager = new ResourceManager("MyApplication.MyServerApp.Resources.Messages", this.GetType().Assembly);
resourceManager.GetString("ResourceText" + MessageType + "Subject")

ASP.net exception "FileNotFoundException" after idle when assembly file is present in the bin folder

The file is present, correctly named and not corrupted. If I move it out and back in from the "Bin", it works again, for about 5 minutes, then the error bellow comes back. Any operation that refreshes the file is fine, publishing it the anew, renaming or moving makes the site work again, for a moment.
{"Message":"Could not load file or assembly \u0027Ouranos,
Version=1.0.0.0, Culture=fr-CA, PublicKeyToken=null\u0027 or one of
its dependencies. Le fichier spécifié est introuvable.","StackTrace":"
at Services.Asynchrone(String DimensionX, String DimensionY, String
Action, String Culture, String Utilisateur, String Interface, String
Source, String Champ, String Valeur, String Classement, String
Direction, StriFileNotFoundExceptionng Page, String
Itérations)","ExceptionType":"System.IO."}
Fusion did give me an error code (0x80070002), which pointed me to get Process Monitor. Which lead me to the temporary assembly folder. Now I may be wrong about this. Comparing the cache files from an healthy website and the sick one, I noticed something odd.
Healthy website as all the DLL from the BIN in cache.
The sick website is missing two DLL in the cache that are present in the BIN.
Now, I know that ASP.net tends to say that the main library is missing when it's in fact one of the referenced library that is missing. In this current situation I don't know what I could do to fix that problem. The two DLL are not set in the cache, thus when it tries to load the main DLL it fails locating the two others from the cache and throws a file not found on the main DLL.
The two culprits are:
PresentationCore.dll
WindowsBase.dll
To troubleshot this kinf of errors, you can use Fusion log, instructions about how to enable it and how to use it can be found here: How to enable assembly bind failure logging (Fusion) in .NET.
It would seem that the following code actually fixes the problem. It checks for all the required assemblies for the assembly and loads the missing. I had such a code before and it did not work, because without the !(Assemblée is System.Reflection.Emit.AssemblyBuilder) && (Assemblée.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder") was not present and has the code causing an exception in .net 4.0 and over. It's not elegant, but it does the job.
public static void Chargeur()
{
var Assemblées_Chargées = (from Assembly Assemblée in AppDomain.CurrentDomain.GetAssemblies() where !(Assemblée is System.Reflection.Emit.AssemblyBuilder) && (Assemblée.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder") && (!Assemblée.GlobalAssemblyCache) && (Assemblée.CodeBase != Assembly.GetExecutingAssembly().CodeBase) select Assemblée).ToList();
var Chemins_Chargés = Assemblées_Chargées.Select(Assemblée => Assemblée.Location).ToArray();
var Chemins_Référencés = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var Assemblées_NonChargées = Chemins_Référencés.Where(Références => !Chemins_Chargés.Contains(Références, StringComparer.InvariantCultureIgnoreCase)).ToList();
Assemblées_NonChargées.ForEach(path => Assemblées_Chargées.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));
}

Cassette.AssetReferenceException being fired when removing a file from folder

I used to have a .js file in /Public/javascripts/jquery1.1js.
Everything was working perfectly but then I needed to delete this file from my project, so I just removed it from the solution in Visual Studio.
Now when I visit my application, I get:
Reference error in "~/Public/javascripts/jquery.unobtrusive-ajax.js",
line 1. Cannot find "~/Public/javascripts/jquery-1.5.1.js". Reference
error in "~/Public/javascripts/jquery.validate.unobtrusive.js", line
1. Cannot find "~/Public/javascripts/jquery-1.5.1.js".
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: Cassette.AssetReferenceException: Reference error
in "~/Public/javascripts/jquery.unobtrusive-ajax.js", line 1. Cannot
find "~/Public/javascripts/jquery-1.5.1.js". Reference error in
"~/Public/javascripts/jquery.validate.unobtrusive.js", line 1. Cannot
find "~/Public/javascripts/jquery-1.5.1.js".
Here's a stacktrace:
[AssetReferenceException: Reference error in "~/Public/javascripts/jquery.unobtrusive-ajax.js", line 1. Cannot find "~/Public/javascripts/jquery-1.5.1.js".
Reference error in "~/Public/javascripts/jquery.validate.unobtrusive.js", line 1. Cannot find "~/Public/javascripts/jquery-1.5.1.js".]
Cassette.BundleContainer.ValidateAssetReferences() +387
Cassette.BundleContainer..ctor(IEnumerable`1 bundles) +41
Granted, I know why this is happening, Cassette is still trying to find the deleted file, but I'm not sure how to tell Cassette: "Hey, this file is no longer relevant. Scan the folder again and rebuild a list of files you need to work with."
But I just don't know how to accomplish this.
The documentation has no mention of this and just implies that it should do this automatically for me.
Here's my Configuration class:
using Cassette.Configuration;
using Cassette.Scripts;
using Cassette.Stylesheets;
namespace XXX.WebUI
{
/// <summary>
/// Configures the Cassette asset modules for the web application.
/// </summary>
public class CassetteConfiguration : ICassetteConfiguration
{
public void Configure(BundleCollection bundles, CassetteSettings settings)
{
bundles.AddPerIndividualFile<ScriptBundle>("Public/javascripts/");
bundles.AddPerIndividualFile<StylesheetBundle>("Public/stylesheets/");
}
}
}
And in my _Layout.cshtml file:
#{
Bundles.Reference("Public/javascripts/site.js");
Bundles.Reference("Public/javascripts/jquery.validate.unobtrusive.js");
Bundles.Reference("Public/stylesheets/site.less");
}
<!DOCTYPE html>
...
<head>
<title>#ViewBag.Title</title>
#Bundles.RenderStylesheets()
...
#Bundles.RenderScripts()
</body>
Does the jquery.validate.unobtrusive.js file contain a JavaScript reference at the top to jquery-1.5.1.js?
The line would look like:
/// <reference path="jquery-1.5.1.js" />
If so, remove that line. Cassette uses those references to determine build order.
ref: http://getcassette.net/documentation/scripts
This can also happen if you reference files with the same root
e.g. easyXDM.js and easyXDM.debug.js
When you reference easyXMD.js it gives the exception and the solution seems to be use a different naming convention that ensures the roots are different such as easyXMDdebug.js.

Error message accessing asp.net Global Resource string programmatically

Trying to access a specific localised resource file such as WebResource.en.us which is located in my App_GlobalResources folder using the following code:
string resData = GetGlobalResourceObject("WebResource.en.us", "SomeResource").ToString();
but this keeps giving me the error below:
Could not find any resources appropriate for the specified culture or
the neutral culture. Make sure "Resources.WebResource.en-us.resources"
was correctly embedded or linked into assembly
"App_GlobalResources.bpqqrnv4" at compile time.
Any ideas ?
I tried the following Code
Page_Load(....)
{
/// note - i did NOT mention the culture when accesing my resourceFiles
Debug.WriteLineIf(
GetGlobalResourceObject("WebResource", "someResource")!=null,
GetGlobalResourceObject("WebResource", "someResource").ToString());
/// accessing a culture specific resource without changing Page Culture
CultureInfo yourCI = new CultureInfo("en-US");
Debug.WriteLine(
HttpContext.GetGlobalResourceObject(
"WebResource",
"someResource",
yourCI).ToString());
}
my Page directive
<% Page Culture="en-US" UICulture="en-US" ..... %>
My App_GlobalResources folder contains two files
WebResource.resx
WebResource.en-US.resx
Using this settings and code - my Debugger printed the value without any problems.
When removing WebResource.resx (my default ressource file) the same code throws an exception.
I would assume that you have to add a default resx file and remove the explicit culture notation in GetGlobalResourceObject(..., ..).
update: added some code to access specific resx culture file
see also MSDN

Categories