I have three resource files in App_GlobalResources for a view:
ConditionsLabels.en.resx
ConditionsLabels.ua.resx
ConditionsLabels.ru.resx
But I can't get any value.
I specified custom namespaces (ViewRes), but in the view autocomplete suggests the only variant #ViewRes.ConditionsLabels_ua. (it's class)
How can I use that?
Don't use App_GlobalResources for MVC projects. Instead, place your .resx files in a standard folder (say, "Resources" or whatever). Next, select the .resx files in Visual Studio and change the following properties:
Custom Tool: PublicResXFileCodeGenerator
Custom Tool Namespace: Resources
Now you should be able to access your resources normally using Resources.MyResource.String1, etc.
Related
I'm trying to create new resources files in VS2015. I created them fine, but when I try to change the "Access Modifier", the dropdown is disabled...
Any ideas?
I'm working in a ASP.NET MVC 6 (ASP.NET5). The project is a Class Library, but I have tested in Web Project with same results.
Thanks!!
Edit: Added Properties window
Can you check the property of your file and verify if Custom Tool is ResXFileCodeGenerator ? If it's GlobalResourceProxyGenerator, the dropdown will be disabled.
I had the same problem but I found this easy solution Visual Studio's Access Modifier drop down option is disabled for resource file
To summarize:
Right click on your file resource, choose Properties (Alt+Enter)
Change Build Action to Embedded Resource
Change Custom Tool to PublicResXFileCodeGenerator
A) In Asp.net Core projects
This problem is a known bug in Asp.net Core projects and access modifier is on public by default and you can not change it. It will be solved by asp.net core team next updates but if you need internal access modifier you can use my temporary solution:
Add your all items by the resource designer in your Resource.resx and save it
In the solution explorer expand the Resource.resx tree and open Resource.Designer.cs
Replace all public strings in it with internal and save it
Note: every time you save the Resource.resx file you should do the step 3 again.
Finally you should have a Resource.Designer.cs file with access modifiers like this:
Also check the namespace in Resource.Designer.cs file. it should be a appropriate namespace. Sync with your project namespace.
B) In Normal Asp.net projects
If you have not CustomTool property in the Properties panel for your resource (.resx) file to change it to
PublicResXFileCodeGenerator and solve the problem
Then you should change some settings in your project (.csproj) file manually. It's so easy, just follow my instructions:
Right click on your project in solution explorer and select Unload Project
Right click again on it and select Edit .....csproj
In the opened .csproj file, find the .resx string, you will see a block of settings there. That is something like bellow codes.
Change it to something like the following code (include
PublicResXFileCodeGenerator):
.
<EmbeddedResource Include="MyResourceFile.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator> <!--important line-->
<LastGenOutput>MyResourceFile.Designer.cs</LastGenOutput>
</EmbeddedResource>
Save the edited .csproj file
Right click again on your project in the solution explorer and select Reload project
Now open your .resx file and enjoy ;)
Note: use your resource file name instead of MyResourceFile
I've been having a similar problem.
This appears to be a known issue: https://github.com/aspnet/Tooling/issues/339
I have a template service that uses RazorEngine to generate HTML for email sending.
I want to make it easy to add a new template to the project, meaning that I don't want to specify the file build action.
Is there a way where I still have the .cshtml file in my project with intellisense and etc. but it automatically will set their build action to Embedded Resource?
Here is a example of my project structure
Wildcard inclusions in C# project files
However, we can use wildcards when specifying these files. Here is an example that includes all .py files from a folder (and all subfolders):
EmbeddedResource Include="Python27\Lib\**\*.py"
Once you save the project file and reload it in Visual Studio, you will see that the Solution Explorer understands the wildcard and will populate with all files that match it. You can copy new .py files into the target directory and Solution Explorer will update accordingly (you need to click the “Refresh” button for this). And while it shows the results of the wildcard inclusion, it does not alter the entries in the project file unlike the behavior of the Add Files dialog.
I have multiple projects in a solution and I'd like them all to share one pool of graphics resources. I've tried this:
1 - Created project1, made its resource file public, added some graphics to it.
2 - Created project2, Alt+dragged Resources.resx from project1\Properties to project2 (not in the Properties folder)
3 - Add reference in project2 to project1
So, now all the images from project1 are available in project2. So far, so good. I can use them at design time just fine.
But, when I want to access them at runtime, I try this (in project2)...
Image img = project1.Properties.Resources.image14;
And that crashes with a MissingManifestResourceException.
What am I doing wrong here? Is there a better way I could approach this? All I'm trying to do is maintain all my graphics in one place, so if I add a resource, it becomes available to all projects in the solution.
Just built an example following these steps:
Create a class library do hold the resources (Project 1)
Create the consumer project (Project 2)
Add a resource file (GlobalResources.resx) in the Project 1 and add a resource item Information
Change the BuildAction of the resource file to Embedded Resource
Change the Do not copy of the resource file to False
Check if the Custom Tool of the resource file is set to PublicResXFileCodeGenerator
Add a reference to the class library (Project 1) to the consumer project (Project2).
Add the resource namespace reference wherever you want to use it.
Finally it is working: GlobalResources.Information
It should be simple.
Edit:
You are concerned about using an external resource file inside the design time property editor. Sorry to inform that there is no standard support for this :(
However, if you think that the benefits are greater than the effort:
Issue with shared WinForms resources across projects in Visual Studio
How do I get the Windows Forms Designer to use resources from external assembly?
Hope it helps.
Choose the referenced file in your solution explorer, then properties, then see what the "copy to output" property looks like. I suspect it's not set to "Copy Always" or "Copy if Newer" of which either should be fine.
Once it's being copied, let's also check to see where it's being copied. Is the output path for that particular item the same as where the program ultimately expects? Is it being copied to the bin\Debug of the correct project?
Make sure it's being copied to the path where the MissingManifestResourceException says it's failed to find the resource.
Finally, given additional information in our comments, I would also suggest you verify the following:
culture the resources are targeting. Check spelling and capitalization.
any culture settings of your build xml or publish xml.
culture setting(s?) of your host system that's running this code.
When I write my APIs for applications I always follow a set pattern (who doesnt!?) that looks like this:
User (Folder/Namespace)
User.cs
UserDao.cs
UserDaoFactory.cs
UserService.cs
UserServiceFactory.cs
I know Resharper has file templates which I already utilize, but it would be great if I didn't have to individually add each file which can get extremely tedious when you are working with upwards of 30 entities in an app. If you can't do this with resharper is there anyway to make some kind of macro with Visual Studio that will execute your resharper file templates?
Create all these classes in a single file (create a file template containing the set of standard classes if you want to)
Write your logic
Press Shift+Alt+L to put focus on the file in Solution Explorer
Press Ctrl+Shift+R to display Refactor this menu with a list of applicable refactorings.
Select Move Types into Matching Files, complete the refactoring wizard, and watch the classes separating into individual files.
To put all the newly created classes into a folder, select them in Solution Explorer, and again invoke Refactor this.
This time, select Move to Folder.
Specify the name of the new folder in the refactoring wizard, press Create this folder, and complete the wizard.
Looks like R# 8 added this feature
From the Resharper Help Page
Starting from ReSharper 8.0, each file template can add more than one
file when it is applied. This > may be helpful for WPF, Web and other
projects where related data can be saved in different files.
Here are a few things you need to know about multi-file templates:
A file template always include one main file, and, optionally, any
number of secondary files. So as soon as you add a secondary file to
any file template you get a multi-file template.
When adding a
secondary file, you can reference any existing file template (Add file
from existing template option). Note that if the referenced template
has multiple files too, then only the main file of the referenced
template will be added when you apply the template.
The set of
parameters spans all files of a multi-file template. I.e., if you use
the same parameter $PARAM$ in several files of a multi-file template,
it will have the same value in all related files when the template is
applied.
If a multi-file templates has editable parameters, the Hot
Spot Session, which deploys when you apply this template, will guide
you through all created files where the user input is required.
I would consider using .tt templates with ReSharper.
create master file with ReSharper.
using one or many tt files to auto generate dependent (.partial.cs) files with partial keyword in it.
extend generated classes if needed.
I want to use HttpContext.GetGlobalResourceObject for localizing strings in my application, but I am unable to create the xml file, i want to know how should i form my XML resource file so that it can be read from the HttpContext.GetGlobalResourceObject method.
The easiest way is to use Visual Studio to add and edit the resource files.
To add a global resource file to a web project, right-click the project, select Add > Add ASP.NET Folder > App_GlobalResources (if it's not already visible in Solution Explorer).
Then right-click App_GlobalResources folder, select Add > New Item..., then select the Resources File item (.RESX), type in a name and click OK.
Visual Studio will also open the RESX file after adding it to the project, and you can enter resource keys and values from there. No need to get into the XML for basic operations.
If you are interested in the XML structure you can open up the RESX file in a text editor to see the underlying XML format.