With ASP.NET Core's ResourcesPath set to "Resources", how must a controller's .resx be named, and why might every lookup return the key?
answer
- root namespace plus folder plus type
- dot naming or folder naming
- assembly name is not root namespace
- [assembly: RootNamespace] and [assembly: ResourceLocation]
- SharedResource inside Resources doubles the path
basics
~10 sThe .resx must mirror the type's full name minus the root namespace, under ResourcesPath: Resources/Controllers/CatalogController.fr.resx or Resources/Controllers.CatalogController.fr.resx. A mismatched root namespace, a misplaced marker class or a library type makes the lookup miss silently.
solid answer
~40 s`ResourceManagerStringLocalizerFactory` builds a base name as root namespace + `ResourcesPath` (slashes become dots) + the type's full name with the root namespace trimmed. For `WineShop.Controllers.CatalogController` that is `WineShop.Resources.Controllers.CatalogController`, which both `Resources/Controllers/CatalogController.fr.resx` (folder naming) and `Resources/Controllers.CatalogController.fr.resx` (dot naming) produce when compiled. The root namespace is read from `[assembly: RootNamespace]` or, without it, the assembly name, so a project like `wine-shop.csproj` whose namespace is `wine_shop` misses until you add that attribute. A `SharedResource` class placed in namespace `WineShop.Resources` doubles the folder. A type in a class library looks in that library's own assembly, located by its `[assembly: ResourceLocation]` or the app's `ResourcesPath`. Every miss returns the key, so compare `LocalizedString.SearchedLocation` with the file's path.
code
csharp · 12 lines// wine-shop.csproj: assembly name "wine-shop", root namespace "wine_shop"
// Properties/AssemblyInfo.cs
using Microsoft.Extensions.Localization;
[assembly: RootNamespace("wine_shop")]
// SharedResource.cs at the project root, NOT inside the Resources folder
namespace wine_shop
{
// with ResourcesPath = "Resources": looked up as Resources/SharedResource.de.resx
public sealed class SharedResource { }
}go deeper
Recall that the .resx is named after the class's namespace and type plus a culture suffix, placed under the folder ResourcesPath names.
Explain the base-name formula, why dot and folder naming both work, and why a mismatch returns the key instead of failing.
Diagnose a silent miss from SearchedLocation or the Debug log: root namespace versus assembly name, a marker class in the wrong namespace, or a library's resources in the wrong project.
Decide how resources are organised across several projects: per-type files versus a shared resource, library-owned translations, and how renames are kept from silently breaking lookups.
## How the factory turns a type into a resource name `IStringLocalizer<T>` asks `ResourceManagerStringLocalizerFactory.Create(typeof(T))` for a localizer. The factory computes a **base name** and hands it, together with `T`'s assembly, to a .NET `ResourceManager`. The rule in the source is: - if no resource path is configured, the base name is the type's **full name**; - otherwise it is `{RootNamespace}.{ResourcesPath}.{FullTypeName minus RootNamespace}`, with any `/` or `\` in the path replaced by dots. The build compiles each `.resx` into a manifest resource named after the project's root namespace, the file's folder (with dots) and the file name; the culture part (`.fr`) sends it into a satellite assembly for that culture. A lookup succeeds only when the two names agree. | Type | `ResourcesPath` | Base name searched | Files that match (French) | |---|---|---|---| | `WineShop.Controllers.CatalogController` | `"Resources"` | `WineShop.Resources.Controllers.CatalogController` | `Resources/Controllers/CatalogController.fr.resx` or `Resources/Controllers.CatalogController.fr.resx` | | `WineShop.Controllers.CatalogController` | not set | `WineShop.Controllers.CatalogController` | `Controllers/CatalogController.fr.resx` or `Controllers.CatalogController.fr.resx` at the project root | | `WineShop.SharedResource` | `"Resources"` | `WineShop.Resources.SharedResource` | `Resources/SharedResource.fr.resx` | **Dot naming** and **folder naming** are interchangeable; pick one per project for tidiness. ## The wine shop's layout at a glance With root namespace `WineShop` and `ResourcesPath = "Resources"`, a working project looks like this: - `Controllers/CatalogController.cs`, namespace `WineShop.Controllers`; - `Resources/Controllers/CatalogController.fr.resx` and `Resources/Controllers/CatalogController.de.resx`; - `SharedResource.cs` at the project root, namespace `WineShop`; - `Resources/SharedResource.fr.resx` and `Resources/SharedResource.de.resx`. The class files and the resource files live in different folders; what ties them together is the **type's full name**, not the location of the `.cs` file. Renaming `CatalogController`, or moving it into `WineShop.Controllers.Shop`, changes its base name, and nothing warns you that its `.resx` files no longer match. ## Where the root namespace comes from The build knows the project's `RootNamespace`, but the running process does not. The factory therefore reads `[assembly: RootNamespace("...")]` if present and otherwise uses the **assembly name**. They usually match. They do not when the project file name is not a valid identifier: `wine-shop.csproj` produces assembly `wine-shop` and root namespace `wine_shop`, so the factory searches `wine-shop.Resources...` while the resources are named `wine_shop.Resources...`. Every lookup returns the key. The fix is one line: ```csharp [assembly: Microsoft.Extensions.Localization.RootNamespace("wine_shop")] ``` ## The shared marker class trap A common layout keeps translations shared by many pages in one resource, named by an empty marker class. If that class is created inside the `Resources` folder, Visual Studio gives it the namespace `WineShop.Resources`. The factory trims the root namespace (`Resources.SharedResource`) and then prepends `ResourcesPath`, searching `WineShop.Resources.Resources.SharedResource`. The file is at `Resources/SharedResource.fr.resx`, so nothing is found. Two fixes: 1. keep the marker class in the root namespace, `WineShop.SharedResource`; 2. or ask for the resource explicitly with `factory.Create("SharedResource", assemblyName)`, where the second argument is the **assembly name** the resources live in. ## Resources in a class library `Create(Type)` looks in **the type's own assembly**. A `CheckoutMessages` type in `WineShop.Ordering.dll` needs its `.resx` files inside that library project, not in the web project. The factory also asks that assembly for `[assembly: ResourceLocation("...")]`; the attribute wins, and without it the app's `ResourcesPath` is assumed for every assembly. A library that keeps its files at its own root while the app sets `"Resources"` therefore misses until it declares its location. ## Diagnosing a silent miss Because a miss returns the key, a naming error looks exactly like a missing translation. Work through it in order: 1. read `LocalizedString.SearchedLocation`, or turn on `Debug` logging for `Microsoft.Extensions.Localization` and read the "searched for ... in ..." line; 2. compare that base name with the root namespace, folder and file name of the `.resx`; 3. check the root namespace against the assembly name; 4. check that the `.resx` is in the same project as `T`; 5. confirm the type was not renamed or moved to another namespace after the file was created. ## What interviewers listen for - knowing that the **type's namespace**, not the folder of the class file, drives the name; - knowing both naming styles work; - knowing the assembly-name fallback and the `RootNamespace` attribute; - spotting the doubled `Resources.Resources` segment without running the app.
- A SharedResource marker sits in Resources/SharedResource.cs, ResourcesPath is "Resources", and every shared string comes back as its key. What happened?The class took the namespace `WineShop.Resources`. The factory trims the root namespace, leaving `Resources.SharedResource`, then prepends `ResourcesPath`, so it searches `WineShop.Resources.Resources.SharedResource` while the file compiles to `WineShop.Resources.SharedResource`. Move the class to the root namespace, or call `factory.Create("SharedResource", assemblyName)`.
- Which wins for a class library: the app's ResourcesPath or the library's ResourceLocation attribute?The attribute. The factory asks the type's assembly for `ResourceLocationAttribute` first and falls back to `LocalizationOptions.ResourcesPath` only when the assembly has none. The `.resx` files still have to be compiled into the library itself, because `Create(Type)` searches the type's own assembly.
- ResourcesPath is searched recursively, so any matching .resx under it is found.
- Dot naming and folder naming are different conventions, and only one is supported.
- The assembly name and the root namespace are interchangeable for localization.
- Putting SharedResource inside the Resources folder namespace is the recommended layout.
- A library type's translations can live in the web project's Resources folder.