Choosing an ASP.NET Project Template

Websites in Visual Studio start with a project template. The website project represents a connection between Visual Studio, the source code of your website, and a web server (be it local or otherwise). What the project template contains, however, continues to evolve and expand to support multiple web application scenarios.

Simple Hypertext Markup Language (HTML) sites with just text, hyperlinks, and a few images are rarely created anymore or even discussed seriously as websites. Instead, ASP.NET along with HTML 5, CSS 3, and various client-side JavaScript libraries have pushed the definition of website well beyond the original ASP (active server pages) model that combined HTML with some server-side script.

Today, we build websites (and web applications) that mimic user interactivity previously possible only with native code. This means client-side code interacting with compiled code on the server, database connectivity and binding, configurable user membership, responsive user interfaces, and so on. The Visual Studio 2015 tooling brings these concepts together to enable you to create these rich, modern web applications.


Web Application Versus Website Projects

Visual Studio enables you to create both websites (File, New, Web Site) and web application projects (File, New, Project). Both are represented by project templates that define default directories, configuration, pages, and other related files and settings. Both are nested in a solution. Both allow you to control properties and manage external references.

A website works well for developers who typically deploy their site as a set of files. A web application project should be used when you want to deploy compiled code and need specialized control over compilation and output assemblies. Presently, you can only create ASP.NET 5, MVC, and Web API sites as web applications. They are the preferred option for the majority of ASP.NET developers. Therefore, this chapter focuses on web application templates.


You create a new ASP.NET web application using the New Project dialog (File, New, Project). You start by selecting the Web node under your chosen language. From there, you select the template titled ASP.NET Web Application, given your project a name and location, and hit the OK button. This brings up the New ASP.NET Project dialog, as shown in Figure 17.2. It allows you to refine your ASP.NET project type selection. Notice the templates are grouped by those that target ASP.NET 4.6 and those that are built on ASP.NET 5. Of course, you pick a template based on your specific needs. The following list provides an overview of many of the ASP.NET web application templates.

Image

FIGURE 17.2 You can select from one of the many ASP.NET project templates in Visual Studio 2015.

The Empty Project Templates

There are two empty project templates shown in Figure 17.2 (one for ASP.NET 4.6 and one for ASP.NET 5). Like they sound, these templates are used to create projects that are free of the basic template files and configuration. The Empty template for ASP.NET 4.6 targets the full .NET framework and follows that approach for development. It contains a web.config file for managing configuration. You use the template to create applications based on MVC 5, Web Forms, or similar web development approaches from prior versions.

The ASP.NET 5 Empty template can target different versions of DNX (dnx451 for the full .NET Framework or dnxcore50 for the .NET Core 5 Framework). It includes the .json JSON style configuration files and the new package managers (see upcoming section), and it can be used to create MVC 6 or Web API projects.

Web Forms

The ASP.NET Web Forms template has been around for a long while and offers developers ease of use, a strong set of controls, and a rich framework to extend. You build Web Forms using HTML and the ASP.NET controls using markup and a design-time editor. Each file has a code-behind class that includes server-side code executed in response to events on the page (such as page load and form postback).

The template includes a directory for storing data (App_Data), a default page (Default.aspx), a configuration file (web.config), a master page (Site.master), a style sheet (Content/Site.css), a page for intercepting application events (Global.asax), code for managing which scripts are loaded (BundleConfig.cs), and more. The template also includes an Account directory that contains pages for managing login and user accounts. The jQuery library is also provided by default in this template (inside the Scripts folder). Of course, additional folders and files can be added as you build out your site.

Web Forms tends to abstract the basic HTML and JavaScript from the developer, leaving us to work with controls that do much of the work for us. In addition, Web Forms combines UI code with presentation, making it difficult to test your UI code or to get good reuse of many of your UI methods. ASP.NET MVC was created as an alternative to get developers back in control of their HTML (the view) and to separate this view from the actual logic (the controller) and the data (the model). As a result, MVC (and related client frameworks) has become the preferred choice of ASP.NET developers over Web Forms.


