elements. These controls also have other benefits, such as multi browser rendering support, a powerful programming model, layout control, and theme support. ■■Note For more information about the differences between HTML server controls and web server controls, visit http://bit.ly/ASPNETWebServerControls. ASP.NET Web Pages ASP.NET Web Pages (now in version 2) is a framework for building web applications in which pages use the Razor syntax. ASP.NET Razor uses a simple programming syntax that lets you embed server-based code into a web page. Razor pages use the extension .cshtml or .vbhtml depending on the language of choice (C# or VB.NET). Because the code is embedded in the web page, the idea of a code-behind file doesn’t exist in Razor. The content in Razor pages is created with HTML, and there are no web server controls such as ■■Note For more information about ASP.NET Web Pages, visit http://bit.ly/ASPNETWebPages. 6 www.it-ebooks.info Chapter 1 ■ Introducing ASP.NET MVC 4 ASP.NET MVC ASP.NET MVC is a free and fully supported framework for building web applications that use the model-view-controller pattern. Like ASP.NET Web Forms, ASP.NET MVC is built on top of the ASP.NET Framework. This means you can use in ASP.NET MVC applications the same APIs for security, state management, membership, caching, and so on that you could use in traditional ASP.NET Web Forms applications. In the ASP.NET MVC world, many improvements to ASP.NET have been included in the framework itself. The main purpose of this design pattern is to isolate business logic from the user interface in order to focus on better maintainability, improved testability, and a cleaner structure to the application. Every ASP.NET MVC application has three core parts: a model, views, and controllers. In short, the model consists of all the classes that handle data and business logic. Data processing using model classes is initiated by the controllers that are in charge of user requests. Once the data processing is complete the controller creates a response to the user by sending the results to a View who then produces HTML to be rendered in the browser. The MVC Pattern Figure 1-2 illustrates a simple implementation of the MVC pattern. The straight arrows indicate direct associations while the curved arrows identify indirect associations. Figure 1-2. Model-view-controller implementation The model in the MVC pattern represents the parts of the application that implement the data domain logic. The operation of the model might come from the generation of classes representing objects in a data store such as a database (for example, Entity Framework data classes). Views are the visible elements in the application. They are the components that typically show users data from the model. A view page typically receives a view model object from the controller (the view doesn’t care how this data was obtained—that’s the controller’s responsibility!). The view page contains HTML (and possibly some UI-related code) to determine how to render the model data back to the browser. The controllers are classes that collect the user requests, work with the model, and ultimately select a view to render the appropriate UI. 7 www.it-ebooks.info Chapter 1 ■ IntroduCIng aSp.net MVC 4 When to Use ASP.NET MVC ASP.NET MVC has certain capabilities that make it the best option to choose if you need one or more of the following: • A high level of control over the generated HTML: Unlike Web Forms, Views in ASP.NET MVC render HTML exactly as you tell them to. Recently, Web Forms have been improved in this area but still don’t have the level of control MVC has. • Easier unit testing: With ASP.NET MVC, it is very easy to follow testing patterns such as test-driven development (TDD). Because of the complex event lifecycle in Web Forms, on top of a control-based framework, TDD is a lot easier with MVC. • Separation of concerns: This refers to having all aspects of the system clearly separated from one another. Because of the pattern it implements, an MVC application is divided into discrete and loosely bound parts (model, views, and controllers), which makes it easy to maintain. ASP.NET MVC Benefits Compared to Web Forms, ASP.NET MVC applications benefit by including all ASP.NET core features but also by the features in the MVC pattern. Some of those benefits are: • The MVC pattern itself makes it easier to manage complexity by clearly separating the functionality of the application into three core parts, the model, the view, and the controller. • ASP.NET MVC web applications do not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application. View state can become very large, which is a problem for devices like smartphones running over slow networks (transmitting all that information can be very slow). In a Web Forms page, you could only have one |