简体   繁体   English

自定义ASP.NET的发布

[英]Customize Publish for ASP.NET

I have an asp.net project and need to run some code after each publish. 我有一个asp.net项目,每次发布后都需要运行一些代码。

I tried to acomplish this using to ways: 我试图通过以下方式完成此操作:

  1. Useing the vs project file 使用vs项目文件
  2. Useing external build tool Cake (c# make) 使用外部构建工具Cake(C#make)

    1.) So found some topics related to this issue: 1.)因此找到了与此问题相关的一些主题:

So like suggested i added following code to my project file: 因此,如建议的那样,我在项目文件中添加了以下代码:

  <Target Name="BeforePublish">
    <Message Text="BeforePublish"></Message>
    <Warning Text="BeforePublish"></Warning>
  </Target>
  <Target Name="AfterPublish">
    <Message Text="AfterPublish"></Message>
    <Warning Text="AfterPublish"></Warning>
  </Target>
  <Target Name="MSDeployPublish" >
    <Message Text="MSDeployPublish"/>
    <Warning Text="MSDeployPublish"/>
  </Target>
  <Target Name="PipelinePreDeployCopyAllFilesToOneFolder" >
    <Message Text="PipelinePreDeployCopyAllFilesToOneFolder" />
    <Warning Text="PipelinePreDeployCopyAllFilesToOneFolder" />
  </Target>
  <Target Name="BeforeBuild">
    <Message Text="BeforeBuild"></Message>
    <Warning Text="BeforeBuild"></Warning>
  </Target>
  <Target Name="AfterBuild">
    <Message Text="AfterBuild"></Message>
    <Warning Text="AfterBuild"></Warning>
  </Target>

But only the Before- & AfterBuild get executed. 但是仅执行Before-和AfterBuild。 All the other targets just get ignored completly. 所有其他目标只是完全被忽略。 Maybe it's because these other topics use VS 2010 and 2012. Is there a working way to do this in VS 2017? 也许是因为这些其他主题都使用了VS 2010和2012。在VS 2017中有可行的方法吗?

2.) When i use following code in a task I don't get same output as when i would have compiled it with VS. 2.)当我在任务中使用以下代码时,与使用VS进行编译时得到的输出不同。

   MSBuild(projectFile, new MSBuildSettings()
   .WithProperty("OutDir", publishDir)
   .WithProperty("DeployTarget", "WebPublish")
   .WithProperty("DeployOnBuild", "true")
   .WithProperty("WebPublishMethod", "FileSystem")
   .WithProperty("Configuration", "Release")
   );

Therefore this isn't a viable solution atm as well. 因此,这也不是可行的解决方案。

I am thankful for any help getting this to work one way or another. 感谢您为使此方法以某种方式工作提供的帮助。

It depends on what you want to do after but if you want your code to run after publish you need to put your task after one of the targets from Microsoft.Web.Publishing.targets (search for the file to see full list of targets). 这取决于您要执行的操作,但是如果要在发布后运行代码,则需要将任务放在Microsoft.Web.Publishing.targets的目标之一之后(搜索文件以查看目标的完整列表) 。 Usually you need AfterTargets="MSDeployPublish" 通常您需要AfterTargets="MSDeployPublish"

1.) So found some topics related to this issue. 1.)因此找到了与此问题相关的一些主题。 So like suggested i added following code to my project file. 因此,如建议的那样,我在项目文件中添加了以下代码。 Actually,after my test in vs2017, from the output log with detailed, what I can find when publishing is like pics below: 实际上,在对vs2017进行测试之后,从输出日志中可以看到详细的信息,发布时我可以找到下面的图片:

在此处输入图片说明 在此处输入图片说明

1.It seems in VS2017, when publishing the project in IDE, there has no valid publish target to redefine or AfterTargets. 1,似乎在VS2017中,在IDE中发布项目时,没有有效的发布目标来重新定义或AfterTargets。 So I'm afraid you can't achieve this goal by publishing in VS. 因此,恐怕您无法通过在VS中发布来实现此目标。

2.Instead, I think you can consider using msbuild tool. 2.相反,我认为您可以考虑使用msbuild工具。 Add test target script below into your Publish profile (For me:FolderProfile.pubxml): 将下面的测试目标脚本添加到您的发布配置文件中(对于我:FolderProfile.pubxml):

<Project...>
...
  <Target Name="Test" AfterTargets="WebFileSystemPublish">
    <Message Text="I'm a test target after webpublish"/>
    <Move SourceFiles="C:\Ase\favicon.ico" DestinationFolder="C:\tomorrow"/>
  </Target>
</Project>

Open your developer command prompt for vs2017, and type command like: 打开您的开发人员命令提示符以获取vs2017,然后键入以下命令:

msbuild C:\xxx\WebApplication55\WebApplication55.sln /p:DeployOnBuild=true /p:PublishProfile=C:\xxx\repos\WebApplication55\WebApplication55\Properties\PublishProfiles\FolderProfile.pubxml 

It will run the test target after webpublish process. 在网络发布过程之后,它将运行测试目标。

2.) When i use following code in a task I don't get same output as when i would have compiled it with VS. 2.)当我在任务中使用以下代码时,与使用VS进行编译时得到的输出不同。 Not sure you use this code for what. 不确定您将这段代码用于什么。 But you can use test target to do what you want.Add a MSBuild Task into test target can achieve this goal for your #2. 但是您可以使用测试目标执行所需的操作。在测试目标中添加MSBuild任务可以为您的#2实现此目标。

And as you mentioned you have some code, compile those code to a .exe first, and add a Command Task into test target could help. 正如您提到的,您有一些代码,请先将这些代码编译为.exe,然后将Command Task添加到测试目标中可能会有所帮助。 In addition: According to your info above, it seems you use file system and asp.net, so I use asp.net web app to test(not .net core). 另外:根据上面的信息,看来您使用文件系统和asp.net,所以我使用asp.net Web应用程序进行测试(不是.net核心)。 A feedback would be expected. 将会收到反馈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM