Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: How to override the default "Build Action" for certain extension types per project or solution?

I'm serving my asp.net mvc views from many assemblies and copying views to the main application on post-build event.

This works, however, I realized, that when I change something in view and just hit F5, changes are not included. What I have to do to see changes is to: save, build<- explicitly clicking, and then hit F5. However, it's pretty annoying solution.

I discovered that setting Build action to "Embedded Resource" on view solves the problem as well, however other devs may not remember that they have to do this after adding every view to the solution.

Is there a way to override the default build action for certain file extensions, such as: *.aspx, *.ascx in project or (better) in solution ?

What I've found is an ability to add this setting globally, per machine, but I do not want to do that (link: http://blog.andreloker.de/post/2010/07/02/Visual-Studio-default-build-action-for-non-default-file-types.aspx)

Any ideas ?

like image 760
Łukasz Podolak Avatar asked Jul 23 '10 16:07

Łukasz Podolak


People also ask

How to change build action Visual Studio?

Set a build action Or, right-click on the file in Solution Explorer and choose Properties. In the Properties window, under the Advanced section, use the drop-down list next to Build Action to set a build action for the file.

How do I change the Build Action property?

using command promptClick in the solution explorer the file that will be modified. Then hit F4 or click with the right button in the file and then select the "Properties" option. In the Properties window, change Build Action to Embedded Resource.

What is build action in Visual Studio?

All files in a Visual Studio project have a build action. The build action controls what happens to the file when the project is compiled. This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see Build actions in Visual Studio for Mac.


1 Answers

Consider the following project file outline:

<Project ToolsVersion="3.5" DefaultTargets="EmbedViews;Build" ...>
...
  <Target Name="EmbedViews">
    <ItemGroup>
      <EmbeddedResource Include="Views\*\*.aspx" />
      <EmbeddedResource Include="Views\*\*.ascx" />
    </ItemGroup>
  </Target>
</Project>

This will add all aspx and ascx files in Views\<subfolder> to your library as Embedded Resource. Notice that EmbedViews is added to DefaultTargets before Build - order is important here, I found out making that mistake myself :-)

Since editing all your project files to get this snippet in is cumbersome, you could make your own project template with this included.

Please let me know if this helped.

like image 175
Jeroen Avatar answered Sep 30 '22 13:09

Jeroen