如何使用ResX资源发布/部署MVC项目? [英] How do I publish/deploy an MVC Project with ResX Resources?

查看:219
本文介绍了如何使用ResX资源发布/部署MVC项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个非常简单的MVC应用程序,它只显示一个资源文件中的一个字符串。它在本地机器上工作得很好,但是当我将项目部署到Web服务器时,我收到错误。


CS0103:资源'不
存在于当前上下文


你可以非常容易地复制我正在做的只是10步


  1. 创建一个新的MVC 2 Web应用程序。

    文件 - >新建 - >项目 - > ASP.NET MVC 2 Web应用程序,对单元测试项目否定)


  2. 添加App_GlobalResources 文件夹。
    (右键单击该项目并选择Add-> Add ASP.NET Folder-> App_GlobalResources)


  3. 将资源文件添加到此文件夹
    (右键单击该文件夹,然后选择Add-> New Item ...-> Resources File。将其命名为Strings.resx)


  4. 将一个字符串添加到资源表。
    (Name =HelloWorld,Value =I localized Hello World!)



  5. (点击文件Strings.resx,然后在属性窗口中设置Build Action =Embedded Resource,CustomTool = PublicResXFileCodeGenerator)


  6. 添加新的Controller
    (右键单击Controllers文件夹,然后选择Add-> Controller。 ..命名为HelloWorldController.cs)


  7. 添加视图
    (将光标放在Index方法中HelloWorldController.cs按CTRL-MV。使用默认值,包括View name =Index)


  8. 修改视图,以便从资源文件显示我们的字符串。 >
    使用

     < h2><%:Resources.Strings.HelloWorld替换MainContent占位符的内容%GT;< / H2> 


  9. 在本地运行以测试它是否有效。应该。


  10. 发布到Web服务器并访问url http:// localhost / HelloWorld


看到顶部描述的错误。



我可以想象,我把这个设置放在ResX文件上是不正确的,资源没有被发布到服务器上。 / p>

帮助非常感谢。



谢谢!

解决方案

啊哈!弄清楚了。在这篇文章的大部分内容中:

http://codetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx



听起来像App_GlobalResources文件夹与MVC不协调。所以我把我的ResX文件移动到一个新的文件夹
〜/ Resources / Strings /Strings.resx



更改以设置文件属性

自定义工具命名空间 =资源



和解决问题!


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!

  1. Create a New MVC 2 Web Application.
    (File->New->Project->ASP.NET MVC 2 Web Application, say no to the Unit Testing Project)

  2. Add the "App_GlobalResources" folder.
    (right click the project and select Add->Add ASP.NET Folder->App_GlobalResources)

  3. Add a Resource File to this folder.
    (right click the folder and select Add->New Item...->Resources File. Name it Strings.resx)

  4. Add a single string to the Resource table.
    (Name = "HelloWorld", Value = "I localized Hello World!")

  5. 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")

  6. Add a new Controller
    (Right click the Controllers folder and select Add->Controller... Name it HelloWorldController.cs)

  7. 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")

  8. 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>
    

  9. Run it locally to test that it works. Which it should.

  10. 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!

这篇关于如何使用ResX资源发布/部署MVC项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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