简体   繁体   中英

ASP.NET Reusable User Controls Across Different Assemblies

We are currently developing several Web projects and would like to be able to create a project that includes User Controls shared across the Web projects instead of having to recreate them. From what I found out, the process overall is a pain since each project needs to have access to the actual ascx file and not just a reference to it.

My way of approaching this is:

1- Use Pre-Build events to copy .ascx files

from the shared assembly into current project's working folder. This works fine for debugging the application , but we need to do the same thing during a file-system publish .

2- Use MSBuild targets to copy .ascx files druing a publish

I added the following target to my project's configuration file:

<Target Name="AfterPublish" AfterTargets="PipelinePreDeployCopyAllFilesToOneFolder">
   <Exec Command="xcopy.exe SourcePath OutputPath" />
</Target>

(Note that SourcePath and TargetPath are full paths to the directories. I replaced them here to make it shorter)

This target gets called after a file-system publish (right-click on the project -> Publish), however, files don't get copied to the destination folder.

I've confirmed that the target gets called (by changing Command=explorer.exe , a Windows Explorer window pops up) and also ran the xcopy.exe command with the same exact paths on CommandPrompt and it did work fine. So I don't understand why it doesn't work as part of the target. Any ideas?

You may have to check your publish options and make sure that the "Delete all existing files prior to publish" 发布选项

The problem is the xcopy command always gets executed before the deletion happens, so they get copied but delete right after.

If you need a clean folder each time I suggest you put everything in your "AfterBuild"

<Target Name="AfterBuild">

<Exec Command="echo @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Clean Publish Folder @@@@@@@@@@@@@@@@@@@@@@@@@@@@" />
<Exec Command="echo $(PublishUrl)" />
<Exec Command="echo Publish Folder Deleted...." Condition="Exists('$(PublishUrl)')"/>
<!--<Exec Command="del /q $(PublishUrl)\*" Condition="Exists('$(PublishUrl)')"/>-->
<!--<Exec Command="FOR /D %%p IN (&quot;$(PublishUrl)*.*&quot;) DO rmdir &quot;%%p&quot; /s /q" />-->
<Exec Command="del /q $(PublishUrl)\*" Condition="Exists('$(PublishUrl)')"/>
<Exec Command="FOR /D %%p IN (&quot;$(PublishUrl)\*.*&quot;) DO rmdir &quot;%%p&quot; /s /q" />
<Exec Command="echo Publish Folder Creation...." Condition="Exists('$(PublishUrl)')"/>
<Exec Command="mkdir $(PublishUrl)" Condition="!Exists('$(PublishUrl)')"/>
<Exec Command="echo @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Copying @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" />
<Exec Command="xcopy.exe SourceDir DestinationDir /S /E /H" />

</Target>

MSBuild has a Copy task. Try the following:

<Target Name="AfterBuild">
  <ItemGroup>
    <MySource Include='path\to\files\**\*.*" />
  </ItemGroup>
  <Copy SourceFiles="@(MySource)" DestinationFolder="$(OutputDir)\%(RecursiveDir)" />
</Target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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