MVC 4 error in accessing resource file from controller - asp.net-mvc

Getting error in accessing resource file string from MVC controller(working file with views)
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "TempNameSpace.App_GlobalResources.Resource_EN_US.resources" was correctly embedded or linked into assembly "TempNameSpace" at compile time, or that all the satellite assemblies required are loadable and fully signed.
What I have already done
1. Resource file build action : from content to Embedded resource
Result : Publish does not include resource files in it.
2. Added new resource file with name Resource.resx
Result : Same error
Solution hierarchy
1. Solution contains 4 projects, two with resource files(Resource.resx and Resource_EN_US under App_GlobalResources folder)
2. Designer for Resource_EN_US contains correct namespace
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TempNameSpace.App_GlobalResources.Resource_EN_US", typeof(Resource_EN_US).Assembly);

In my projects I use a seperate project for resources. I have set all resource files Build Action property to Embedded Resource.
On the reference to the resources project, I have Copy Local set to True for the resources project. (Rightclick a reference and select Properties)
If you have your resource files inside your web project, I think that the property corresponding with Copy Local should be Copy to Output Directory (Also accessible via rightclick and Properties).
Edit:
I see that in your comment you said that you access the file as Resource.Resource_EN_US.WaterMarkEmail but this is not the right way to access the files. You should access them as one single file: Resources.Property.
My files are named as: Standard.resx, Standard.en.resx, Standard.no.resx and so on. To access them I can use Standard.Property. The resource files will mold toghether, but only if you give them valid names. If you can access all your resource files individually, then you have not used the correct naming convention.
I recommend implementing a ResourceHelper that uses the ResourceManager to access the property that you need.

Related

F# Embedded resource is ignoring subfolder structure

I am trying to embed resources (.xsd) to my project. The folder structure is as follows
- Project
project.fsproj
- subfolder
- v1
file.xsd
- v2
file.xsd
This file is embedded as a recourse and I would expect that I can access the files as follows {Namespace}.subfolder.v1.file.xsd and replace v1 with v2 if I want to load v2. However, my resources are being embedded without any reference to namespace or folder structure. When I call GetManifestResourceNames() it tells me that there is only one file
file.xsd
If I change v2/file.xsd -> v2/file2.xsd output for GetManifestResourceNames is
file.xsd
file2.xsd
What I expected the names would be
{Namespace}.v1.file.xsd
{Namespace}.v2.file.xsd
Project is part of a much larger solution that may be causing this in its build process. I can't reproduce the behaviour with an isolated project. But I am not sure where to start looking.

Swashbuckle custom asset not found

