Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I customize the build of a web site project?

We have some custom MS Build scripts that generates code and configuration files for web application projects when they build. Currently we call these build scripts by overriding the "BeforeBuild" and "AfterBuild" targets in the .csproj file of the web project. However, in another solution I have a web site project which is opened directly from the local IIS and has no .csproj file. How can I customize the build of this type of project?

If it is not possible to customize the MS Build process of the web site, it would be good enough to invoke something like a .bat file which that would run my custom MS Build script. We are currently using a .bat file but the developers have to manually run it but they sometimes forget this. I can't see a simple way to kick it off automatically when clicking build.

I would like something that runs as part of the solution build, both in Visual Studio and Team Build.

like image 234
Tim Booker Avatar asked Dec 14 '12 13:12

Tim Booker


3 Answers

Note that unlike with Web Applications, Visual Studio doesn't really ever build Web Sites per se. Of course, there is certainly a Build command that you can run from VS, but all it does is basically check that your site can build without error. The key point is that when you build a Web Site from VS, it does not produce any build artifacts at all. Instead, everything that needs to be built happens at runtime when you send http requests to the site.

There are various ways that you can customize how things are built at runtime (e.g. using Build Providers), but if your goal is to generate a web.config file, then this will not help you.

As an alternative, you could potentially have your Web Site take a dependency on another project (e.g. a library), and then have that project perform some custom build actions on behalf of the site (e.g. by modifying the site's files). With the project dependency, you'll be guaranteed that the other project is always built before the Web Site.

like image 87
David Ebbo Avatar answered Nov 16 '22 23:11

David Ebbo


I have now achieved what I need by adding a separate MS Build file in my solution and ensuring it builds before the web site by using the project dependencies in Visual Studio.

The actual build file is quite minimal and is called "Configure.csproj" and sits in the root of the web site:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">
  <Import Project="CodeGeneration.targets" />
  <Target Name="Build">
    <CallTarget Targets="GenerateCode" />
  </Target>
  <Target Name="Rebuild">
    <CallTarget Targets="GenerateCode" />
  </Target>
  <Target Name="Clean">
  </Target>
</Project>

Right-click the solution and choose "Add existing project" and select this file.

It seems odd to have this named *.csproj when there is no C# to build, but this file extension is one recognised by Visual Studio when clicking "Add existing project".

Then to make sure this is built before the web site, right-click the solution and choose "Project dependencies". Select the web site project from the "Projects" drop-down list and then check the box next to "Configure".

It is of course possible to run a custom build after the web site by adding another project and adding a dependency on the web site.

like image 3
Tim Booker Avatar answered Nov 16 '22 21:11

Tim Booker


Visual Studio < 2012: Web Deployment Projects

Since 2005 Microsoft has released a separate installable project type for Visual Studio called the Web Deployment Project (sometimes referred to as WDP). It was first revealed by Scott Guthrie on his blog, and it hasn't changed much over the years, so his introduction is still a good read to understand in detail what WDP has to offer (or go to the MSDN reference).

When you create a WDP, the project is added to your solution with a built-in reference to an exsiting Web Site project. It's really only an MSBuild file (.wdproj), the contents of which are controlled through a settings dialog that gets added to Visual Studio. When this project is built, it outputs assemblies that are normally generated by the ASP.NET runtime on the server (e.g. on the first request for an ASP.NET file per each directory). In essence, you get what is called a pre-compiled website.

Because it is based on MSBuild, you can easily add this to your existing build scripts, or add your customizations to the <BeforeBuild> and <AfterBuild> elements (as well as <BeforeMerge> and <AfterMerge>). This should also work on a build server, although some features may require that a Windows SDK is installed. (In my experience aspnet_merge.exe was the main issue, it's responsible for merging all output assemblies into a single assembly if that's configured in the project file.)

This solution is the 'official' way to MSBuild a web site project, and is less convoluted than adding empty Class Library projects with dependency to simulate before and after build events. As a downside, it may increase build time when your web site is large. But to end with on a positive note, having pre-compiled websites built for you may improve your overall deployment efforts.

Downloads for Visual Studio 2008 and 2010 (sorry, I can't find the one for VS2005, it might be part of SP1 though).

Visual Studio 2012

For Visual Studio 2012 the situation has changed. There is no WDP release planned (although there is demand for it), but the new publish features will be supported for Web Site projects, according to this blog post from the web dev tools blog. Publish profiles are MSBuild files as well, so integrating them into you existing build scripts should be possible just as it is with WDP. See also: Visual Studio 2012 Web Deployment Projects are Dead – Long Live Publishing Profiles.

If you are now using both Web Application and Web Site projects, then you may find it beneficial to have the same build setup for both types of projects. In that case, when Publish Profile support for Web Site projects is released in the near future (there's now an ASP.NET and Web Tools 2012.2 RC with support for this), you should definitely switch to that, instead of two separate empty Class Library projects.

like image 3
Michiel van Oosterhout Avatar answered Nov 16 '22 22:11

Michiel van Oosterhout