- 12 Jan 2023
- 2 Minutes to read
-
Print
-
DarkLight
-
PDF
ASP.NET MVC & WebForms
- Updated on 12 Jan 2023
- 2 Minutes to read
-
Print
-
DarkLight
-
PDF
Building an ASP.NET MVC or WebForms Application
ASP.NET MVC is the most popular web framework for .NET, allowing developers to create maintainable, scalable, and cross-platform web applications. By separating the concerns (i.e., not coupling the HTML views with the database backend and vice versa), teams of developers with different skill sets can focus on their areas of expertise without having to understand the intimate details of the underlying framework as they did with its predecessor, ASP.NET WebForms.
Building ASP.NET Applications
MSBuild (Microsoft Build Engine) is a tool for building all types of .NET applications, and it's used internally by Visual Studio to build projects and solutions. CI servers would perform similar build tasks or operations by invoking MSBuild operations.
MSBuild doesn't require Visual Studio to be installed, but you will need to install and configure the following:
- Visual Studio Build Tools are installed
- Ensure that ".NET desktop build tools" and "Web development build tools" options are chosen during installation/modification
The simplest way to build a web application is by running MSBuild directly:
msbuild.exe MyWebProject.csproj "/p:Configuration=Release" "/p:OutDir=C:\tmp\website\\"
The difference between web applications and other application types (e.g. console applications, WinForms or WPF) is that when an output directory is specified, the build output is generated in a special subdirectory in that format:
<outputDir>\_PublishedWebsites\<projectName>
The contents of this output directory are the files that would be deployed to the IIS home directory.
Building ASP.NET Applications with BuildMaster
Because building ASP.NET applications involves running MSBuild, building ASP.NET applications is simple with BuildMaster. Behind the scenes, BuildMaster uses the MSBuild::Build-Project
operation to run MSBuild.
The general process for building an ASP.NET application is as follows:
- Get source code from the source control repository
- Compile project with MSBuild
- Capture artifact for deployment
A rough example plan of this would be:
Git::Get-Source
(
RepositoryUrl: https://github.com/Inedo/ProfitCalc.git,
Branch: master
);
MSBuild::Build-Project ProfitCalc.Web\ProfitCalc.Web.csproj
(
To: ~\Output
);
Create-Artifact
(
From: ~\Output\_PublishedWebsites\ProfitCalc.Web
);
The MSBuild::Build-Project
operation in this example effectively runs the following MSBuild command:
msbuild.exe ProfitCalc.Web\ProfitCalc.Web.csproj "/p:Configuration=Release" "/p:OutDir=C:\...<buildmaster-temp>...\Output\\"
Restoring NuGet Packages
By default, MSBuild does not restore NuGet packages during a build, which is often the cause of "are you missing an assembly reference" errors.
To install NuGet packages before running MSBuild, use the NuGet::Restore-Packages()
as follows:
NuGet::Restore-Packages
(
Target: ~\Src\<project-name>,
Source: https://proget.corp/nuget/InternalNuGet/
);
This will essentially call nuget.exe install
, and instruct it to look for a packages.config
file in the SourceDirectory
, and packages in the target Source
.
Unit Tests
Unit tests for ASP.NET applications are handled by VSTest. An example operation to execute and capture unit test results is as follows:
WindowsSdk::Execute-VSTest
(
TestContainer: ~\Output\ProfitCalc.Tests.dll
);
Deploying ASP.NET Applications to IIS
While ASP.NET applications can be hosted on a variety of web servers, Microsoft's Internet Information Services (IIS) is the most common. See Deploying an IIS Website to learn how to accomplish this with BuildMaster.