如何在 ASP.NET MVC 中使用已编译的全局资源 [英] How to use compiled global resources in ASP.NET MVC

查看:17
本文介绍了如何在 ASP.NET MVC 中使用已编译的全局资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 App_GlobalResources/Strings.resx 编译到我的程序集中(并最终将附属程序集用于 Strings.es.resx、Strings.fr.resx 等.) 但应用发布后会出现以下错误:

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:

无法加载文件或程序集‘App_GlobalResources’或其依赖项之一.系统找不到指定的文件."

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

复制步骤:

  1. 创建一个新的 ASP.NET MVC 项目.
  2. 添加一个 App_GlobalResources 文件夹和一个 Strings.resx 文件.
  3. 将文件的构建操作设置为嵌入式资源"
  4. 将字符串添加到 Strings.resx 并在 HomeController.Index() 中使用它,例如ViewData["Message"] = Strings.MyTest
  5. F5 调试,工作正常.
  6. 发布到 IIS 并且(因为 Strings.resx 被排除在发布之外)您将在发布的站点中收到上述错误.
  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.

ASP.NET MVC 项目可以使用编译到 dll 中的字符串资源吗?如果可以,我做错了什么?

更新:这是我在 Reflector 中看到的:

Update: Here is what I see in Reflector:

那么为什么 ResourceManager 没有找到他们?难道它不应该回退到主程序集 默认情况下?

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

推荐答案

这是一个很好的起点 http://odetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx

我像您提到的那样设置了一个项目,并且在部署到 IIS6 时遇到了完全相同的错误.根据 Scott 的建议,将 Custom Tool 更改为 PublicResXFileCodeGenerator 并将 Custom Tool Namespace 更改为 Resources 即可解决问题.

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.

在消除 App_GlobalResources 方面,我也会效仿 Scott 的做法,但我让它工作得很好.这可能是个人喜好问题.

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.

这篇关于如何在 ASP.NET MVC 中使用已编译的全局资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