I added to SwaggerConfig.cs this string
c.CustomAsset("index", thisAssembly, "Table.Web.CustomContent.index.html");
...than I run the application, go to swagger docs and get error:
An error has occurred.
Embedded resource not found - Table.Web.CustomContent.index.html
Swashbuckle.SwaggerUi.AssetNotFound
The build action property of the index.html was set to embedded resource
What should I do to fix it?
I struggled with this for a while today in a ASP.NET C# project and finally resolved it by cobbling together a few different resources.
First (as noted in the Swagger comments), the item must be marked as an Embedded Resource by right-clicking the item in the solution explorer and going to Properties, and selecting Embedded Resource from the Build Action dropdown.
Second, the logical name can be tricky to identify. In my case, a dash in a directory name was being converted to an underscore once embedded, leading to lots of hair-pulling (hair_pulling?). The easiest way to get the true logical path is to get it from the Build Output window.
Go to Tools => Options
Expand the Projects and Solutions sidebar item and click Build and Run
Set the MSBuild Output to Detailed.
Clean the solution and rebuild, opening the Output window if necessary.
The output log should unambiguously state the true logical name of the file with a line like...
Resource file 'swagger-ui\SwaggerUiStyle.css' gets manifest resource name 'MySolutionName.swagger_ui.SwaggerUiStyle.css'
(credit to #bkwdesign for his excellent explanation on this part)

Generate URL of resources that are handled by Grails AssetPipeline

I need to access a local JSON file. Since Grails 2.4 implements the AssetPipeline plugin by default, I saved my local JSON file at:
/grails-app/assets/javascript/vendor/me/json/local.json
Now what I need is to generate a URL to this JSON file, to be used as a function parameter on my JavaScript's $.getJSON() . I've tried using:
var URL.local = ""${ raw(asset.assetPath(src: "local.json")) }";
but it generates an invalid link:
console.log(URL.local);
// prints /project/assets/local.json
// instead of /project/assets/vendor/me/json/local.json
I also encountered the same scenario with images that are handled by AssetPipeline1.9.9— that are supposed to be inserted dynamically on the page. How can I generate the URL pointing this resource? I know, I can always provide a static String for the URL, but it seems there would be a more proper solution.
EDIT
I was asked if I could move the local JSON file directly under the assets/javascript root directory instead of placing it under a subdirectory to for an easier solution. I prefer not to, for organization purposes.
Have you tried asset.assetPath(src: "/me/json/local.json")
The assets plugin looks in all of the immediate children of assets/. Your local.json file would need to be placed in /project/assets/foo/ for your current code to pick it up.
Check out the relevant documentation here which contains an example.
The first level deep within the assets folder is simply used for organization purposes and can contain folders of any name you wish. File types also don't need to be in any specific folder. These folders are omitted from the URL mappings and relative path calculations.

HttpContext.GetGlobalResourceObject returns null but the value is available through Resources

I'm trying to access values from a resx resource file using HttpContext.GetGlobalResourceObject.
The resource file is in a folder called Resources and has the CustomTool property set to PublicResXFileCodeGenerator as per K. Scott Allen: http://odetocode.com/blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx
The resources are being built and embedded (I can see then in the assembly via ILSpy) and I can access the values in code using:
var b = MyApp.Resources.GlobalStrings.VendorUITitle;
But trying to access them via GetGlobalResourceObject always returns null
var s = HttpContext.GetGlobalResourceObject("MyApp.Resources.GlobalStrings", "VendorUITitle");
I was able to solve this problem by setting the Custom Tool of my resource file to be GlobalResourceProxyGenerator
And the Build Action to Content and Copy to Output Directory property to Copy Always
If I didn't do this then when I published to production calls to HttpContext.GetGlobalResourceObject would always return null.
Create resource file under App_GlobalResources
Set Build Action to Content
Set Copy to Output Directory to Do not copy
Ensure you have App_GlobalResources folder in deployed aplication

How do I publish/deploy an MVC Project with ResX Resources?

I have written a VERY simple MVC application which just displays a single string from a Resource file. It works just fine on my local machine but when I deploy the project to the web server I get the error.
CS0103: The name 'Resources' does not
exist in the current context
You can very easily replicate exactly what I am doing in just 10 steps!
Create a New MVC 2 Web Application.
(File->New->Project->ASP.NET MVC 2 Web Application, say no to the Unit Testing Project)
Add the "App_GlobalResources" folder.(right click the project and select Add->Add ASP.NET Folder->App_GlobalResources)
Add a Resource File to this folder.(right click the folder and select Add->New Item...->Resources File. Name it Strings.resx)
Add a single string to the Resource table.(Name = "HelloWorld", Value = "I localized Hello World!")
Set the File Properties for the Resource File.(Click the file Strings.resx and int the Properties window set Build Action = "Embedded Resource" and the CustomTool = "PublicResXFileCodeGenerator")
Add a new Controller(Right click the Controllers folder and select Add->Controller... Name it HelloWorldController.cs)
Add a the View(With the cursor in the Index method of the HelloWorldController.cs Press CTRL-M-V. Use the default values including View name = "Index")
Modify the View so that it displays our string from the resource file.Replace the content of the MainContent placeholder with
<h2><%: Resources.Strings.HelloWorld %></h2>
Run it locally to test that it works. Which it should.
Publish it to a web server and visit the url "http://localhost/HelloWorld"
This is where I see the error described at the top.
I would imagine that the settings I've put on the ResX file are incorrect and the resource is not published to the server.
Help is greatly appreciated.
Thanks!
Ah ha! Figured it out. In LARGE part to this article:
http://odetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx
Sounds like the App_GlobalResources folder is NOT cooperative with MVC. So I moved my ResX file to a new folder~/Resources/Strings/Strings.resx
This along with 1 minor change to set the File Property
Custom Tool Namespace = Resources
and Problem Solved!

Resources