.net – 使用Wix编辑Web.Config连接字符串设置

我目前正在尝试修改我的Wix(V3.5)安装程序,以编辑我想要安装的.NET应用程序的Web.config设置.这对于普通的ASP.NET应用程序来说很好,但现在我试图将我的Wix设置项目应用到Entity Framework .NET应用程序,您可能知道它有一个更复杂的连接字符串设置与模型.csdl和.ssdl设置.

因此,如果我的web.config连接字符串设置看起来像这样:(其中[DBSERVER]& [DBNAME]是从对话框中恢复的属性)

<connectionStrings>
   <add name="SSITacticalSolutionEntities" connectionString="metadata=res://*/Model.TacticalSolutionModel.csdl|res://*/Model.TacticalSolutionModel.ssdl|res://*/Model.TacticalSolutionModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=sd-sql2008r2;Initial Catalog=SsiTacticalSolution1.2.4;Integrated Security=True;MultipleActiveResultSets=True&quot; />
  </connectionStrings>

我在我的Product.Wsx文件中编辑我的Web.config,如下所示:

<util:XmlFile Id="ModifyConnectionString" Action="setValue" Permanent="yes" File="[INSTALLLOCATION]Web.config"
                  ElementPath="/configuration/connectionStrings/add[\[]@name='!(loc.EntityName)'[\]]" Name="connectionString"
                  Value="Data Source=[DBSERVER];Initial Catalog=[DBNAME];Integrated Security=true;providerName=System.Data.EntityClient;MultipleActiveResultSets=True&quot;"   Sequence="5"/>

我得到一个像这样的连接字符串:

<connectionStrings>
      <add name="SSITacticalSolutionEntities" connectionString="Data Source=sd-sql2008r2;Initial Catalog=SsiTacticalSolution1.2.4;Integrated Security=true;providerName=System.Data.EntityClient;MultipleActiveResultSets=True&quot;"/>
  </connectionStrings>

这当然是有道理的,因为我要求它用我在值中定义的内容替换当前的连接字符串属性.

但我真正需要的是编辑我的连接字符串的特定部分并留下余数(我可以在这里使用某种替换操作),即.保留我的所有模型设置,并根据需要更换数据库服务器和名称等.我以前使用Visual Studio安装程序执行此操作没有问题,并且它非常易于使用.

所以我的问题是可以使用util.XMLFile或者util:XmlConfig来完成吗?我没有运气就试过了.

或者这不可能与util.XMLFile一起使用,我是否必须在CustomAction中执行此操作?
任何想法都会有很大的帮助,在此先感谢…

解决方法

我最终得到了这个工作,最后我没有为这个特定设置使用自定义操作,我使用在我的本地化文件中设置的变量.

我这样做是因为它会是开发人员,而不是那个知道模型名称和实体名称的用户(不是通过安装对话框的用户,他们不会知道这个信息),所以我有一个具有不同属性的本地化文件它像产品名称等,所以我添加了一个模型名称,并为此命名.我从用户输入的对话框中获得的所有其他内容:即数据库名称,虚拟目录,模拟用户等…

如果它对任何人有帮助,这就是我最终为我的web.config提出的内容;这是我的product.wxs中处理此问题的部分.正如您所看到的,我在顶部有一个连接字符串属性,并在我的本地化文件中设置了loc.ModelName的占位符:

<Property Id="CONNECTION_STRING"
  Value="metadata=res://*/Model.!(loc.ModelName).csdl|res://*/Model.!(loc.ModelName).ssdl|res://*/Model.!(loc.ModelName).msl;provider=System.Data.SqlClient;provider connection string=&quot;"/>

