[VS2013]发布网站时修改配置文件

本文来自:https://msdn.microsoft.com/en-us/library/ee942158.aspx#encrypt_webconfig

Web Deployment FAQ for Visual Studio and ASP.NET

.NET Framework 4.5
 

 

This topic answers frequently asked questions about how to deploy Visual Studio web projects.

NoteNote

This topic applies to Visual Studio 2012 and Visual Studio Express 2012 for Web. The topic covers features that are included in the latest Visual Studio Web Publish Update available as of June, 2013. Most of these features are also available in Visual Studio 2010 and Visual Web Developer 2010 Express when you install the Web Publish Update.

Many of the answers instruct you to change deployment settings by editing the publish profile (.pubxml) file or the wpp.targets file. For information about how to do this, see How to: Edit Deployment Settings in Publish Profile (.pubxml) Files and the .wpp.targets File in Visual Studio Web Projects.

This topic contains the following sections:

Can I exclude specific files or folders from deployment?

 

You can limit the files that are deployed by selecting the Only files needed to run this application or All files in this projectoptions on the Package/Publish Web tab. If you select the All files in this project option, you can right-click a file in Solution Explorer and select Exclude From Project to keep it from being deployed. For more information about what files are excluded when you use the Only files needed to run this application or All files in this project options, see Why don't all of the files in my project folder get deployed?.

If these options are not flexible enough for you, another option is to edit the .pubxml or the .wpp.targets file and add an ExcludeFilesFromDeployment element or an ExcludeFoldersFromDeployment element (or both) in the PropertyGroupelement. In each element, you can specify a single name, or you can specify multiple names delimited by semicolons (;), as shown in the following example:

 
<PropertyGroup">
  <ExcludeFilesFromDeployment>
    File1.aspx;File2.aspx
  </ExcludeFilesFromDeployment>
  <ExcludeFoldersFromDeployment>
    Folder1;Folder2
  </ExcludeFoldersFromDeployment>
</PropertyGroup> 

Another option is to use the PublishIgnore NuGet package. This option is explained in Web Publishing a simpler way to exclude files/folders from being published on the .NET Web Development and Tools blog.

For more information, see the following posts on Sayed Hashimi's blog:

  1. Web Deployment Tool (MSDeploy): Build Package including extra files or excluding specific files

  2. Web Deployment Tool (MSDeploy): How to exclude files from package based on Configuration

Back to Top

Why don't all of the files in my project folder get deployed?

 

From the Project menu select Package/Publish Settings to open the Package/Publish Web tab of the Project Propertieswindow. A drop-down list in the section labeled Items to deploy (applies to all deployment methods) offers three options:

  • Only files needed to run this application. This is the default value. Visual Studio tries to determine which files are required for the application to run successfully. For example, this includes assemblies in the bin folder, files generated during the build, and files marked as Content. To see if a file is marked as Content, select the file in Solution Explorer, and check the file's Build Action property in the Properties window. You can change the Build Action value to Content to cause the file to be deployed, or change it to something else, such as None, to prevent the file from being deployed. Some file types that are automatically set to Content include .master, .svc, .ashx, .asax, .skin, .browser, .config, .and sitemap. A file must be included in the project in order to have a Build Action property.

  • All files in this project. Visual Studio deploys all files that are included in the project, regardless of their Build Actionproperty values.

  • All files in the project folder. Visual Studio deploys all files that are in the project folder and subfolders, regardless of whether they are included in the project or their Build Action property values.

If you are familiar with MSBuild syntax, you can find details about how these three options work in the following files:

  • Microsoft.Web.Publishing.OnlyFilesToRunTheApp.targets

  • Microsoft.Web.Publishing.AllFilesInTheProject.targets

  • Microsoft.Web.Publishing.AllFilesInProjectFolder.targets

These files can be found at the following location in a computer that has Visual Studio installed:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\

Back to Top

How do I disable Web.config transformation?

 

You have several options:

  • Comment out specific transforms in the Web.config transform file.

  • Rename the transform file to a name that does not correspond to a defined build configuration or a Publish profile. For example, you might change Web.Debug.config to Web.Debugx.config. (You might have to rename the file outside of Visual Studio.)

  • Delete the transform file. If you have customized the file, the customizations will be lost.

  • Edit the .pubxml file or .wpp.targets file by adding a TransformWebConfigEnabled element to the PropertyGroupelement and setting its value to False.

