Can't read from appSettings

孤街醉人 提交于 2019-12-11 05:47:38

问题


I have a test project with a web.config file in the test project. I keep getting null when I try to read a value from the appSettings section of the web.config.

I have a reference to System.configuration and a using statement using System.Configuration;

    <appSettings>
<add key="SatisfactionSurveyLink" value="Link"/>
    </appSettings>


        [TestMethod()]
        public void TestReadFromAppSettings() 
        {
            String surveyLink = ConfigurationManager.AppSettings["SatisfactionSurveyLink"];

            Assert.IsNotNull(surveyLink);
        }

I've tried many different things and can't get this to work. Does anyone have any suggestions as to what the problem could be?

thanks,

Ronald


回答1:


if it is a test project created as class library, does not need the web.config but the app.config ;-)




回答2:


Does anyone have any suggestions as to what the problem could be

Have you double-checked you've not mistyped the key, either in the web.config itelf, or when you read it out?

Is it possible something is updating the value before you read it?

Is the build action of the web.config set as "Content" in the properties window?




回答3:


You said you have a test project, you should have an app.config then



来源:https://stackoverflow.com/questions/6959024/cant-read-from-appsettings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!