<i id='XTNkS'><tr id='XTNkS'><dt id='XTNkS'><q id='XTNkS'><span id='XTNkS'><b id='XTNkS'><form id='XTNkS'><ins id='XTNkS'></ins><ul id='XTNkS'></ul><sub id='XTNkS'></sub></form><legend id='XTNkS'></legend><bdo id='XTNkS'><pre id='XTNkS'><center id='XTNkS'></center></pre></bdo></b><th id='XTNkS'></th></span></q></dt></tr></i><div id='XTNkS'><tfoot id='XTNkS'></tfoot><dl id='XTNkS'><fieldset id='XTNkS'></fieldset></dl></div>
    • <bdo id='XTNkS'></bdo><ul id='XTNkS'></ul>
    1. <tfoot id='XTNkS'></tfoot>
        <legend id='XTNkS'><style id='XTNkS'><dir id='XTNkS'><q id='XTNkS'></q></dir></style></legend>

      1. <small id='XTNkS'></small><noframes id='XTNkS'>

        App_LocalResources 如何与 MVC 一起工作?

        时间:2023-08-25
        1. <small id='vDjXh'></small><noframes id='vDjXh'>

            <tbody id='vDjXh'></tbody>
          <i id='vDjXh'><tr id='vDjXh'><dt id='vDjXh'><q id='vDjXh'><span id='vDjXh'><b id='vDjXh'><form id='vDjXh'><ins id='vDjXh'></ins><ul id='vDjXh'></ul><sub id='vDjXh'></sub></form><legend id='vDjXh'></legend><bdo id='vDjXh'><pre id='vDjXh'><center id='vDjXh'></center></pre></bdo></b><th id='vDjXh'></th></span></q></dt></tr></i><div id='vDjXh'><tfoot id='vDjXh'></tfoot><dl id='vDjXh'><fieldset id='vDjXh'></fieldset></dl></div>

            <bdo id='vDjXh'></bdo><ul id='vDjXh'></ul>
              • <legend id='vDjXh'><style id='vDjXh'><dir id='vDjXh'><q id='vDjXh'></q></dir></style></legend><tfoot id='vDjXh'></tfoot>

                  本文介绍了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

                  1. 您将 App_LocalResources 文件夹作为子文件夹放在各自的 View 文件夹中
                  2. 您创建与您的视图相对应的 resx 文件... IE:Index.resxAbout.resx
                  3. 您将 Resx 文件属性设置为Public"(PublicResXFileCodeGenerator)、Embeded Resource"、Views.[viewname]"命名空间
                  4. 创建一个名为Title"的新资源名称,并将您想要的任何内容放入 Value 中.
                  1. You put the App_LocalResources folder as a sub folder within your respective View folder
                  2. You create resx files that correspond to your Views... IE: Index.resx or About.resx
                  3. You set the Resx files properties to be "Public" (PublicResXFileCodeGenerator), "Embeded Resource", "Views.[viewname]" namespace
                  4. 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 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 WPF/XAML 中显示本地时区的时间 下一篇:如何在 Windows Phone 上更改 C# 中的本地化?

                  相关文章

                  最新文章

                  <small id='KsWq3'></small><noframes id='KsWq3'>

                  <legend id='KsWq3'><style id='KsWq3'><dir id='KsWq3'><q id='KsWq3'></q></dir></style></legend>

                    <i id='KsWq3'><tr id='KsWq3'><dt id='KsWq3'><q id='KsWq3'><span id='KsWq3'><b id='KsWq3'><form id='KsWq3'><ins id='KsWq3'></ins><ul id='KsWq3'></ul><sub id='KsWq3'></sub></form><legend id='KsWq3'></legend><bdo id='KsWq3'><pre id='KsWq3'><center id='KsWq3'></center></pre></bdo></b><th id='KsWq3'></th></span></q></dt></tr></i><div id='KsWq3'><tfoot id='KsWq3'></tfoot><dl id='KsWq3'><fieldset id='KsWq3'></fieldset></dl></div>

                    <tfoot id='KsWq3'></tfoot>
                    • <bdo id='KsWq3'></bdo><ul id='KsWq3'></ul>