I´m having a strange problem with resources files. Strange because it was working great.
Well, I have some resources files. I created a Resources folder on my asp.net mvc 3 project structure and I am mirroring the views's folder structure. For now I have:
Views
Shared
_Layout.cshtml
Resources
Shared
Layout.pt-BR.resx
Layout.en-US.resx
Layout.ko-KR.resx
All resources files are configured as Embedded Resource, PublicResXFileCodeGenerator and with Custom Tool Namespace "ViewRes". All ".Designer.cs" files are empty. I remember to see some code in them when it was working.
On my .csproj file I have these data:
<ItemGroup>
<EmbeddedResource Include="Resources\Shared\Layout.en-US.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Layout.en-US.Designer.cs</LastGenOutput>
<CustomToolNamespace>ViewRes</CustomToolNamespace>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Shared\Layout.ko-KR.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator>
<CustomToolNamespace>ViewRes</CustomToolNamespace>
<LastGenOutput>Layout.ko-KR.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Shared\Layout.pt-BR.resx">
<SubType>Designer</SubType>
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Layout.pt-BR.Designer.cs</LastGenOutput>
<CustomToolNamespace>ViewRes</CustomToolNamespace>
</EmbeddedResource>
</ItemGroup>
The problem is: When I try to access the #ViewRes on my views, It is not recognized. I tried already to remove the custom tool namespace but the #Resource is not recognized too. In both situations I got this compilation error:
Compiler Error Message: CS0103: The name 'ViewRes' does not exist in the current context
Does anybody have any idea?
Thanks,
Paulo
EDIT: NEW INFORMATIONS
I created a global Resources.resx file by project's properties menu in VS2010 and it's working with the custom tool namespaces I have entered. "Resources" namespace still not working. I think the problem is with the local resources files. Unfortunatly I need them because I can't rename the global resources.resx file including the culture code.
EDIT 2
I created new .resx file on Resource folder and what I can see is that the filename can not have 2 ".". Is the name is Resources.resx, it works. If the name is Resource-en-US.resx, it works but, If the name is Resources.en-US.resx it fails!
I think you can also write a custom helper, that will return by reflection the string.
Related
This may be another simple question. I did my research and I couldn't find a way to resolve the issue. I have .net 6 web api project. the project folder does not have a wwwroot folder. I placed images folder within the project root. i.e. src\WebUI\images\ where src\WebUI\WebUI.csproj exists. these images are being used only for email templates.
I had no issues with the development environment. but when I try to deploy files to the server, I noticed that the image folder was not included in the release build.
I have following middleware in program.cs
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "images")),
RequestPath = "/images"
});
Could someone please tell me what am I missing here?
I've just realized after posting the question that I need to add following to my src\WebUI\WebUI.csproj
<ItemGroup>
<Content Include="images\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Before modifying a resource file, it looks like this in VS.NET (2013):
Strings.resx
Strings.Designer.cs
After saving, it looks like this:
Strings.resx
Strings.Designer.cs
Strings1.Designer.cs
If I try to run the project, I get an error that Strings already exist. I have to then delete Strings.Designer.cs and rename Strings1.Designer.cs to Strings.Designer.cs.
Then I have to unload the project and modify those references in the project file XML. Then reload and all is well until I modify again.
Any idea how to fix this?
I faced the same issue. I deleted my old resource file and created a new resource file with same name and content. Now it is working fine.
So, delete the Strings.resx file and create a new file with the same name that is Strings.resx
I found the answer from this link Generating *.Designer.cs from .resx.
Basically, you need to:
Right-click on your project and click Unload.
Then right-click again and click on Edit on your project.
Find the string <LastGenOutput> and change the entry to whatever you want.
For example, I need to generate Strings.Designer.cs:
<ItemGroup>
<Compile Update="App_GlobalResources\Strings.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="App_GlobalResources\Strings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
I hope this post can help someone in the future.
Cheers
Is there an extension to easily link a file to another in a VS project? The same way a designer file is linked to another file.
I know i can open include the file in the project then open the project in notepad and change
the include element to depend on another file like
<None Include="Features\ContactTestData.json">
<DependentUpon>ContactInfo.feature</DependentUpon>
</None>
but is there a way to do this through the UI?
It sounds as if partial classes may solve your issue. Which is how the form designers work.
It allows you to have multiple class files with the same name in the same namespace accessible as the same class.
I am attempting to embed an unmanaged dll in my console project. The default namespace of the project is Company.Project1Exe. The Assembly Name (output exe ) is named project1.exe
The dlls are added to the project using the Add as Link option and are located in a Libs\x86 subfolder
Company.Project1Exe
|
|--Program.cs
|--Libs
|--x86
|-My1st.dll
|-My2nd.dll
They have been added to the project using the Add as Link option, thus are not physically locate in the Libs subfolder.
I have set the Build Action of both these dlls to 'Embedded Resource'.
By default, MSBuild will embed these dlls using the DefaultNamspace.ExtendedNamespace.FileName where the ExtendedNamespace represents the directory structure of the project.
This results in resource being embedded as Company.Project1.Libs.x86.My1st.dll and Company.Project1.Libs.x86.My2nd.dll respectively.
I want these resources to embedded using the Assembly Name so that they are embedded as Project1.Libs.x86.My1st.dll and Project1.Libs.x86.My2nd.dll respectively.
How can I do this?
The one method that can address this issue is to set the LogicalName of the embedded resource. By default when you embed a resource, you will find an entry in your csproj file similar to
<EmbeddedResource Include="path to embdedded resource"/>
In the case of resources that are added using Add as Link, you will find an additional Link attribute. In this case, the Link attribute is the path of the resource relative to your project structure and the Include attribute is the pointing to file's location on your machine (relative to your project).
<EmbeddedResource Include="path to embdedded resource"/>
<Link>Libs\x86\My1st.dll</Link>
</EmbeddedResource>
In order to get the assemblies embedded using a different namespace the LogicalName attribute can be added to the above which allows one to override the default msbuild behaviour.
<EmbeddedResource Include="path to embdedded resource"/>
<Link>Libs\x86\My1st.dll</Link>
<LogicalName>$(TargetName).Libs.x86.My1st.dll</LogicalName>
</EmbeddedResource>
The downside it would seem, is that one will need to do this for every resource added. I would however have preferred that this convention be set in some way such that this can be the default way to embed any resource in my project i.e. use the $(TargetName) as a replacement for the default namespace
I have a WinForm project that contains a form called MainUI. You can see that the automatically generated partial class shows up as a node under MainUI.cs. Is there a way to "move" my self created partial class MainUI.Other.cs under MainUI.cs so that it'll show as another node?
Close the solution in Visual Studio, and open your .csproj file in a text editor. Find MainUI.Other.cs, and add the following XML element:
<Compile Include="MainUI.Other.cs">
<SubType>Form</SubType>
<DependentUpon>MainUI.cs</DependentUpon> <!-- this is the magic incantation -->
</Compile>
Reopen the solution in Visual Studio and enjoy subnodular goodness.
That said, you may want to reconsider whether this is a good idea. The reason the .designer.cs file is displayed as a subnode is because you won't normally need or want to open it, because it contains generated code which you'd normally view or edit through the designer. Whereas a partial class file will contain your code, that you'll want to edit and view; it may be confusing to maintenance programmers if the file is not easily visible in Solution Explorer. However, only you can know what's right for your project -- just something to bear in mind!
Yes, this is possible, but you will have to hand edit the project file.
In the project file (open it with the XML Editor) locate the file listing item group. In my example, I left the form as "Form1.cs". Add the child element "<DependentUpon>" to your extended class as per the example below:
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Form1.Designer.Other.cs">
<DependentUpon>Form1.cs</DependentUpon>
<SubType>Form</SubType>
</Compile>
Typically though you wouldn't want any non-generated code to be hidden as a child node though. My normal practice is to create a folder in the project called "Partial Classes" and add them all in the same location.
You can modify the project source file to group the related files.
In the project source file, find ItemGroup element which contains MainUI.cs and add an entry for MainUI.Others.cs
Here a blog post showing how to do it in details.
Group/nest source code files
Just to add to #itowlson's answer: if you're getting an error such as "Duplicate 'Compile' items were included." when compiling, that's probably because the files you're telling it to include are already included using a wild card.
The solution is to remove then add them to the compile config like so:
</Project>
<ItemGroup>
<Compile Remove="MainUI.Other1.cs" />
<Compile Remove="MainUI.Other2.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainUI.Other1.cs">
<DependentUpon>MainUI.cs</DependentUpon>
</Compile>
<Compile Include="MainUI.Other2.cs">
<DependentUpon>MainUI.cs</DependentUpon>
</Compile>
</ItemGroup>
</Project>