Back to Top

When should I use Web Deploy parameters instead of Web.config transformation?

 

Web Deploy parameters are more complex to set up than Visual Studio Web.config transformations but are very flexible. Web Deploy parameters are complex to set up because they can automate many other deployment tasks, such as updating database scripts and IIS settings. Web Deploy parameters are useful for Web.config transformation when you are creating a deployment package and when you are creating the package you don't know the values that need to go into the deployed Web.config file. Web Deploy parameters let you specify values for the parameters when the package is installed, not just when it is created. This is especially useful in enterprise environments, where it's common for different people to be responsible for creating and installing deployment packages. For example, the developer who creates a package might not know certain passwords that need to be in the Web.config file. The IT administrator who installs the package can enter those values when the package is installed. For more information, see Parameterization vs. Web.config Transformation on Vishal Joshi's blog and How to: Use Web Deploy Parameters in a Web Deployment Package.

Back to Top

How do I deploy a Code First database without Migrations?

 

When you implement an Entity Framework Code First context class to access a database, the Settings tab of the Publish Webwizard displays a check box that lets you use Code First Migrations to automate deployment for that database. But if you are only accessing the database by using the Code First API, and Code First is not being used to create the database, you can't use Migrations to deploy it. In this scenario, what you want is the Update database check box that lets you deploy a SQL Server database that you don't use a Code First context for.

How do I deploy an Entity Framework database that uses DbContext without using Migrations?

  1. In Visual Studio, if you have the Publish Web wizard open, close it.

  2. In the application Web.config file, create an additional connection string element for the database. Give this new connection string element a name that does not match either the context class name or the fully qualified class name.

  3. Build the project, and then open the Publish Web wizard and select the profile you want to work with.

  4. Select the Settings tab.

    You now see two entries for the database in the Databases section of the tab, one for the context class (with the Execute Code First Migrations check box) and one for the new connection string in the Web.config file.

  5. In the entry for the context class, enter the connection string that you want the application to use at run time, and clear the Execute Code First Migrations check box.

  6. In the entry for the new Web.config file connection string, enter the connection string that should be used to make schema changes during deployment, and select Update database.

    For more information about how to enter database deployment settings, see How to: Deploy a Web Project Using One-Click Publish in Visual Studio.

Back to Top

How to make Web Deploy use file checksums instead of dates to determine which files were changed?

 

By default, Web Deploy determines which files need to be copied to the server by comparing the dates that the local files were last changed against the dates that the server files were last changed. If you use a source control system that changes file dates when you check out files, it appears that they have all changed, and Web Deploy copies them all to the server when you publish.

An alternative for this scenario is to configure Web Deploy to use file checksums to determine which files have changed. Use checksums only if file dates are unreliable indicators of what has changed, because comparing checksums takes more CPU processing time than comparing dates.

To configure Web Deploy to use checksums instead of dates to determine which files need to be copied to the server, add the following element to the .pubxml file:

 
<MSDeployUseChecksum>true</MSDeployUseChecksum>

Insert this element in the PropertyGroup element, as shown below:

 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSDeployUseChecksum>true</MSDeployUseChecksum>
    <!— other settings omitted to keep the example short -->
    <PublishDatabaseSettings>
      <!— this section omitted to keep the example short -->
    </PublishDatabaseSettings>
  </PropertyGroup>
</Project>

See also Web publishing updates for app offline and usechecksum.

Back to Top

How to encrypt the Web.config file during deployment?

 

You can configure Web Deploy to automatically encrypt the Web.config file on the destination server by adding the following element to the .pubxml file:

 
<MSDeployEnableWebConfigEncryptRule>true</MSDeployEnableWebConfigEncryptRule>

Insert this element in the PropertyGroup element, as shown below:

 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSDeployEnableWebConfigEncryptRule>true</MSDeployEnableWebConfigEncryptRule>
    <!— other settings omitted to keep the example short -->
    <PublishDatabaseSettings>
      <!— this section omitted to keep the example short -->
    </PublishDatabaseSettings>
  </PropertyGroup>
</Project>

This feature only works with Web Deploy 3.5 and later versions, and it does not work in Windows Azure Web Sites. Windows Azure has other options for securely storing sensitive data. For more information, see Windows Azure Web Sites: How Application Strings and Connection Strings Work.

posted on 2018-04-18 11:42  z5337  阅读(656)  评论(0编辑  收藏  举报