将内部版本号放在web.config中

时间:2010-10-12 15:34:38

标签: c# asp.net asp.net-mvc

我正在尝试将我的内部版本编号放入ASP.Net MVC项目的web.config中(很长一段时间,但 是一个原因)......就像这样:

<appSettings>
    <add key="version" value="1.0.3936.27150" />
</appSettings>

现在,我正在使用可执行文件在post-build事件上执行它,方法是查看程序集以获取版本和内容到web.config中。它有效,但并不完全是我想要的。

任何简单方法都可以更轻松/更清洁吗?感谢您的任何意见/建议。

2 个答案:

答案 0 :(得分:0)

你有几个选择,第一个是你选择的那个,这是旧的歪斜。下一个选项是使用Visual Studio Macro ...这只适用于Visual Studio以及配置它的系统。接下来的两个选项将是最好的。使用MSBuild脚本作为后期构建事件...或使用NAnt脚本作为后期构建事件来生成mod。我会倾向于支持我自己的NAnt,但后来我在NAnt有一条黑带。 : - )

答案 1 :(得分:0)

在部署项目之前,我还必须将内部版本号放在web.config中。我在这里按照教程http://www.ewaldhofman.nl/post/2010/05/13/Customize-Team-Build-2010-e28093-Part-5-Increase-AssemblyVersion.aspx

由于上面的教程没有告诉我如何修改web.config,我不得不修改自定义活动(下面的代码):

 [BuildActivity(HostEnvironmentOption.All)]
public sealed class UpdateWebConfigWithBuild : CodeActivity {

    // Define the activity arguments of type string
    [RequiredArgument]
    public InArgument<IBuildDetail> BuildDetail { get; set; }


    // The SourcesDirectory as initialized in the Build Process Template
    [RequiredArgument]
    public InArgument<string> SourcesDirectory { get; set; }

    // The SourcesDirectory as initialized in the Build Process Template
    [RequiredArgument]
    public InArgument<string> TFSBuildNumberAppSettingName { get; set; }


    public OutArgument<string> TextOut { get; set; }

    // If your activity returns a value, derive from CodeActivity<TResult>
    // and return the value from the Execute method.
    protected override void Execute(CodeActivityContext context) {
        // Obtain the runtime value of the Text input argument
        // Contains the build number
        IBuildDetail buildDetail = context.GetValue(this.BuildDetail);
        // The TFS directoty
        string sourcesDirectory = context.GetValue(this.SourcesDirectory);
        // web config app setting
        string TFSBuildNumberSetting = context.GetValue(this.TFSBuildNumberAppSettingName);

        // output String
        string output = String.Empty;



         // Get all AssemblyInfo files
        foreach (string file in Directory.EnumerateFiles(sourcesDirectory,"web.config", SearchOption.AllDirectories)) {
             // IS tag founds
            bool settingFound = false;

            FileAttributes attributes = File.GetAttributes(file);

            // Remove readonly attribute
            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) {
                //Remove readonly
                File.SetAttributes(file,attributes^FileAttributes.ReadOnly);
            }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(file);
            // iterate through all the app settings to find the TFS BUILD 
            foreach (XmlNode node in xmlDoc.SelectNodes("/configuration/appSettings/add")) {
                // MATCH
                if (node.Attributes.GetNamedItem("key") != null && node.Attributes.GetNamedItem("value") != null && node.Attributes.GetNamedItem("key").Value.Equals("TFSBuildNumber")) {
                    node.Attributes.GetNamedItem("value").Value = buildDetail.BuildNumber;
                    settingFound = true;
                    output += "MODIFIED with BuildNumber " + buildDetail.BuildNumber.ToString() + " :" + file + "\n";
                    break;
                }
            }
            // if the app setting is not found then add it
            if (!settingFound) {
                XmlNode node = xmlDoc.SelectSingleNode("/configuration/appSettings");
                if (null != node) {
                    XmlElement elem = xmlDoc.CreateElement("add");
                    XmlAttribute attrKey = xmlDoc.CreateAttribute("key");
                    attrKey.Value = "TFSBuildNumber";
                    XmlAttribute attrVal = xmlDoc.CreateAttribute("value");
                    attrVal.Value = buildDetail.BuildNumber;
                    elem.Attributes.Append(attrKey);
                    elem.Attributes.Append(attrVal);
                    node.AppendChild(elem);
                    output += "ADDED with BuildNumber " + buildDetail.BuildNumber.ToString() + " :" + file + "\n";
                }
            }
            // Save file
            xmlDoc.Save(file);

            //add readonly attribute back to web.config file
            File.SetAttributes(file,attributes | FileAttributes.ReadOnly);
        }
        // Set the values
        context.SetValue<string>(this.TextOut,output);
    }