Exercise 4: Creating a View
So far you have been returning strings from controller actions. Although that’s a useful way of understanding how controllers work, it’s not how you’d want to build a real web application. Views are components that provide a better approach for generating HTML back to the browser with the use of template files.
In this exercise you will learn how to add a MasterPage to setup a template for common HTML content, a StyleSheet to enhance the look and feel of the site and finally a View template to enable HomeController to return HTML.
Task 1 – Adding a MasterPage
ASP.NET MasterPages are layout files that allow you to setup a template for common HTML to use across the entire website. In this task you will add a MasterPage with a common header with links to the Home page and Store area.
Did you know? Visual Studio 2010 has snippets that make it easy to add commonly used code in HTML, code files and more! Try it out by typing <div + tab (twice) to insert a complete div tag.
Task 2 – Adding CSS Stylesheet
The empty project template includes a very streamlined CSS file which just includes styles used to display validation messages. You will use additional CSS and images (potentially provided by a designer) in order to enhance the look and feel of the site.
In this task, you will add a CSS stylesheet to Site.Master to define the styles of the site.
Task 3 – Adding a View Template
In this task, you will add a View template to generate the HTML response that will use the Master Page and CSS added in this exercise.