How does the App_LocalResources work with MVC?(App_LocalResources 如何与 MVC 一起工作?)
问题描述
我一直在研究我的第一个 MVC 应用程序,我想对它进行本地化.我知道如何使用 App_GlobalResources,但我更喜欢使用 App_LocalResources.
I've been working through my first MVC application, and I'd like to localize it. I know how to use the App_GlobalResources but I'd prefer to use the App_LocalResources.
在典型的 WebForms 应用程序中,您将 App_LocalResources 文件夹与 ASPX 文件放在同一目录中……这在 MVC 中是否相同(IE:我是否将它们放在views"目录中的相应子文件夹中)?
In a typical WebForms application, you put the App_LocalResources folder in the same directory as the ASPX file... is this the same in MVC (IE: do I put them in the respective subfolders in the "views" directory)?
另外,在您的视图中访问 App_GlobalResources 时,您可以这样做 <%=Html.Encode(Resources.Index.Title)%>
但是我无法使用 App_LocalResources.我不想让我的 <label runat="server"
来利用 meta:resourcekey
Also, when accessing the App_GlobalResources in your view you can do it like so <%=Html.Encode(Resources.Index.Title)%>
however I cannot do that with the App_LocalResources. I don't want to have to make my <label runat="server"
in order to take advantage of the meta:resourcekey
我发现了一种方法来做到这一点,但我不确定这是否是最好的方法.思路是这样的
I discovered ONE method to do this, but I'm not sure if this is the best method. Here's the idea
- 您将
App_LocalResources
文件夹作为子文件夹放在各自的 View 文件夹中 - 您创建与您的视图相对应的 resx 文件... IE:
Index.resx
或About.resx
- 您将 Resx 文件属性设置为Public"(PublicResXFileCodeGenerator)、Embeded Resource"、Views.[viewname]"命名空间
- 创建一个名为Title"的新资源名称,并将您想要的任何内容放入 Value 中.
- You put the
App_LocalResources
folder as a sub folder within your respective View folder - You create resx files that correspond to your Views... IE:
Index.resx
orAbout.resx
- You set the Resx files properties to be "Public" (PublicResXFileCodeGenerator), "Embeded Resource", "Views.[viewname]" namespace
- Create a new resource name called "Title" and put whatever you want in the Value.
现在我可以使用类似这样的方式引用本地资源
Now I can reference the Local Resources using something like this
<%= MyProj.Views.Home.Index.Title%>
我看到的唯一问题是资源是嵌入的,如果不重新编译项目,我无法添加新的语言环境.
The only problem I see with this is the fact that the resources are embedded and I cannot add new locales without re-compiling the project.
这是我对这种方法的参考.
推荐答案
不应将 App_GlobalResources 或 App_LocalResources 与 MVC 一起使用.这是因为这些资源的工作方式.这些资源没有嵌入到您的项目中,而是由 ASP.NET 运行时编译.这意味着当不在 ASP.NET 运行时中运行时,您的资源将不可用,这意味着如果您使用其中的资源,您将失去对视图和控制器进行单元测试的能力.
You should not use App_GlobalResources or App_LocalResources with MVC. That is because of the way these resources work. These resources are not embedded in your project, but are instead compiled by the ASP.NET runtime. This means that your resources will not be available when not running inside the ASP.NET runtime, which means you would lose the ability to unit test your views and controllers if you use resources in them.
这篇文章提供了更多关于它的细节.
This post gives more details about it.
相反,您应该使用您在问题中提到的方法.将您的资源放在您想要的任何位置,并将其设置为嵌入式资源",自定义工具应为PublicResXFileCodeGenerator".
Instead, you should use the approach you mention yourself in your question. Put your resource whereever you want, and set it to 'Embedded resource' and the custom tool should be 'PublicResXFileCodeGenerator'.
这篇关于App_LocalResources 如何与 MVC 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:App_LocalResources 如何与 MVC 一起工作?
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01