Building Blocks of the C# ASP.NET MVC Web Stack
Summary #
The ASP.NET web stack is Microsoft’s mature platform for creating feature-rich, powerful web applications.
Rather than explaining how to write code for the ASP.NET stack, this article intends to walk through each of the major building blocks, providing information necessary to successfully deploy, upgrade, and administer the ASP.NET Web Stack in your organization.
The Building Blocks #
- C# Language
- Compiler
- MSBuild
- .NET Framework/CLR
- .NET Core
- ASP.NET
- MVC
- NuGet
- Visual Studio
- IIS
C# Language #
- C# is a high-level language that gets transformed into MSIL (Microsoft Intermediate Language by the compiler
- MSIL is then further compiled down to machine language by the Runtime
- MSIL is platform/language agnostic. C#, VBasic, F# all compile to the same MSIL
- MSIL is executed by the .NET Common Language Runtime (CLR)
- The C# language version is not explicitly tied to the version of the .NET CLR, so typically you do not need to update the CLR every time there are improvements/upgrades to the C# language
- In your C# project file, you choose the version of the .NET framework to target, which determines what MSIL is emitted by the compiler so it can be run on the target server
- Certain C# language features in the future may be incompatible with old versions of the .NET Framework, but in that case your project will fail to build at compile time
- New C# language features are only available with updates to the compiler(s)
- ASP.NET is unique in that there is the compilation that happens during development, but also the run-time compilation of view files!
C# Compiler #
- Starting in 2013, Roslyn is the new C# compiler for transforming C# into MSIL
- Prior to Roslyn, csc.exe was the main compiler, written in C++, and it shipped with the .NET Framework
- The ASP.NET runtime will compile views on-the-fly. To make it use Roslyn for this, install the CodeDOM Provider NuGet Package in your MVC project
- Roslyn on GitHub
MSBuild #
- Suite of tools around building/deploying .NET applications
- Leverages a specific compiler under the covers; I don’t see a way to force MSBuild to use an older compiler
- Previously shipped with the .NET framework under C:/Windows/Microsoft.NET/Framework/v3.5/MSBuild.exe, leveraging csc.exe compiler
- Starting in 2013, it started shipping with Visual Studio Releases under C:/Program Files (x86)/MSBuild/14.0/MSBuild.exe, leveraging Roslyn compiler
- It’s possible to compile a C# project without using MSBuild, but there’s no compelling reason to do that, and it’s so tightly integrated that it would be a challenge to do so
- MSBuild on GitHub
.NET Framework/CLR #
- The .NET Framework is the Common Language Runtime (CLR) + managed libraries and tools
- The CLR always ships with the .NET Framework major version
- Targeting newer versions of the .NET Framework gives you access to new/modified libraries, as well as performance improvements when executing your MSIL code in the new CLR
- The managed libraries include ASP.NET, Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and Windows Workflow Foundation (WWF), each of which have a dedicated team making improvements that ship together as a .NET Framework Release
- Recently, some teams, especially ASP.NET have been shifting towards shipping releases out-of-band from the full framework as NuGet packages to enable faster release cycles
- Current Versions
.NET Core (CoreCLR + .NET CoreFX Libraries) #
The future of the .NET Framework for ASP.NET Applications.
Primarily available via NuGet: understanding and creating processes for working with NuGet is becoming increasingly critical for building ASP.NET applications.
-
.NET Core
- Is a subset of the .NET Framework
- Officially part of ASP.NET
- Officially supported by Microsoft on Windows, Mac, and Linux
-
NuGet Packages
- In contrast to the .NET Framework, the .NET Core platform is delivered as a set of NuGet packages
- These packages are designed to be modular so you only need to include the .NET Framework libraries you will actually use in your application
- Code named CoreFX, these libraries are refactored versions of the regular framework libraries that aim to reduce the number of interdependencies
-
Side-by-side
- Also, by packaging the runtime with your application, .NET Core eliminates the need to have a specific .NET Framework version installed on the target server
- In fact, you can run several apps on the same server, all targeting a different version of the .NET Framework
- I.e. by using the side by side execution of ASP.NET 5, you decide when you adopt .NET updates on an app basis (as opposed to machine basis).
- With reduced library dependencies, applications will be less affected by framework updates, since they will only be affected when their specific dependencies are updated
-
Availability
- Intended to supplant the many versions of .NET already existing: Compact Framework, Mono, Xamarin, others
- Available for Console Apps, Web Applications, and Native Apps
- Not available for WPF, Windows Forms, Xamarin Applications
- One of the major design changes in .NET Core is full support for Dependency Injection so that it’s easy to target towards new platforms (perhaps other flavors of linux, or BSD, etc.)
- MSDN Docs
- MSDN Blog
- Roadmap
Although CoreFX will be made available as a fairly large number of individual NuGet packages, it will continue to ship periodically as a full unit that Microsoft has tested as a whole. These distributions will most likely ship at a lower cadence than individual packages, allowing time to perform necessary testing, fixes, and the distribution process.
Basically, the CLR will be different, the available libraries will be different and even the low level integration into IIS or self-hosted options are also different. All in all, the .NET Core 5 stack will be much lighter, a with a much smaller memory footprint.
ASP.NET #
- The portion of the .NET framework containing methods and code for creating web applications using Microsoft technology
- Although sharing the acronym, ASP.NET is in no way related to Active Server Pages of years gone by
- ASP.NET encompasses
- WebPages (embedded code, like PHP)
- Web Forms (modular components/controls, similar design to WPF)
- MVC (similar to Ruby on Rails format)
- WebAPI (for easily creating REST APIs)
- SignalR (two-way open communication between clients and servers, really cool tech!)
- This page helps to clarify when to use each of these
- ASP.NET applications primarily run on IIS, but also in its own process via the .NET Core initiative, and on Mono
- www.asp.net is a great starting point for learning more, and the documentation is well-organized for getting started
MVC #
- Set of libraries that enable large, complex web applications
- Sensible defaults, but open-source and extensible at every stage to enable custom logic where needed
- Deployed by installing the appropriate NuGet packages into your C# Project
- Major components:
- Route Handling (custom routes, default routes)
- Authentication: username/password, ActiveDirectory,
- Authorization (roles, restrict access to portions of your application)
- Standard models, views, controllers, but also support for pre-compiled views to make refactoring safer, and find errors at compile-time
- Advantages
- Excellent performance, battle-tested over several years
- Microsoft is heavily investing in continuing to develop the platform and drive towards better integration with common open-source tools such as Bootstrap, Bower, Grunt, and other packages available on NuGet
- Built-in support for common security problems such as XSS, XSRF, SQL Injection, etc.
- Auto-detecting mobile browsers, serving separate version or separate stylesheets
- Contains helpers for defining one set of data validation rules, and then applying them both server-side and client side through javascript
- Support for internationalizing web applications via the .NET Framework
NuGet #
- Package manager for .NET applications
- NuGet packages installed in a C# project can be restored at any time by the developers on your team from NuGet.org so there’s no need to embed shared DLLs in your application, or worry about dev/production systems having the exact same version of the .NET Framework installed
- I’ll be writing a separate post in more detail about NuGet, but for now here’s a link to some interesting background on NuGet
Visual Studio 2015 #
- The main code editing environment for C# and .NET applications
- Installs .NET Framework 4.6.1
- Installs MSBuild 14.0
- Leverages MSBuild.exe to compile your C# code
- Immediately allows writing C#6 code in the editor
IIS #
- Runs ASP.NET websites
- Can be enabled on all windows server operating systems via “Web Server” role
- IIS App Pools will target the CLR version of .NET, which for .NET Framework 4.6.1 is .NET CLR 4.0