<!-- The root of the installer. -->
<Directory Id='TARGETDIR' Name='SourceDir'>

  <!-- Install into the inetpub/wwwroot directory -->
  <Directory Id="IISMain" Name='inetpub'>
    <Directory Id="WWWMain" Name='wwwroot' ComponentGuidGenerationSeed='C38ED13E-E1E3-40DB-B1FA-39400C6B2BC4'>


      <Directory Id='INSTALLLOCATION' Name="!(loc.ProductName)">

        <!-- The component to define the Virtual Directory.-->
        <Component Id="WebVirtualDirComponent"
                   Guid="D814F88F-6E0C-4365-A411-2F9807522C3D">

          <!-- WebVirtualDir: The virtual directory we are installing. -->
          <!-- Alias:         Alias attribute is the name that we will see in IIS.-->
          <!-- Directory:     The Directory attribute is the "Physical Path" property in
                            IIS and needs to tie to the ID specified above as the install location. -->
          <!-- WebSite:       The WebSite attribute ties to a <WebSite> element in the 
                            setup file(see below). As this is an example of installing into the 
                            "Default Web Site" so that element is not under a component.-->
          <iis:WebVirtualDir Id="VDir" Alias="[VIRTUALDIRECTORYVALUE]"
                             Directory="INSTALLLOCATION"
                              WebSite="DefaultWebSite">

            <!-- This turns the Virtual Directory into a web application. -->
            <iis:WebApplication Id="MyWebAppApplication"
                               Name="[VIRTUALDIRECTORYVALUE]" WebAppPool="AppPool"/>

            <iis:WebDirProperties Id="WebSite_Properties" AnonymousAccess="no"
                                  WindowsAuthentication="yes" DefaultDocuments="!(loc.DefaultDocument)"
                                  Script="yes" Read="yes" />

          </iis:WebVirtualDir>
          <CreateFolder/>
          <RemoveFolder Id= "GuidFolders" On= "uninstall"/>
      </Component>

        <!-- Components - this decides what we want to incude in our install
        Here we will alter our web.config for Impersonation,debug to false and connection string. -->
        <Component Id="Web.config" Guid="2ED81B77-F153-4003-9006-4770D789D4B6">

          <!--install our web.config file,this isnt part of our initial MSBUILD-->
         <File Id="Web.config" Name="Web.config" Source="$(var.SolutionDir)!(loc.WebApplicationProjectName)\Web.config" DiskId="1" KeyPath="yes" />

          <!--Modify our web.config - here we need to add Identity impersonation,changes session settings,add connection string settings and set debug setting-->
          <!--Ensure that the identity setting exists--> 
          <util:XmlFile Id="system.webidentity" 
                        File="[INSTALLLOCATION]Web.config" 
                        Action="createElement" 
                        ElementPath="/configuration/system.web" 
                        Name="identity" 
                        SelectionLanguage="XPath"
                        Sequence="1" />

          <util:XmlFile Id="system.webIdentityAttribute" 
                        Action="setValue" 
                        File="[INSTALLLOCATION]Web.config" 
                        ElementPath="/configuration/system.web/identity" 
                        Name="impersonate" 
                        Value="true" 
                        SelectionLanguage="XPath"
                        Sequence="2" />

          <util:XmlFile Id="system.webIdentityAttribute2" 
                        Action="setValue" 
                        File="[INSTALLLOCATION]Web.config" 
                        ElementPath="/configuration/system.web/identity" 
                        Name="password" 
                        Value="[IMPERSONATIONUSERPASSWORD]" 
                        SelectionLanguage="XPath"
                        Sequence="3" />

          <util:XmlFile Id="system.webIdentityAttribute3" 
                        Action="setValue" 
                        File="[INSTALLLOCATION]Web.config" 
                        ElementPath="/configuration/system.web/identity" 
                        Name="userName" 
                        Value="[IMPERSONATIONUSER]"
                        SelectionLanguage="XPath"
                        Sequence="4" />

          <util:XmlFile Id="ModifyConnectionString" 
                        Action="setValue" 
                        Permanent="yes" 
                        File="[INSTALLLOCATION]Web.config"
                        ElementPath="/configuration/connectionStrings/add[\[]@name='!(loc.EntityName)'[\]]" 
                        Name="connectionString"
                        Value="[CONNECTION_STRING]Data Source=[DBSERVER];Initial Catalog=[DBNAME];Integrated Security=True;MultipleActiveResultSets=True&quot;" 
                        SelectionLanguage="XPath"
                        Sequence="5"/>

          <!--<authentication mode="Forms">-->
          <util:XmlFile Id="AuthenticationModeWindows" 
                        Action="setValue" 
                        File="[INSTALLLOCATION]Web.config"
                        ElementPath="/configuration/system.web/authentication" 
                        Name="mode" 
                        Value="Windows" 
                        Sequence="6" />

          <!--Switch off debug-->
          <util:XmlConfig Sequence="7" 
                          Id="SwitchOffDebug" 
                          File="[INSTALLLOCATION]\web.config" 
                          Action="create" On="install" 
                          Node="value" 
                          ElementPath="/configuration/system.web/compilation" 
                          Name="debug" 
                          Value="false" />


           <!--Session configuration  <sessionState mode="InProc" timeout="15" />-->
          <util:XmlFile Id="system.websessionState" 
                        File="[INSTALLLOCATION]Web.config" 
                        Action="createElement" 
                        ElementPath="/configuration/system.web" 
                        Name="sessionState" 
                        Sequence="8" />

          <util:XmlFile Id="system.websessionStateAttribute" 
                        Action="setValue" 
                        File="[INSTALLLOCATION]Web.config" 
                        ElementPath="/configuration/system.web/sessionState" 
                        Name="mode" Value="InProc" 
                        Sequence="9" />

          <util:XmlFile Id="system.websessionStateAttribute2" 
                        Action="setValue" 
                        File="[INSTALLLOCATION]Web.config" 
                        ElementPath="/configuration/system.web/sessionState" 
                        Name="timeout" 
                        Value="15" 
                        Sequence="10" />

          <util:XmlFile Id="system.websessionStateAttribute3" 
                        Action="setValue" 
                        File="[INSTALLLOCATION]Web.config" 
                        ElementPath="/configuration/system.web/sessionState" 
                        Name="cookieName" 
                        Value="[VIRTUALDIRECTORYVALUE]" 
                        Sequence="11" />
        </Component>

<iis:WebSite Id='DefaultWebSite'
             Description='Default Web Site'
             Directory='INSTALLLOCATION' SiteId ='[WEBSITEVALUE]' >

  <iis:WebAddress Id="AllUnassigned" Port="80" />
</iis:WebSite>
<iis:WebAppPool Id="AppPool" Name="[APPPOOLVALUE]" />

<CustomAction Id="MapVirtualDirectory"  Directory="INSTALLLOCATION"  Return="asyncNoWait"
              ExeCommand='[ASPNETREGIIS] -norestart -s "W3SVC/[WEBSITEVALUE]/ROOT/[VIRTUALDIRECTORYVALUE]"' />

<InstallExecuteSequence>
  <Custom Action="MapVirtualDirectory" After="InstallFinalize"    >ASPNETREGIIS AND NOT Installed</Custom>
</InstallExecuteSequence>

<CustomAction Id="GetIISWebSites" BinaryKey="IisManager" DllEntry="GetWebSites" Execute="immediate" Return="check" />
<CustomAction Id="GetIISAppPools" BinaryKey="IisManager" DllEntry="GetAppPools" Execute="immediate" Return="check" />

<InstallUISequence>
  <Custom Action="GetIISWebSites" After="CostFinalize" Overridable="yes">NOT Installed</Custom>
  <Custom Action="GetIISAppPools" After="CostFinalize" Overridable="yes">NOT Installed</Custom>
</InstallUISequence>

<Feature Id='ApplicationFeatures' Title="!(loc.ProductName)" Level='1'>
  <ComponentRef Id='WebVirtualDirComponent' />
  <ComponentGroupRef  Id="MyWebApp_Project" />
  <ComponentRef Id="Web.config" />

</Feature>

<!-- Specify UI -->
<Property Id="WIXUI_INSTALLDIR">INSTALLLOCATION</Property>
<UIRef Id="MyCustomUI"/>

这是我的本地化文件:

<?xml version="1.0" encoding="utf-8"?>
<WixLocalization Culture="en-us" xmlns="http://schemas.microsoft.com/wix/2006/localization">

  <!--application settings-->
  <String Id="LANG">1033</String>
  <String Id="ProductName">MyTestWebSite</String>
  <String Id="ProductVersion">1.0.0.0</String>
  <String Id="CompanyName">MyCompanyName</String>
  <String Id="DefaultDocument">Default.aspx</String>
  <String Id="WebApplicationProjectName">MyWebApp</String>


  <!--database settings-->
  <String Id="EntityName">MyEntities</String>
  <String Id="ModelName">MyModel</String>

</WixLocalization>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


