How to use compiled global resources in ASP.NET MVC

冷暖自知 提交于 2019-11-29 06:45:26

问题


I want to compile App_GlobalResources/Strings.resx into my assembly (and eventually use satellite assemblies for Strings.es.resx, Strings.fr.resx, etc.) but the following error occurs once the app is published:

"Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified."

Steps to Reproduce:

  1. Create a new ASP.NET MVC project.
  2. Add an App_GlobalResources folder and a Strings.resx file.
  3. Set the file's build action to 'Embedded Resource'
  4. Add a string to Strings.resx and use it in HomeController.Index(), e.g. ViewData["Message"] = Strings.MyTest
  5. F5 to debug, works fine.
  6. Publish to IIS and (since Strings.resx is excluded from publishing) you will get the above error in the published site.

Can ASP.NET MVC projects use string resources compiled into the dll and if so, what am I doing wrong?


Update: Here is what I see in Reflector:

So why is the ResourceManager not finding them? Isn't it supposed to fallback to the main assembly by default?


回答1:


Here's a good starting point http://odetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx

I setup a project just like you mentioned and got precisely the same error when I deployed to IIS6. Changing Custom Tool to PublicResXFileCodeGenerator and Custom Tool Namespace to Resources per Scott's suggestions cleared it right up.

I'd also follows Scott's lead with respect to eliminating App_GlobalResources but I got it to work just fine. That might be a matter of personal preference.




回答2:


Probably there is one more thing one should know about the PublicResXFileCodeGenerator - it does not work with files that has more then one dot in the file name (see http://social.msdn.microsoft.com/Forums/ru-RU/9412f049-acd8-4a77-b73a-f3810df18376/naming-resources-files-issue-aspnet-mvc-3?forum=dlr). So the codebehind for the Strings.es.resx, Strings.fr.resx, etc. remains empty. The easy solution is to rename the files.



来源:https://stackoverflow.com/questions/1848955/how-to-use-compiled-global-resources-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!