为什么我不能在公共静态字符串上使用连接

Car*_*omp 3 c# string static concatenation

仅供参考我花了大约30分钟寻找答案.如果我错过了stackoverflow,我很抱歉.这似乎是一个简单的答案,但我的同事都不知道.

我正在使用现有的库.我正在尝试保持与当前系统的集成,同时添加了更改某些硬编码值的功能.我重构了代码以使用ConfigurationManager,因此我可以使用参数化的Web部署.

我的问题是这个..为什么,当我访问Constants.CourseMillRegisterURL时,我只回到变量的一部分?我得到的部分是从web.config读取的部分.我希望得到一个包含两个变量的完整URL,但我只得到我的web.config值"userlogin.jsp".

我也尝试过对它进行编码,以便在私有中将值连接起来,但它也不会那样工作.我真的希望保持静态,因为整个库使用代码来引用这个类

string theUrl = Constants.CoursMillUrl + Constants.CourseMillRegisterUrl
Run Code Online (Sandbox Code Playgroud)

每个变量返回以下内容:

为什么我的价值观没有

我的代码如下.

namespace STTI.CourseMill.Library
{
    #region

    using System.Configuration;

    #endregion

    public static class Constants
    {
        // prod 

        #region Static Fields

        public static string CourseMillRegisterURL = CourseMillURL + courseMillRegisterURL;

        public static string CourseMillURL = courseMillURL;

        public static string CourseMillUserLoginURL = CourseMillURL + courseMillUserLoginURL;

        #endregion

        #region Properties

        private static string courseMillRegisterURL
        {
            get
            {
                string output = ConfigurationManager.AppSettings["CourseMillRegisterUrl"];
                if (output == null)
                {
                    output = "sttilogin.jsp?d=t";
                }

                return output;
            }
        }

        private static string courseMillURL
        {
            get
            {
                string output = ConfigurationManager.AppSettings["CourseMillURL"];
                if (output == null)
                {
                    output = "http://hardcodedvalue/cm6/cm0670";
                }

                if (!output.EndsWith("/"))
                {
                    output += "/";
                }

                return output;
            }
        }

        private static string courseMillUserLoginURL
        {
            get
            {
                string output = ConfigurationManager.AppSettings["CourseMillLoginUrl"];
                if (output == null)
                {
                    output = "sttilogin.jsp?d=t";
                }

                return output;
            }
        }

        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

Bat*_*eba 8

静态字符串按它们在文件中出现的顺序初始化.

courseMillRegisterURLCourseMillRegisterURL例如,在之后初始化.

这就是你的字符串不完整的原因.