In October 2007, Microsoft announced a new MVC Web development platform, built on the core ASP.NET platform, clearly designed as a direct response to the evolution of technologies such as Rails and as a reaction to the criticisms of Web Forms. The following sections describe how this new platform overcame the Web Forms limitations and brought ASP.NET back to the cutting edge.
It is important to distinguish between the MVC architectural pattern and the ASP.NET MVC Framework. The MVC pattern is not new—it dates back to 1978 and the Smalltalk project at Xerox PARC—but it has gained enormous popularity today as a pattern for Web applications, for the following reasons:
User interaction with an MVC application follows a natural cycle: the user takes an action, and in response the application changes its data model and delivers an updated view to the user. And then the cycle repeats. This is a convenient fit for Web applications delivered as a series of HTTP requests and responses.
Web applications necessitate combining several technologies (databases, HTML, and executable code, for example), usually split into a set of tiers or layers. The patterns that arise from these combinations map naturally onto the concepts in MVC.
The ASP.NET MVC Framework implements the MVC pattern and, in doing so, provides greatly improved separation of concerns. In fact, ASP.NET MVC implements a modern variant of the MVC pattern that is especially suitable for Web applications.
By embracing and adapting the MVC pattern, the ASP.NET MVC Framework provides strong competition to Ruby on Rails and similar platforms, and brings the MVC pattern into the mainstream of the .NET world. By capitalizing on the experience and best practices discovered by developers using other platforms, ASP.NET MVC has, in many ways, pushed forward beyond what even Rails can offer.
The MVC Framework is built as a series of independent components that satisfy a .NET interface or that are built on an abstract base class. You can easily replace components, such as the routing system, the view engine, and the controller factory, with a different one of your own implementation. In general, the MVC Framework gives you three options for each component:
Use the default implementation of the component as it stands (which should be enough for most applications).
Derive a subclass of the default implementation to tweak its behavior.
Replace the component entirely with a new implementation of the interface or abstract base class.
Tight Control over HTML and HTTP
ASP.NET MVC produces clean, standards-compliant markup. Its built-in HTML helper methods produce standards-compliant output, but there is a more significant philosophical change compared with Web Forms. Instead of generating out swathes of HTML over which you have little control, the MVC Framework encourages you to craft simple, elegant markup styled with CSS.
Of course, if you do want to throw in some ready-made widgets for complex UI elements such as date pickers or cascading menus, ASP.NET MVC’s “no special requirements” approach to markup makes it easy to use best-of-breed UI libraries such as jQuery UI or the Bootstrap CSS library. ASP.NET MVC meshes so well with jQuery, for example, that Microsoft ships jQuery as a built-in part of the default Visual Studio ASP.NET MVC project template, along with other popular libraries, such as Bootstrap, Knockout and Modernizr.
ASP.NET MVC–generated pages don’t contain any View State data, so they are smaller than typical pages from ASP.NET Web Forms. Despite today’s fast connections, this economy of bandwidth still gives an enormously improved end-user experience and helps reduce the cost of running a popular web application.
ASP.NET MVC works in tune with HTTP. You have control over the requests passing between the browser and server, so you can fine-tune your user experience as much as you like. AJAX is made easy, and there aren’t any automatic postbacks to interfere with client-side state.
The MVC architecture gives you a great start in making your application maintainable and testable because you naturally separate different application concerns into independent pieces. Yet the ASP.NET MVC designers didn’t stop there. To support unit testing, they took the framework’s component-oriented design and made sure that each separate piece is structured to meet the requirements of unit testing and mocking tools.
They added Visual Studio wizards to create unit test projects on your behalf, which can be integrated with open source unit test tools such as NUnit and xUnit as well as the test tools that are included in Visual Studio. Even if you have never written a unit test before, you will be off to a great start.
Testability is not only a matter of unit testing. ASP.NET MVC applications work well with UI automation testing tools, too. You can write test scripts that simulate user interactions without needing to guess which HTML element structures, CSS classes, or IDs the framework will generate, and you do not have to worry about the structure changing unexpectedly.
Powerful Routing System
The style of URLs has evolved as Web application technology has improved. URLs like this one:
are increasingly rare, replaced with a simpler, cleaner format like this:
There are some good reasons for caring about the structure of URLs. First, search engines give weight to keywords found in a URL. A search for “rent in Chicago” is much more likely to turn up the simpler URL. Second, many Web users are now savvy enough to understand a URL, and appreciate the option of navigating by typing it into their browser’s address bar. Third, when someone understands the structure of a URL, they are more likely to link to it, share it with a friend, or even read it aloud over the phone. Fourth, it doesn’t expose the technical details, folder, and file name structure of your application to the public Internet, so you are free to change the underlying implementation without breaking all your incoming links.
Clean URLs were hard to implement in earlier frameworks, but ASP.NET MVC uses a feature known as URL routing to provide clean URLs by default. This gives you control over your URL schema and its relationship to your application, offering you the freedom to create a pattern of URLs that is meaningful and useful to your users, without the need to conform to a predefined pattern. And, of course, this means you can easily define a modern REST-style URL schema if you wish.
Built on the Best Parts of the ASP.NET Platform
Microsoft’s existing ASP.NET platform provides a mature, well-proven set of components and facilities for developing effective and efficient Web applications. First and most obviously, as ASP.NET MVC is based on the .NET platform, you have the flexibility to write code in any .NET language and access the same API features—not just in MVC itself but in the extensive .NET class library and the vast ecosystem of third-party .NET libraries.
Second, ready-made ASP.NET platform features—such as authentication, membership, roles, profiles, and internationalization—can reduce the amount of code you need to develop and maintain any Web application, and these features are just as effective when used in the MVC Framework as they are in a classic Web Forms project. The underlying ASP.NET platform provides a rich set of tools on which to build web applications with the MVC Framework.
Microsoft’s .NET platform has evolved with each major release, supporting – and even defining – the state-of-the-art aspects of modern programming. ASP.NET MVC 5 is built for .NET 4.5.1, so its API can take full advantage of recent language and runtime innovations, including the await keyword, extension methods, lambda expressions, anonymous and dynamic types, and Language Integrated Query (LINQ). Many of the MVC Framework’s API methods and coding patterns follow a cleaner, more expressive composition than was possible with earlier platforms. Don’t worry if you are not up to speed on the latest C# language features.
ASP.NET MVC Is Open Source
Unlike previous Microsoft Web development platforms, you are free to download the original source code for ASP.NET MVC, and even modify and compile your own version of it. This is invaluable when your debugging trail leads into a system component and you want to step into its code (and even read the original programmers’ comments). It is also useful if you are building an advanced component and want to see what development possibilities exist, or how the built-in components actually work.
Additionally, this ability is great if you do not like the way something works, if you find a bug, or if you just want to access something that’s otherwise inaccessible, because you can simply change it yourself. However, you’ll need to keep track of your changes and reapply them if you upgrade to a newer version of the framework. ASP.NET MVC is licensed under the Microsoft Public License (Ms-PL, http://www.opensource.org/licenses/ms-pl.html), an Open Source Initiative (OSI)–approved open source license. This means that you can change the source code, deploy it, and even redistribute your changes publicly as a derivative project. You can download the MVC source code from http://aspnetwebstack.codeplex.com.
Pro ASP.NET MVC 5, Fifth Edition
By: Adam Freeman