【问题标题】:Best way to set-up a config file to store parameters in asp net mvc 4设置配置文件以在 asp net mvc 4 中存储参数的最佳方法
【发布时间】:2013-05-24 04:20:20
【问题描述】:

我正在启动 Asp Net MVC4 项目,由于我来自 php 开发,我很好奇设置配置文件以存储我可能需要的系统名称、版本等参数的最佳方式是什么.

是否有任何指导方针可以做到这一点,例如使用配置文件或数据库查询..?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 configuration-files


    【解决方案1】:

    我在这类事情上广泛使用 web.config。

    例子

    <appSettings>
        ....
        <add key="SearchUserResultMaxCount" value="100" />
    </appSettings>
    

    代码

    public static class SearchHelper
    {
        /// <summary>
        /// Gets the max records for resultset from db, configurable in web.config
        /// </summary>
        /// <returns></returns>
        public static int SearchEntityResultMaxCount()
        {
            int count;
            Int32.TryParse(ConfigurationManager.AppSettings["SearchEntityResultMaxCount"], out count);
            return count;
        }
    }
    

    【讨论】:

    • 谢谢,但这些值需要输入到共享模板中。我没有通过控制器...我该如何继续?我必须创建视图并将它们调用到模板中吗?
    • 此代码也不在我的控制器中。您可以简单地编写一个类,其中每个属性从配置中加载它的默认值。这正是这个助手为我做的事情。我现在可以从任何地方参考SearchHelper.SearchEntityResultMaxCount。在您的情况下,只需将其设为属性而不是方法即可。
    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多