HTML代码中要想改变字体颜色,常常需要使用CSS样式表。CSS是一门用来描述网页上样式的语言,通过编写CSS代码可以实现网页中各元素的大小、颜色、字体等各种样式的控制。那么如何在HTML代码中应用CSS样式来改变字体颜色呢?这里为大家介绍一下。 首先,在HTML代码...
HTML代码如何让字体盖住图片呢?需要使用CSS的position属性及z-index属性。 img { position: relative; z-index: -1; } p { position: absolute; to...
HTML代码字体设置 在HTML中,我们可以使用标签来设置网页中的文字字体。常用的字体标签是font和style,下面我们来学习如何使用这些标签。 1. font标签 使用font标签可以改变文字的字体、颜色和大小。它有三个属性font-family、color和...
在网页设计中,HTML代码的字体和字号选择是非常重要的一个环节,因为它们直接关系到页面的可读性和视觉效果。 要指定文本的字体和字号,可以使用HTML中的样式属性。使用样式属性设置字体和字号,如下所示: <p style="font-family: Aria...
HTML(Hypertext Markup Language,超文本标记语言)是一种用于创建网页的标准语言。它由许多标签(一对尖括号包围的关键字)组成,这些标签告诉浏览器如何显示内容。使用HTML代码,我们可以轻松地创建各种类型的图像和图形,如太极图。 在HTM...
外链是指在一个网页中添加一个指向其他网站的链接,用户可以通过这个链接直接跳转到其他网站。在HTML中,实现外链的方法很简单,只需要使用标签就可以了。 <a href="http://www.example.com">这是一个外链,点击跳转到www.ex...
HTML代码是实现网页界面的基础,而网页中的各种表单则是用户和网站进行交互的重要方式之一。下面我们来介绍如何使用HTML代码实现一个简单的报名表格。 <form action="submit.php" method="post"> &lt...
HTML是一种标记语言,用于开发网站和其他互联网内容。字体是网站设计中的关键元素之一,它可以决定网站的整体风格和呈现效果。HTML提供了字体编辑器,使网站设计变得更加容易。 <font face="Arial"> 这里是Arial字体 &...
HTML代码中,字体样式是开发者们必备的一部分。在HTML中,我们可以通过特定的标签和属性设置字体的样式、颜色和大小,以达到更好的排版效果。 <p style="font-size: 14px; color: #333; font-family:...
HTML中的字体可以设为粗体,以强调文本信息。我们可以通过使用一些标签来实现这一功能。其中,常用的标签包括: 1. 标签:该标签会把文本加粗显示,语法如下: 这是一段加粗的文本 2. 标签:与标签作用相同,但语义更强,表示该文本内容的重要性。语法如下:...
HTML代码可以实现文件的上传和下载,在网页开发中相当常见。通过使用<input>标签和<form>标签,我们可以轻松创建一个文件上传表单。 <form action="upload.php" method="post" enct...
HTML代码非常常见于网页设计中。在一些需要处理时间相关数据的场景下,可能需要将时间戳转换为实际时间,这时候就需要使用一些特定的HTML代码。 function timeStamp2Time(time){ var date = new Date(time...
HTML是一种用于创建网页的标记语言。在HTML中,我们可以使用超链接标签实现下载文件到本地的功能。 具体实现步骤如下: <a href="文件的URL" download="文件名">下载文件</a> 其中,href属性是文件...
在HTML代码中,对于字体靠左对齐有各种方法。其中最简单的方式之一是使用pre标签。 使用pre标签可以保留一段文本中的空格和换行符,从而使代码排版更加整齐。下面是一个例子: <p>这是一个段落。</p> &lt...
HTML代码字典是一本解释HTML标记和属性的参考文献。这本字典中包含了最常用的HTML代码以及它们的属性和值的详细描述。 例如,以下是HTML代码字典中的一部分内容: <a href="url">link text</a> 在...
在网页开发过程中,遇到一些需要用户复制的内容,我们通常都会提供复制按钮,让用户更方便地复制所需内容。下面我们来介绍如何使用html代码实现一键复制的功能。 var copyBtn = document.querySelector('#copy-bt...
用户登录 欢迎来到公司登录界面,请输入用户名和密码登录 用户名: 密码: 代码解释: 第1行:定义了一个 HTML 文档 第2行:开始了 HTML 头部 第3行:定义了...
HTML 代码是用来创建网页的语言,它包含了许多不同的元素和属性,让我们可以在网页中添加各种不同的元素和内容,如文字、图片、链接等等。在编写 HTML 代码时,我们可以使用各种不同的样式来美化我们的网页,例如更改字体、颜色、大小等等。 font-family:...
HTML代码中的字体转移 在编写HTML代码时,我们经常会使用各种字体来增强页面的可读性和视觉效果。但是,有些字符或特殊符号可能会在HTML中具有不同的含义,这就需要使用字体转义转换成HTML可以正常显示的字符。 在HTML中,使用"&"符号表示一个特殊字符将要被转...
HTML 编程语言中,你可以使用字体属性来更改文本的字体大小、颜色和样式。其中,字体颜色是最常用的样式更改。在 HTML 中,你可以使用 "color" 属性来更改文本的颜色。下面是一个使用 "pre" 标签的代码示例,演示如何使用 "color" 属性来更改字体颜色...