Note

Your Web Forms 4.6 applications will still run in Visual Studio 2015 without modifications. They do require the full .NET Framework/Runtime. Microsoft continues to evolve the technology. You will see new features including the implementation of HTTP 2 for better request handling, async model binding, and the Roslyn compilers for code-behind files.


Web Pages

The ASP.NET Web Pages technology and project template were created the same time Microsoft introduced the Razor syntax for working with server-side code and basic HTML in the same page. This is the way MVC sites work. However, with Web Pages, you do not need the complexities of the MVC pattern. Instead, you create a URL-addressable page as a .cshtml page (no controller or models required). You then write basic HTML (no server-side controls) and embed C# code directly within the page itself. You use the Razor syntax throughout your HTML to display variable values, do conditional checks, loop through collections, and similar activities.

The ASP.NET Web Pages with Razor syntax gives developers a simple, easy-to-learn model for creating web pages. By contrast, ASP.NET Web Forms is largely based on controls that render their HTML and JavaScript to the browser at runtime. This is feature-rich but also carries with it a steep learning curve and some heavyweight pages. Web Pages with Razor eliminates these controls in favor of just HTML (including HTML 5). You can still add server-side code to your page to affect how the HTML is written to the browser and to respond to user requests. The results are web pages that are easier to understand, a technology that is faster to learn, and a lightweight processing engine.

You create a Web Pages template in Visual Studio from the File, New, Website menu and then select ASP.NET Web Site (Razor v3) as your template. This template includes a few .cshtml pages to get you started. Like the MVC template, it also defines a master layout called _SiteLayout.cshtml. This page contains common elements for your page, such as navigation, style sheet links, jQuery inclusion script tags, and a footer. You then need only add your specific page content to the new pages you create.

We cover the details of the Razor syntax later in this chapter. These details apply to Web Pages and MVC sites.

MVC

The ASP.NET MVC template leverages the MVC design pattern to separate the view (your HTML), the model (your data), and the logic that combines data with the right view (the controller). This separation increases opportunities for code reuse, makes your code more understandable, and supports unit testing of controller code.

The MVC template is based on a prior versions of ASP.NET and thus includes a web.config file and is set to target the full .NET Framework. However, outside of configuration and package management, the majority of development concepts and programming techniques are the same between the MVC template of old and the new ASP.NET 5 template. We cover the latter in depth later in this chapter. However, much of what we cover is also applicable to the MVC template.

Web API

There are two Web API templates: one targeting ASP.NET 4.6 and the other for ASP.NET 5. Both templates allows you to easily create HTTP REST-based services. The 4.6 template requires the full .NET Framework and is backward compatible with prior versions of the framework.

Microsoft has unified the technologies of Web API and MVC within ASP.NET 5. Therefore, you can use the Web API 5 template or simply build HTTP services with one of the other ASP.NET 5 templates. We cover creating services with Web API in Chapter 19.

Single-Page Application (SPA)

The SPA template focuses on building a rich web client that uses HTML 5 and JavaScript to provide an interactive user experience. This template is based on the MVC template. However, it focuses on client-side libraries of Knockout, jQuery, Twitter Bootstrap, and others. It was created to help you build a site using these client frameworks, HTML 5, CSS 3, and JavaScript.

You can create a SPA using any of the templates; you need only include the right client-side libraries and be ready to write JavaScript. We cover more on building SPAs in Chapter 18.

ASP.NET 5 Web Site

The ASP.NET 5 Web Site is the new template for building ASP.NET 5 applications. This template is also focused squarely on the MVC pattern. However, it uses the new versions of the .NET Framework and includes tools for working with client-frameworks. That makes it a great candidate for a new website, Web API projects, and SPAs.

This template (along with the DNX and ASP.NET 5) is the focus of this chapter; thus, the primary details are covered within.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset