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

 Qualcommtjmag_716 发布于 2023-02-09 19:33

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

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

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

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

string theUrl = Constants.CoursMillUrl + Constants.CourseMillRegisterUrl

每个变量返回以下内容:

Constants.CourseMillUrl =" http://www.valuefromwebconfig.com/cm6/cm0670 "

Constants.CourseMillRegisterUrl ="valuefromwebconfig.jsp"

Constants.CourseMillLoginUrl ="othervaluefromwebconfig.jsp"

为什么我的价值观没有

Constants.CourseMillUrl =" http://www.valuefromwebconfig.com/cm6/cm0670 "

Constants.CourseMillRegisterUrl ="" http://www.valuefromwebconfig.com/cm6/cm0670/valuefromwebconfig.jsp "

Constants.CourseMillLoginUrl =" http://www.valuefromwebconfig.com/cm6/cm0670/othervaluefromwebconfig.jsp "

我的代码如下.

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
    }
}

Bathsheba.. 8

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

courseMillRegisterURLCourseMillRegisterURL例如,在之后初始化.

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

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

    courseMillRegisterURLCourseMillRegisterURL例如,在之后初始化.

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

    2023-02-09 19:35 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有