我目前正在开发一个需要静态文本可配置的项目(包括标签、消息和验证错误)。我想知道最好的方法是什么。该应用程序是使用 ASP.NET MVC 和 C# 3.5 编写的。我需要将所有这些静态可配置文本从不同的项目提供给 MVC 项目。我考虑使用全局资源,或者使用在应用程序启动时加载的 XML 文件。顺便说一句,这与本地化无关,最终用户也无法配置静态文本。
I am currently working on a project that requires Static Text to be configurable(including labels, messages, and validation errors). I was wondering what the best approach would be. The app is being written using ASP.NET MVC and C# 3.5. I need all this static configurable text to be fed to the MVC project from a different project. I thought of using Global Resources, or using an XML file that would be loaded on application start. By the way, this is not about localization, also static text won't be configurable by the end-user.
发布评论
评论(5)
正如詹姆斯回答的那样,您可以使用 AppSettings 和 web.config 。您还可以将其存储在数据库中,采用键值对结构。
但您还需要将其从配置项目获取到 ASP.Net MVC 项目。我会这样做:
You could use AppSettings and web.config as James answered. you could also store it in a database, with key value pair structure.
But you also need to get it from the configuration project to the ASP.Net MVC project. I would do it like this:
我可能会制作一个单独的项目,其中包含一系列 Resx 文件,这些文件非常容易使用,并且免费为您提供本地化。这是我要开始的地方,如果您需要通过某些管理工具即时编辑它们,那么您可以使用类似 Rick Strahl 的数据驱动提供商。这可能是比尝试提出自己的数据库驱动设计更好的方法。
我确实同意您可能需要澄清一下文本可编辑的方式和时间。
绝对远离 web.config 和 appsettings,除非我们只讨论 1 或 2 行文本。一般来说,这不是一个好主意,因为其他人已经说过有关应用程序重新启动和一般配置膨胀的许多原因。
I would probably make a separate project that contained a series of Resx files, these are pretty easy to work with and give you localization for free. This is where I would start and if you need to edit them on the fly through some admin tool then you could use something like Rick Strahl's data driven provider. This is probably a better approach then trying to come up with your own DB driven design.
I do agree that you probably need to clarify a bit of how and when the text will be editable.
Definitely stay away from the web.config and appsettings unless we are only talking about 1 or 2 lines of text. In general this is not a good idea for many of the reasons others have stated about app restarts and just general config bloat.
我会将其存储在 Web.Config 文件的 AppSettings 部分中。
I would store it in the AppSettings section in the Web.Config file.
本地化实际上是处理这个问题的一个不错的方法——它解决了同样的问题,你只需要提供一个单一的语言文件。缺点是本地化位不一定易于最终用户编辑。这让我意识到,这里要回答的真正问题是“用户如何编辑这些信息?”如果答案是“频繁且轻松”,那么您可能需要在数据库中创建某种 UI Snippets 表并进行相应的处理。另一个不错的选择是使用自定义配置部分并使用配置 API 对其进行读/写。如果需要,还可以保留手动编辑 XML 文件的权限。
Localization is actually a decent way to handle this--it is solving the same problem, you'd just need to provide a single language file. Downside is that localization bits are not necessarily easily end-user editable. Which drives me to the fact that the real question to answer here is "how user editable is this information going to be?" If the answer is "frequently and easily" then you might want to make some sort of UI Snippets table in your database and handle it accordingly. Another decent option would be to use a custom configuration section and read/write to it using the configuration API. Also leaves open hand-editing XML files if need be.
我会在应用程序启动时使用带有单次加载的 XML 文件
I would use a XML file with a single load at the application startup