问题描述
每次我都必须将文件作为链接链接到ASP.NET项目中时,总有一些事情我忘了做,因为Visual Studio对这些问题过于厌烦.有一个错误/限制,因此它不会将文件复制到Debug输出文件夹中,并且我们必须更改文件的属性,以便也将其复制到Publish输出文件夹中.
Every time I have to link a file into an ASP.NET project as link, there is always something I forget to do, because Visual Studio is way too buggy and annoying with these.There is a bug/limitation with it so it does not copy the file into the Debug output folder, and we must change the properties of the file so that it is copied into the Publish output folder as well.
经过大量研究,我以艰辛的方式学习了这一点,并创建了一个AfterBuild目标来帮助简化此过程.但是,添加文件时仍然需要做一些手动工作.
I learned this in the hard way, after a lot of research, and I created an AfterBuild target to help in making this process easier. However, there is still some manual work to do when adding the file.
这是我维护的项目的示例.假设我们要在ASP.NET项目的"utilities"文件夹中添加文件YIUCARD.
Here is an example for a project I maintain. Say we want to add the file YIUCARD in the "utilities" folder of the ASP.NET project.
1)右键单击Visual Studio上的实用程序"文件夹,然后选择添加->现有项目".
1) Right-click "utilities" folder on Visual Studio and select "Add -> Existing item".
2)选择要添加的文件,单击一次(不要双击).
2) Select the file to add, clicking on it ONCE (do not double-click).
3)单击添加"按钮旁边的箭头,然后选择添加为链接".
3) Click in the arrow next to the "Add" button and select "Add As Link".
目前,这将无济于事.该文件将不会复制到任何文件夹(调试和发布).它只是添加到项目中.
At the moment, this will do nothing. The file won’t be copied to any folder (Debug and Publish). It is just added to the project.
4)右键单击文件,然后打开属性".
4) Right-click in the file and open Properties.
5)更改:从构建动作"到内容"从复制到输出目录"到始终复制"
5) Change:"Build Action" to "Content""Copy to Output Directory" to "Copy always"
这时,文件将被复制到发布"输出文件夹(发布时在我们定义的任何位置).但是,当我们在本地调试(F5)时,它将无法正常工作,因为该文件未复制到代码库的本地实用程序"文件夹中.
At this time, the file will be copied into the Publish output folder (anywhere we define when publishing).However, when we debug locally (F5), it won’t work because the file is not copied into the local "utilities" folder on the codebase.
6)通过选择全部保存"将更改保存在Visual Studio中.这样就可以保存".csproj"文件,因为我们对其进行了更改,现在我们将对其进行手动编辑.
6) Save the changes on Visual Studio, by selecting "Save All". This is so that the ".csproj" file is saved, as we made changes on it and we will now edit it manually.
7)在记事本++中打开".csproj".
7) Open the ".csproj" in Notepad++.
8)将文件添加到"BeforeBuild"事件中(这是我的):
8) Add the file to the "BeforeBuild" event (here's mine):
<Target Name="BeforeBuild">
<ItemGroup>
<UtilitiesFiles Include="..\path\to\file\REDOPXX.NEW" />
<UtilitiesFiles Include="..\path\to\file\REDFMTST" />
<UtilitiesFiles Include="..\path\to\file\JOBCARD" />
<UtilitiesFiles Include="..\path\to\file\YIUCARD" />
</ItemGroup>
<Copy SourceFiles="@(UtilitiesFiles)" DestinationFolder=".\utilities" SkipUnchangedFiles="true" />
</Target>
9)对于发布模式,我有一个"AfterBuild"事件,该事件会自动为我发布项目,以便文件直接进入我想要的输出文件夹:
9) I have an "AfterBuild" event for Release mode that automatically publishes the project for me, so that the files go directly to the output folder I want:
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
<PropertyGroup>
<OutputDir>..\path\to\publish\MyProject</OutputDir>
</PropertyGroup>
<Message Importance="High" Text="Output DIR: $(OutputDir)" />
<RemoveDir Directories="$(OutputDir)" ContinueOnError="true" />
<MSBuild Projects="MyProject.csproj" Properties="Configuration=$(Configuration);WebProjectOutputDir=$(OutputDir);OutDir=$(OutputDir)bin\" Targets="ResolveReferences;_CopyWebApplication" />
</Target>
10)将文件保存在Notepad ++中,然后将其重新加载到Visual Studio中.
10) Save the file in Notepad++ and reload it on Visual Studio.
构建项目时,文件将被复制到Debug和Release/Publish文件夹中!
When you build the project, the file will be copied to both Debug and Release/Publish folders!
但是,我想知道是否有任何更简单/直观的方法来做到这一点.
您可能会说可以通过将调试"输出文件夹更改为与发布/发布"文件夹相同来解决,以便在我第一次发布后这些文件就可以存在.但是,请注意,这也存在一个错误.如果我使用除"bin \"以外的输出文件夹,aspx文件将抱怨它们找不到程序集.即使将复制到输出目录"设置为始终复制",它也会抱怨找不到"Global.asax.cs".
You may say that this would be fixed by changing the Debug output folder to the same as the Release/Publish folder, so that the files would be there after the first time I published.However, please note that there is a bug with this, as well. If I use an output folder other than the "bin\", aspx files will complain that they can't find the assemblies. Even if I set "Copy to Output Directory" to "Copy Always", it will complain that "Global.asax.cs" could not be found.
请参阅以下相关问题:
解析器错误消息:无法加载类型'TestMvcApplication. MvcApplication'
无法加载类型[Namespace].Global" ;使我悲伤
在Visual Studio的较新版本中是否对此问题进行了修复/改进?
推荐答案
MSBuild. WebApplication.CopyContentLinkedFiles NuGet可能会帮助您.该软件包添加了一个MSBuild目标,该目标将在构建过程中作为链接添加的所有内容文件复制到Web应用程序文件夹.
The MSBuild.WebApplication.CopyContentLinkedFiles NuGet might help you. This package adds a MSBuild target which copies all content files added as link to the Web Application folder during the build.
这篇关于在Visual Studio上将文件添加为链接-调试与发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!