Sie sind auf Seite 1von 572

MCT USE ONLY.

STUDENT USE PROHIBITED


O F F I C I A L M I C R O S O F T L E A R N I N G P R O D U C T

20486A
Developing ASP.NET MVC 4 Web
Applications
MCT USE ONLY. STUDENT USE PROHIBITED
ii Developing ASP.NET MVC 4 Web Applications

Information in this document, including URL and other Internet Web site references, is subject to change
without notice. Unless otherwise noted, the example companies, organizations, products, domain names,
e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with
any real company, organization, product, domain name, e-mail address, logo, person, place or event is
intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the
user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in
or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property
rights covering subject matter in this document. Except as expressly provided in any written license
agreement from Microsoft, the furnishing of this document does not give you any license to these
patents, trademarks, copyrights, or other intellectual property.

The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding
these manufacturers or the use of the products with any Microsoft technologies. The inclusion of a
manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product. Links
may be provided to third party sites. Such sites are not under the control of Microsoft and Microsoft is not
responsible for the contents of any linked site or any link contained in a linked site, or any changes or
updates to such sites. Microsoft is not responsible for webcasting or any other form of transmission
received from any linked site. Microsoft is providing these links to you only as a convenience, and the
inclusion of any link does not imply endorsement of Microsoft of the site or the products contained
therein.
© 2013 Microsoft Corporation. All rights reserved.

Microsoft and the trademarks listed at


http://www.microsoft.com/about/legal/en/us/IntellectualProperty/Trademarks/EN-US.aspx are trademarks
of the Microsoft group of companies. All other trademarks are property of their respective owners.

Product Number: 20486A

Part Number : X18-52161

Released: 03/2013
MCT USE ONLY. STUDENT USE PROHIBITED
MICROSOFT LICENSE TERMS
OFFICIAL MICROSOFT LEARNING PRODUCTS
MICROSOFT OFFICIAL COURSE Pre-Release and Final Release Versions

These license terms are an agreement between Microsoft Corporation and you. Please read them. They apply to
the Licensed Content named above, which includes the media on which you received it, if any. These license
terms also apply to any updates, supplements, internet based services and support services for the Licensed
Content, unless other terms accompany those items. If so, those terms apply.

BY DOWNLOADING OR USING THE LICENSED CONTENT, YOU ACCEPT THESE TERMS. IF YOU DO NOT ACCEPT
THEM, DO NOT DOWNLOAD OR USE THE LICENSED CONTENT.

If you comply with these license terms, you have the rights below.

1. DEFINITIONS.

a. “Authorized Learning Center” means a Microsoft Learning Competency Member, Microsoft IT Academy
Program Member, or such other entity as Microsoft may designate from time to time.

b. “Authorized Training Session” means the Microsoft-authorized instructor-led training class using only
MOC Courses that are conducted by a MCT at or through an Authorized Learning Center.

c. “Classroom Device” means one (1) dedicated, secure computer that you own or control that meets or
exceeds the hardware level specified for the particular MOC Course located at your training facilities or
primary business location.

d. “End User” means an individual who is (i) duly enrolled for an Authorized Training Session or Private
Training Session, (ii) an employee of a MPN Member, or (iii) a Microsoft full-time employee.

e. “Licensed Content” means the MOC Course and any other content accompanying this agreement.
Licensed Content may include (i) Trainer Content, (ii) software, and (iii) associated media.

f. “Microsoft Certified Trainer” or “MCT” means an individual who is (i) engaged to teach a training session
to End Users on behalf of an Authorized Learning Center or MPN Member, (ii) currently certified as a
Microsoft Certified Trainer under the Microsoft Certification Program, and (iii) holds a Microsoft
Certification in the technology that is the subject of the training session.

g. “Microsoft IT Academy Member” means a current, active member of the Microsoft IT Academy
Program.

h. “Microsoft Learning Competency Member” means a Microsoft Partner Network Program Member in
good standing that currently holds the Learning Competency status.

i. “Microsoft Official Course” or “MOC Course” means the Official Microsoft Learning Product instructor-
led courseware that educates IT professionals or developers on Microsoft technologies.
MCT USE ONLY. STUDENT USE PROHIBITED
j. “Microsoft Partner Network Member” or “MPN Member” means a silver or gold-level Microsoft Partner
Network program member in good standing.

k. “Personal Device” means one (1) device, workstation or other digital electronic device that you
personally own or control that meets or exceeds the hardware level specified for the particular MOC
Course.

l. “Private Training Session” means the instructor-led training classes provided by MPN Members for
corporate customers to teach a predefined learning objective. These classes are not advertised or
promoted to the general public and class attendance is restricted to individuals employed by or
contracted by the corporate customer.

m. “Trainer Content” means the trainer version of the MOC Course and additional content designated
solely for trainers to use to teach a training session using a MOC Course. Trainer Content may include
Microsoft PowerPoint presentations, instructor notes, lab setup guide, demonstration guides, beta
feedback form and trainer preparation guide for the MOC Course. To clarify, Trainer Content does not
include virtual hard disks or virtual machines.

2. INSTALLATION AND USE RIGHTS. The Licensed Content is licensed not sold. The Licensed Content is
licensed on a one copy per user basis, such that you must acquire a license for each individual that
accesses or uses the Licensed Content.

2.1 Below are four separate sets of installation and use rights. Only one set of rights apply to you.

a. If you are a Authorized Learning Center:


i. If the Licensed Content is in digital format for each license you acquire you may either:
1. install one (1) copy of the Licensed Content in the form provided to you on a dedicated, secure
server located on your premises where the Authorized Training Session is held for access and
use by one (1) End User attending the Authorized Training Session, or by one (1) MCT teaching
the Authorized Training Session, or
2. install one (1) copy of the Licensed Content in the form provided to you on one (1) Classroom
Device for access and use by one (1) End User attending the Authorized Training Session, or by
one (1) MCT teaching the Authorized Training Session.
ii. You agree that:
1. you will acquire a license for each End User and MCT that accesses the Licensed Content,
2. each End User and MCT will be presented with a copy of this agreement and each individual
will agree that their use of the Licensed Content will be subject to these license terms prior to
their accessing the Licensed Content. Each individual will be required to denote their
acceptance of the EULA in a manner that is enforceable under local law prior to their accessing
the Licensed Content,
3. for all Authorized Training Sessions, you will only use qualified MCTs who hold the applicable
competency to teach the particular MOC Course that is the subject of the training session,
4. you will not alter or remove any copyright or other protective notices contained in the
Licensed Content,
MCT USE ONLY. STUDENT USE PROHIBITED
5. you will remove and irretrievably delete all Licensed Content from all Classroom Devices and
servers at the end of the Authorized Training Session,
6. you will only provide access to the Licensed Content to End Users and MCTs,
7. you will only provide access to the Trainer Content to MCTs, and
8. any Licensed Content installed for use during a training session will be done in accordance
with the applicable classroom set-up guide.

b. If you are a MPN Member.


i. If the Licensed Content is in digital format for each license you acquire you may either:
1. install one (1) copy of the Licensed Content in the form provided to you on (A) one (1)
Classroom Device, or (B) one (1) dedicated, secure server located at your premises where
the training session is held for use by one (1) of your employees attending a training session
provided by you, or by one (1) MCT that is teaching the training session, or
2. install one (1) copy of the Licensed Content in the form provided to you on one (1)
Classroom Device for use by one (1) End User attending a Private Training Session, or one (1)
MCT that is teaching the Private Training Session.
ii. You agree that:
1. you will acquire a license for each End User and MCT that accesses the Licensed Content,
2. each End User and MCT will be presented with a copy of this agreement and each individual
will agree that their use of the Licensed Content will be subject to these license terms prior
to their accessing the Licensed Content. Each individual will be required to denote their
acceptance of the EULA in a manner that is enforceable under local law prior to their
accessing the Licensed Content,
3. for all training sessions, you will only use qualified MCTs who hold the applicable
competency to teach the particular MOC Course that is the subject of the training session,
4. you will not alter or remove any copyright or other protective notices contained in the
Licensed Content,
5. you will remove and irretrievably delete all Licensed Content from all Classroom Devices and
servers at the end of each training session,
6. you will only provide access to the Licensed Content to End Users and MCTs,
7. you will only provide access to the Trainer Content to MCTs, and
8. any Licensed Content installed for use during a training session will be done in accordance
with the applicable classroom set-up guide.

c. If you are an End User:


You may use the Licensed Content solely for your personal training use. If the Licensed Content is in
digital format, for each license you acquire you may (i) install one (1) copy of the Licensed Content in
the form provided to you on one (1) Personal Device and install another copy on another Personal
Device as a backup copy, which may be used only to reinstall the Licensed Content; or (ii) print one (1)
copy of the Licensed Content. You may not install or use a copy of the Licensed Content on a device
you do not own or control.
MCT USE ONLY. STUDENT USE PROHIBITED
d. If you are a MCT.
i. For each license you acquire, you may use the Licensed Content solely to prepare and deliver an
Authorized Training Session or Private Training Session. For each license you acquire, you may
install and use one (1) copy of the Licensed Content in the form provided to you on one (1) Personal
Device and install one (1) additional copy on another Personal Device as a backup copy, which may
be used only to reinstall the Licensed Content. You may not install or use a copy of the Licensed
Content on a device you do not own or control.

ii. Use of Instructional Components in Trainer Content. You may customize, in accordance with the
most recent version of the MCT Agreement, those portions of the Trainer Content that are logically
associated with instruction of a training session. If you elect to exercise the foregoing rights, you
agree: (a) that any of these customizations will only be used for providing a training session, (b) any
customizations will comply with the terms and conditions for Modified Training Sessions and
Supplemental Materials in the most recent version of the MCT agreement and with this agreement.
For clarity, any use of “customize” refers only to changing the order of slides and content, and/or
not using all the slides or content, it does not mean changing or modifying any slide or content.

2.2 Separation of Components. The Licensed Content components are licensed as a single unit and you
may not separate the components and install them on different devices.

2.3 Reproduction/Redistribution Licensed Content. Except as expressly provided in the applicable


installation and use rights above, you may not reproduce or distribute the Licensed Content or any portion
thereof (including any permitted modifications) to any third parties without the express written permission
of Microsoft.

2.4 Third Party Programs. The Licensed Content may contain third party programs or services. These
license terms will apply to your use of those third party programs or services, unless other terms accompany
those programs and services.

2.5 Additional Terms. Some Licensed Content may contain components with additional terms,
conditions, and licenses regarding its use. Any non-conflicting terms in those conditions and licenses also
apply to that respective component and supplements the terms described in this Agreement.

3. PRE-RELEASE VERSIONS. If the Licensed Content is a pre-release (“beta”) version, in addition to the other
provisions in this agreement, then these terms also apply:

a. Pre-Release Licensed Content. This Licensed Content is a pre-release version. It may not contain the
same information and/or work the way a final version of the Licensed Content will. We may change it
for the final version. We also may not release a final version. Microsoft is under no obligation to
provide you with any further content, including the final release version of the Licensed Content.

b. Feedback. If you agree to give feedback about the Licensed Content to Microsoft, either directly or
through its third party designee, you give to Microsoft without charge, the right to use, share and
commercialize your feedback in any way and for any purpose. You also give to third parties, without
charge, any patent rights needed for their products, technologies and services to use or interface with
any specific parts of a Microsoft software, Microsoft product, or service that includes the feedback. You
will not give feedback that is subject to a license that requires Microsoft to license its software,
technologies, or products to third parties because we include your feedback in them. These rights
MCT USE ONLY. STUDENT USE PROHIBITED
survive this agreement.

c. Term. If you are an Authorized Training Center, MCT or MPN, you agree to cease using all copies of the
beta version of the Licensed Content upon (i) the date which Microsoft informs you is the end date for
using the beta version, or (ii) sixty (60) days after the commercial release of the Licensed Content,
whichever is earliest (“beta term”). Upon expiration or termination of the beta term, you will
irretrievably delete and destroy all copies of same in the possession or under your control.

4. INTERNET-BASED SERVICES. Microsoft may provide Internet-based services with the Licensed Content,
which may change or be canceled at any time.

a. Consent for Internet-Based Services. The Licensed Content may connect to computer systems over an
Internet-based wireless network. In some cases, you will not receive a separate notice when they
connect. Using the Licensed Content operates as your consent to the transmission of standard device
information (including but not limited to technical information about your device, system and
application software, and peripherals) for internet-based services.

b. Misuse of Internet-based Services. You may not use any Internet-based service in any way that could
harm it or impair anyone else’s use of it. You may not use the service to try to gain unauthorized access
to any service, data, account or network by any means.

5. SCOPE OF LICENSE. The Licensed Content is licensed, not sold. This agreement only gives you some rights
to use the Licensed Content. Microsoft reserves all other rights. Unless applicable law gives you more
rights despite this limitation, you may use the Licensed Content only as expressly permitted in this
agreement. In doing so, you must comply with any technical limitations in the Licensed Content that only
allows you to use it in certain ways. Except as expressly permitted in this agreement, you may not:
• install more copies of the Licensed Content on devices than the number of licenses you acquired;
• allow more individuals to access the Licensed Content than the number of licenses you acquired;
• publicly display, or make the Licensed Content available for others to access or use;
• install, sell, publish, transmit, encumber, pledge, lend, copy, adapt, link to, post, rent, lease or lend,
make available or distribute the Licensed Content to any third party, except as expressly permitted
by this Agreement.
• reverse engineer, decompile, remove or otherwise thwart any protections or disassemble the
Licensed Content except and only to the extent that applicable law expressly permits, despite this
limitation;
• access or use any Licensed Content for which you are not providing a training session to End Users
using the Licensed Content;
• access or use any Licensed Content that you have not been authorized by Microsoft to access and
use; or
• transfer the Licensed Content, in whole or in part, or assign this agreement to any third party.

6. RESERVATION OF RIGHTS AND OWNERSHIP. Microsoft reserves all rights not expressly granted to you in
this agreement. The Licensed Content is protected by copyright and other intellectual property laws and
treaties. Microsoft or its suppliers own the title, copyright, and other intellectual property rights in the
Licensed Content. You may not remove or obscure any copyright, trademark or patent notices that
appear on the Licensed Content or any components thereof, as delivered to you.
MCT USE ONLY. STUDENT USE PROHIBITED
7. EXPORT RESTRICTIONS. The Licensed Content is subject to United States export laws and regulations. You
must comply with all domestic and international export laws and regulations that apply to the Licensed
Content. These laws include restrictions on destinations, End Users and end use. For additional
information, see www.microsoft.com/exporting.

8. LIMITATIONS ON SALE, RENTAL, ETC. AND CERTAIN ASSIGNMENTS. You may not sell, rent, lease, lend or
sublicense the Licensed Content or any portion thereof, or transfer or assign this agreement.

9. SUPPORT SERVICES. Because the Licensed Content is “as is”, we may not provide support services for it.

10. TERMINATION. Without prejudice to any other rights, Microsoft may terminate this agreement if you fail
to comply with the terms and conditions of this agreement. Upon any termination of this agreement, you
agree to immediately stop all use of and to irretrievable delete and destroy all copies of the Licensed
Content in your possession or under your control.

11. LINKS TO THIRD PARTY SITES. You may link to third party sites through the use of the Licensed Content.
The third party sites are not under the control of Microsoft, and Microsoft is not responsible for the
contents of any third party sites, any links contained in third party sites, or any changes or updates to third
party sites. Microsoft is not responsible for webcasting or any other form of transmission received from
any third party sites. Microsoft is providing these links to third party sites to you only as a convenience,
and the inclusion of any link does not imply an endorsement by Microsoft of the third party site.

12. ENTIRE AGREEMENT. This agreement, and the terms for supplements, updates and support services are
the entire agreement for the Licensed Content.

13. APPLICABLE LAW.


a. United States. If you acquired the Licensed Content in the United States, Washington state law governs
the interpretation of this agreement and applies to claims for breach of it, regardless of conflict of laws
principles. The laws of the state where you live govern all other claims, including claims under state
consumer protection laws, unfair competition laws, and in tort.

b. Outside the United States. If you acquired the Licensed Content in any other country, the laws of that
country apply.

14. LEGAL EFFECT. This agreement describes certain legal rights. You may have other rights under the laws of
your country. You may also have rights with respect to the party from whom you acquired the Licensed
Content. This agreement does not change your rights under the laws of your country if the laws of your
country do not permit it to do so.

15. DISCLAIMER OF WARRANTY. THE LICENSED CONTENT IS LICENSED "AS-IS," "WITH ALL FAULTS," AND "AS
AVAILABLE." YOU BEAR THE RISK OF USING IT. MICROSOFT CORPORATION AND ITS RESPECTIVE
AFFILIATES GIVE NO EXPRESS WARRANTIES, GUARANTEES, OR CONDITIONS UNDER OR IN RELATION TO
THE LICENSED CONTENT. YOU MAY HAVE ADDITIONAL CONSUMER RIGHTS UNDER YOUR LOCAL LAWS
WHICH THIS AGREEMENT CANNOT CHANGE. TO THE EXTENT PERMITTED UNDER YOUR LOCAL LAWS,
MICROSOFT CORPORATION AND ITS RESPECTIVE AFFILIATES EXCLUDE ANY IMPLIED WARRANTIES OR
CONDITIONS, INCLUDING THOSE OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NON-INFRINGEMENT.
MCT USE ONLY. STUDENT USE PROHIBITED
16. LIMITATION ON AND EXCLUSION OF REMEDIES AND DAMAGES. TO THE EXTENT NOT PROHIBITED BY
LAW, YOU CAN RECOVER FROM MICROSOFT CORPORATION AND ITS SUPPLIERS ONLY DIRECT
DAMAGES UP TO USD$5.00. YOU AGREE NOT TO SEEK TO RECOVER ANY OTHER DAMAGES, INCLUDING
CONSEQUENTIAL, LOST PROFITS, SPECIAL, INDIRECT OR INCIDENTAL DAMAGES FROM MICROSOFT
CORPORATION AND ITS RESPECTIVE SUPPLIERS.

This limitation applies to


o anything related to the Licensed Content, services made available through the Licensed Content, or
content (including code) on third party Internet sites or third-party programs; and
o claims for breach of contract, breach of warranty, guarantee or condition, strict liability, negligence,
or other tort to the extent permitted by applicable law.

It also applies even if Microsoft knew or should have known about the possibility of the damages. The
above limitation or exclusion may not apply to you because your country may not allow the exclusion or
limitation of incidental, consequential or other damages.

Please note: As this Licensed Content is distributed in Quebec, Canada, some of the clauses in this agreement
are provided below in French.

Remarque : Ce le contenu sous licence étant distribué au Québec, Canada, certaines des clauses dans ce
contrat sont fournies ci-dessous en français.

EXONÉRATION DE GARANTIE. Le contenu sous licence visé par une licence est offert « tel quel ». Toute
utilisation de ce contenu sous licence est à votre seule risque et péril. Microsoft n’accorde aucune autre garantie
expresse. Vous pouvez bénéficier de droits additionnels en vertu du droit local sur la protection dues
consommateurs, que ce contrat ne peut modifier. La ou elles sont permises par le droit locale, les garanties
implicites de qualité marchande, d’adéquation à un usage particulier et d’absence de contrefaçon sont exclues.

LIMITATION DES DOMMAGES-INTÉRÊTS ET EXCLUSION DE RESPONSABILITÉ POUR LES DOMMAGES. Vous


pouvez obtenir de Microsoft et de ses fournisseurs une indemnisation en cas de dommages directs uniquement
à hauteur de 5,00 $ US. Vous ne pouvez prétendre à aucune indemnisation pour les autres dommages, y
compris les dommages spéciaux, indirects ou accessoires et pertes de bénéfices.
Cette limitation concerne:
• tout ce qui est relié au le contenu sous licence , aux services ou au contenu (y compris le code)
figurant sur des sites Internet tiers ou dans des programmes tiers ; et
• les réclamations au titre de violation de contrat ou de garantie, ou au titre de responsabilité
stricte, de négligence ou d’une autre faute dans la limite autorisée par la loi en vigueur.

Elle s’applique également, même si Microsoft connaissait ou devrait connaître l’éventualité d’un tel dommage.
Si votre pays n’autorise pas l’exclusion ou la limitation de responsabilité pour les dommages indirects,
accessoires ou de quelque nature que ce soit, il se peut que la limitation ou l’exclusion ci-dessus ne s’appliquera
pas à votre égard.

EFFET JURIDIQUE. Le présent contrat décrit certains droits juridiques. Vous pourriez avoir d’autres droits prévus
par les lois de votre pays. Le présent contrat ne modifie pas les droits que vous confèrent les lois de votre pays
si celles-ci ne le permettent pas.

Revised December 2011


MCT USE ONLY. STUDENT USE PROHIBITED
x Developing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications xi

Acknowledgments
Microsoft Learning wants to acknowledge and thank the following for their contribution toward
developing this title. Their effort at various stages in the development has ensured that you have a good
classroom experience.

Lakshmi Kamal—Lead Content Developer


Lakshmi is a practice lead for learning design at Sify and has close to seven years of work experience in
instructional design and development for software-based and hardware–based training. Under Lakshmi’s
leadership from an instructional design perspective, the Sify learning design team has been able to
execute over 400 hours of instructor-led training and e-learning for Microsoft Learning on their products
and technologies such as Windows Server 2008, Microsoft Office SharePoint Server 2007, SQL Server 2008,
Office Communications Server 2007, Windows Vista, Visual Studio 2008, and Office Server servers. Lakshmi
has also been the Lead learning designer for projects for Dell, General Electric, Cisco, Oracle, Allianz, and
Standard Charted Bank.

Lakshmi is conversant with authoring and development tools such as MS Office, Adobe Captivate, Snagit,
and Articulate. She holds a Master of Philosophy degree and a Post Graduation degree in English.

Shireesha Seshanaath—Content Developer


Shireesha is a Senior Instructional Designer at Sify Technologies Ltd. She has over five and a half years of
work experience in instructional design and development. She has designed and developed several
information technology–based courses for Microsoft, General Electric, Dell, NIC, NCERT, ABRL and UIDAI.

Alistair Matthews—Subject Matter Expert


Alistair Mathews is a consultant with extensive and cutting-edge experience in Microsoft technologies,
Alistair has spent the last ten years developing with, consulting on, and communicating about both
Developer and IT Professional sides of SharePoint, Visual Studio, Active Directory, Exchange and Windows.
He architects and delivers custom SharePoint solutions in knowledge management, enterprise and web
content management. He now works with customers throughout Microsoft to document products and
develop learning materials of all types.

Paul Litwin—Technical Reviewer


Paul Litwin is a developer who specializes in ASP.NET, C#, SQL Server, SQL Server Reporting Services, and
related technologies. Paul is a Programming Manager with Fred Hutchinson Cancer Research Center in
Seattle. He is also the owner of Deep Training, a developer-owned training company that specializes in
.NET developer & SQL Server Reporting Services training.

Paul is also an author of a number of books and training material. These training contents are based on
ASP.NET, SQL Server Reporting Services, and Micrsoft Access. Currently, Paul is co-writing Agile ASP.NET
Unleashed along with Stephen Walther and Ruth Wather. Paul is the conference chair of Microsoft
ASP.NET Connections, a Microsoft MVP, and a member of the INETA Speakers Bureau. He is also a regular
presenter at a number of conferences, user groups, and code camps.
MCT USE ONLY. STUDENT USE PROHIBITED
xii Developing ASP.NET MVC 4 Web Applications

Contents
Module 1: Exploring ASP.NET MVC 4
Module Overview 01-1
Lesson 1: Overview of Microsoft Web Technologies 01-2
Lesson 2: Overview of ASP.NET 4.5 01-11
Lesson 3: Introduction to ASP.NET MVC 4 01-19
Lab: Exploring ASP.NET MVC 4 01-23
Module Review and Takeaways 01-30

Module 02: Designing ASP.NET MVC 4 Web Applications


Module Overview 02-1
Lesson 1: Planning in the Project Design Phase 02-2
Lesson 2: Designing Models, Controllers, and Views 02-15
Lab: Designing ASP.NET MVC 4 Web Applications 02-20
Module Review and Takeaways 02-24

Module 03: Developing ASP.NET MVC 4 Models


Module Overview 03-1
Lesson 1: Creating MVC Models 03-2
Lesson 2: Working with Data 03-11
Lab: Developing ASP.NET MVC 4 Models 03-20
Module Review and Takeaways 03-28

Module 4: Developing ASP.NET MVC 4 Controllers


Module Overview 04-1
Lesson 1: Writing Controllers and Actions 04-2
Lesson 2: Writing Action Filters 04-12
Lab: Developing ASP.NET MVC 4 Controllers 04-16
Module Review and Takeaways 04-23

Module 05: Developing ASP.NET MVC 4 Views


Module Overview 05-1
Lesson 1: Creating Views with Razor Syntax 05-2
Lesson 2: Using HTML Helpers 05-13
Lesson 3: Reusing Code in Views 05-21
Lab: Developing ASP.NET MVC 4 Views 05-25
Module Review and Takeaways 05-33
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications xiii

Module 06: Testing and Debugging ASP.NET MVC 4 Web Applications


Module Overview 06-1
Lesson 1: Unit Testing MVC Components 06-2
Lesson 2: Implementing an Exception Handling Strategy 06-14
Lab: Testing and Debugging ASP.NET MVC 4 Web Applications 06-23
Module Review and Takeaways 06-31

Module 07: Structuring ASP.NET MVC 4 Web Applications


Module Overview 07-1
Lesson 1: Analyzing Information Architecture 07-2
Lesson 2: Configuring Routes 07-8
Lesson 3: Creating a Navigation Structure 07-18
Lab: Structuring ASP.NET MVC 4 Web Applications 07-24
Module Review and Takeaways 07-30

Module 08: Applying Styles to ASP.NET MVC 4 Web Applications


Module Overview 08-1
Lesson 1: Using Layouts 08-2
Lesson 2: Applying CSS Styles to an MVC Application 08-5
Lesson 3: Creating an Adaptive User Interface 08-9
Lab: Applying Styles to MVC 4 Web Applications 08-15
Module Review and Takeaways 08-22

Module 09: Building Responsive Pages in ASP.NET MVC 4 Web Applications


Module Overview 09-1
Lesson 1: Using AJAX and Partial Page Updates 09-2
Lesson 2: Implementing a Caching Strategy 09-6
Lab: Building Responsive Pages in ASP.NET MVC 4 Web Applications 09-12
Module Review and Takeaways 09-19

Module 10: Using JavaScript and jQuery for Responsive MVC 4 Web Applications
Module Overview 10-1
Lesson 1: Rendering and Executing JavaScript Code 10-2
Lesson 2: Using jQuery and jQueryUI 10-7
Lab: Using JavaScript and jQuery for Responsive MVC 4
Web Applications 10-14
Module Review and Takeaways 10-19

Module 11: Controlling Access to ASP.NET MVC 4 Web Applications


Module Overview 11-1
Lesson 1: Implementing Authentication and Authorization 11-2
Lesson 2: Assigning Roles and Membership 11-7
MCT USE ONLY. STUDENT USE PROHIBITED
xiv Developing ASP.NET MVC 4 Web Applications

Lab: Controlling Access to ASP.NET MVC 4 Web Applications 11-13


Module Review and Takeaways 11-26

Module 12: Building a Resilient ASP.NET MVC 4 Web Application


Module Overview 12-1
Lesson 1: Developing Secure Sites 12-2
Lesson 2: State Management 12-8
Lab: Building a Resilient ASP.NET MVC 4 Web Application 12-14
Module Review and Takeaways 12-18

Module13: Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications
Module Overview 13-1
Lesson 1: Introducing Windows Azure 13-2
Lesson 2: Designing and Writing Windows Azure Services 13-6
Lesson 3: Diagnostic Logs 13-9
Lesson 4: Consuming Windows Azure Services in a Web Application 13-11
Lab: Using Windows Azure Web Services in ASP.NET MVC 4
Web Applications 13-14
Module Review and Takeaways 13-21

Module14: Implementing Web APIs in ASP.NET MVC 4 Web Applications


Module Overview 14-1
Lesson 1: Developing a Web API 14-2
Lesson 2: Calling a Web API from Mobile and Web Applications 14-11
Lab: Implementing APIs in ASP.NET MVC 4 Web Applications 14-14
Module Review and Takeaways 14-21

Module15: Handling Requests in ASP.NET MVC 4 Web Applications


Module Overview 15-1
Lesson 1: Using HTTP Modules and HTTP Handlers 15-2
Lesson 2: Using Web Sockets 15-6
Lab: Handling Requests in ASP.NET MVC 4 Web Applications 15-11
Module Review and Takeaways 15-17

Module16: Deploying ASP.NET MVC 4 Web Applications


Module Overview 16-1
Lesson 1: Deploying a Web Application 16-2
Lesson 2: Deploying an MVC 4 Web Application 16-7
Lab: Deploying ASP.NET MVC 4 Web Applications 16-11
Module Review and Takeaways 16-15
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications xv

Lab Answer Keys


Module 1 Lab: Exploring ASP.NET MVC 4 L01-1
Module 2 Lab: Designing ASP.NET MVC 4 Web Applications L02-1
Module 3 Lab: Developing ASP.NET MVC 4 Models L03-1
Module 4 Lab: Developing ASP.NET MVC 4 Controllers L04-1
Module 5 Lab: Developing ASP.NET MVC 4 Views L05-1
Module 6 Lab: Testing and Debugging ASP.NET MVC 4
Web Applications L06-1
Module 7 Lab: Structuring ASP.NET MVC 4 Web Applications L07-1
Module 8 Lab: Applying Styles to MVC 4 Web Applications L08-1
Module 9 Lab: Building Responsive Pages in ASP.NET MVC 4
Web Applications L09-1
Module 10 Lab: Using JavaScript and jQuery for Responsive MVC 4
Web Applications L10-1
Module11 Lab: Controlling Access to ASP.NET MVC 4
Web Applications L11-1
Module 12 Lab: Building a Resilient ASP.NET MVC 4 Web Application L12-1
Module 13 Lab: Using Windows Azure Web Services in ASP.NET MVC 4
Web Applications L13-1
Module14 Lab: Implementing APIs in ASP.NET MVC 4
Web Applications L14-1
Module 15 Lab: Handling Requests in ASP.NET MVC 4
Web Applications L15-1
Module 16 Lab: Deploying ASP.NET MVC 4 Web Applications L16-1
MCT USE ONLY. STUDENT USE PROHIBITED
xvi Developing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
About This Course i

About This Course


This section provides a brief description of the course, audience, suggested prerequisites, and course
objectives.

Course Description

This first release (‘A’) MOC version of course 20486A has been developed on RTM software.
Microsoft Learning will release a ‘B’ version of this course with enhanced PowerPoint slides, copy-
edited content, and Course Companion content on Microsoft Learning site.

In this 5-day course, the professional web developers will learn to develop advanced ASP.NET
MVC application using .NET Framework 4.5 tools and technologies. The focus will be on coding
activities that enhance the performance and scalability of the Web site application. This course
will also prepare the student for exam 70-486.

Audience

This course is intended for professional web developers who use Microsoft Visual Studio in an individual-
based or team-based, small-sized to large development environment. Candidates for this course are
interested in developing advanced web applications and want to manage the rendered HTML
comprehensively.

Student Prerequisites
This course requires that you meet the following prerequisites:

 A minimum of two to three years of experience in developing web-based applications by using


Microsoft Visual Studio and Microsoft ASP.NET.
 Proficiency in using the .NET Framework and some familiarity with the C# language.

Students who attend this training can meet the prerequisites by attending the following courses or by
obtaining equivalent knowledge and skills:
 20483A: Programming in C#

 10958A: Programming Fundamentals of Web Applications

Course Objectives

After completing this course, students will be able to:

 Describe the Microsoft Web Technologies stack and select an appropriate technology to use to
develop any given application.
 Design the architecture and implementation of a web application that will meet a set of functional
requirements, user interface requirements, and address business models.
MCT USE ONLY. STUDENT USE PROHIBITED
ii About This Course

 Create MVC Models and write code that implements business logic within Model methods,
properties, and events.
 Add Controllers to an MVC Application to manage user interaction, update models, and select and
return Views.
 Create Views in an MVC application that display and edit data and interact with Models and
Controllers.
 Run unit tests and debugging tools against a web application in Visual Studio 2012.
 Develop a web application that uses the ASP.NET routing engine to present friendly URLs and a
logical navigation hierarchy to users.
 Implement a consistent look and feel, including corporate branding, across an entire MVC web
application.
 Use partial page updates and caching to reduce the network bandwidth used by an application and
accelerate responses to user requests.
 Write JavaScript code that runs on the client-side and utilizes the jQuery script library to optimize the
responsiveness of an MVC web application.
 Implement a complete membership system in an MVC 4 web application.
 Build an MVC application that resists malicious attacks and persists information about users and
preferences.
 Describe how to write a Windows Azure web service and call it from and MVC application.
 Describe what a Web API is and why developers might add a Web API to an application.
 Modify the way browser requests are handled by an MVC application.
 Describe how to package and deploy an ASP.NET MVC 4 web application from a development
computer to a web server for staging or production.

Course Outline
The course outline is as follows:
Module 1, “Exploring ASP.NET MVC 4", provides an overview of the Microsoft web technologies
stack. It also describes the three programming models available in ASP.NET 4.5 such as web pages,
web forms, and MVC.

Module 2, “Designing ASP.NET MVC 4 Web Applications", explains the planning of the overall
architecture, controllers, views, and models of the MVC 4 web application.

Module 3, “Developing ASP.NET MVC 4 Models", explains how to create Models in an MVC 4 web
application. The module also describes how to implement a connection to a database using the Entity
Framework.

Module 4, “Developing ASP.NET MVC 4 Controllers", explains how to create Controllers in an MVC 4
web application. The module also describes how to write code in action filters that executes before or
after a controller action.

Module 5, “Developing ASP.NET MVC 4 Views", explains how to create Views in an MVC 4
application that display and edit data and interact with Models and Controllers.

Module 6, “Testing, Debugging, and Troubleshooting ASP.NET MVC 4 Web Applications", explains
how to unit test the components of an MVC 4 application and to implement exception handling
strategy for the application.

Module 7, “Structuring ASP.NET MVC 4 Web Applications", explains how to structure an MVC 4 web
application that uses routing engine and navigation controls.

Module 8, “Applying Styles to ASP.NET MVC 4 Web Applications", explains how to implement a
consistent look and feel to an MVC application by using a template view.
MCT USE ONLY. STUDENT USE PROHIBITED
About This Course iii

Module 9, “Building Responsive Pages in ASP.NET MVC 4 Web Applications" explains how to use
partial page updates and caching to build responsive pages in an MVC 4 web application.

Module 10, “Using JavaScript and jQuery for Responsive MVC 4 Web Applications", explains how to
increase the responsiveness of the MVC 4 web application by using JavaScript and jQuery.

Module 11, “Controlling Access to ASP.NET MVC 4 Web Applications", explains how to implement
authentication and authorization for accessing the MVC 4 web application.

Module 12, “Building a Resilient ASP.NET MVC 4 Web Application", explains how to build an MVC 4
web application that is stable, reliable, resists malicious attacks, and persists information about users
and preferences.

Module 13, “Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications", provides an
overview on Windows Azure and also explains how to design and write a Windows Azure service.

Module 14, “Implementing Web APIs in ASP.NET MVC 4 Web Applications", provides an overview of
Web API and also explains how to develop a Web API and call it from other applications.

Module 15, “Handling Requests in ASP.NET MVC 4 Web Applications", describes how to create HTTP
Modules and HTTP Handlers to handle requests in an MVC 4 web application. The module also
describes the use of Web Sockets in creating a live data feed in the MVC 4 web application.

Module 16, “Deploying ASP.NET MVC 4 Web Applications", explains how to deploy a completed
MVC application to a web server or Windows Azure.

Course Materials
The following materials are included with your kit:

 Course Handbook: A succinct classroom learning guide that provides all the critical technical
information in a crisp, tightly-focused format, which is essential for an effective in-class learning
experience.

 Lessons: Guide you through the learning objectives and provide the key points that are critical to
the success of the in-class learning experience.

 Labs: Provide a real-world, hands-on platform for you to apply the knowledge and skills learned
in the module.
 Module Reviews and Takeaways: Provide on-the-job reference material to boost knowledge
and skills retention.

 Lab Answer Keys: Provide step-by-step lab solution guidance.

Course Companion Content: A searchable and easy-to-browse digital content with integrated
premium online resources that supplement the Course Handbook.

 Modules: Include companion content, such as questions and answers, detailed demo steps and
additional reading links, for each lesson. Additionally, they include Lab Review questions and
answers and Module Reviews and Takeaways sections, which contain the review questions and
answers, best practices, common issues and troubleshooting tips with answers, and real-world
issues and scenarios with answers.
MCT USE ONLY. STUDENT USE PROHIBITED
iv About This Course

 Resources: Include well-categorized additional resources that give you immediate access to the
most current premium content on TechNet, MSDN®, or Microsoft® Press®.

Note: For this version of the Courseware on Prerelease Software (specify RC0/Beta etc.),
Companion Content is not available. However, the Companion Content will be published when
the next (B) version of this course is released, and students who have taken this course will be
able to download the Companion Content at that time from the
http://www.microsoft.com/learning/companionmoc site. Please check with your instructor
when the ‘B’ version of this course is scheduled to release to learn when you can access
Companion Content for this course.

Student Course files: Includes the Allfiles.exe, a self-extracting executable file that contains all
required files for the labs and demonstrations.

Note: For this version of the Courseware on Prerelease Software (specify RC0/Beta etc.), Allfiles.exe
file is not available. However, this file will be published when the next (B) version of this course is
released, and students who have taken this course will be able to download the Allfiles.exe at that
time from the http://www.microsoft.com/learning/companionmoc site.

 Course evaluation: At the end of the course, you will have the opportunity to complete an online
evaluation to provide feedback on the course, training facility, and instructor.

 To provide additional comments or feedback on the course, send an email to


support@mscourseware.com. To inquire about the Microsoft Certification Program, send an
email to mcphelp@microsoft.com.
MCT USE ONLY. STUDENT USE PROHIBITED
About This Course v

Virtual Machine Environment


This section provides the information for setting up the classroom environment to support the business
scenario of the course.

Virtual Machine Configuration


In this course, you will use Microsoft® Hyper-V™ to perform the labs.

Important: At the end of each lab, you must revert the virtual machines to a snapshot.
You can find the instructions for this procedure at the end of each lab.

The following table shows the role of each virtual machine that is used in this course:

Virtual machine Role


20486A-SEA-DEV11 Windows 8 client computer

MSL-TMG1 Microsoft Forefront® Threat Management


Gateway (TMG) virtual machine

Software Configuration
The following software is installed on each virtual machine:
 Windows 8

 Visual Studio 2012 Ultimate

 Microsoft Office 2010 Professional Plus

 Microsoft Visio Premium 2010

Course Files
The files associated with the labs in this course are located in the C:\Program Files\Microsoft
Learning\20486\Drives\20486A-SEA-DEV11 folder on the student computers.

Classroom Setup
Each classroom computer will have the same virtual machine configured in the same way.

Course Hardware Level


To ensure a satisfactory student experience, Microsoft Learning requires a minimum equipment
configuration for trainer and student computers in all Microsoft Certified Partner for Learning Solutions
(CPLS) classrooms in which Official Microsoft Learning Product courseware is taught.

Hardware Level 7

 64 bit Intel Virtualization Technology (Intel VT) or AMD Virtualization (AMD-V) processor (2.8 Ghz
dual core or better recommended)

 Dual 500 GB hard disks 7200 RPM SATA or faster (striped)

 16 GB RAM

 DVD (dual layer recommended)


MCT USE ONLY. STUDENT USE PROHIBITED
vi About This Course

 Network adapter with Internet connectivity

 Dual SVGA monitors 17” or larger supporting 1440X900 minimum resolution

 Video adapter that supports 1440 x 900 resolution

 Microsoft Mouse or compatible pointing device

 Sound card with amplified speakers

In addition, the instructor computer must be connected to a projection display device that supports
1280X1024 pixels, 16 bit colors.
MCT USE ONLY. STUDENT USE PROHIBITED
01-1

Module01
Exploring ASP.NET MVC 4
Contents:
Module Overview 01-1 

Lesson 1: Overview of Microsoft Web Technologies 01-2 

Lesson 2: Overview of ASP.NET 4.5 01-11 

Lesson 3: Introduction to ASP.NET MVC 4 01-19 

Lab: Exploring ASP.NET MVC 4 01-23 

Module Review and Takeaways 01-30 

Module Overview
Microsoft ASP.NET MVC 4 and the other web technologies of the .NET Framework help you create and
host dynamic, powerful, and extensible web applications. ASP.NET MVC 4 supports agile, test-driven
development and the latest web standards such as HTML 5. To build robust web applications, you need to
be familiar with the technologies and products in the Microsoft web stack. You also need to know how
ASP.NET applications work with IIS, Visual Studio, SQL Server, Windows Azure, and Windows Azure SQL
Database to deliver engaging webpages to site visitors. To choose a programming language that best
suits a set of business requirements, you need to know how Model-View-Controller (MVC) applications
differ from the other ASP.NET programming models: Web Pages and Web Forms.

The web application that you will design and develop in the labs throughout this course will help you
develop web applications that fulfill business needs.

Objectives
After completing this module, you will be able to:

o Describe the role of ASP.NET in the web technologies stack, and how to use ASP.NET to build
web applications.

o Describe the three programming models available in ASP.NET: Web Pages, Web Forms, and
MVC—and select an appropriate model for a given project.

o Distinguish between an MVC Model, an MVC Controller, and an MVC View.


MCT USE ONLY. STUDENT USE PROHIBITED
01-2 Exploring ASP.NET MVC 4

Lesson 1
Overview of Microsoft Web Technologies
Before you use ASP.NET MVC 4, you need to know where Microsoft ASP.NET 4.5 fits in the context of the
entire Microsoft web technologies stack. You should know how ASP.NET 4.5 websites are hosted in
Internet Information Server 8 (IIS) and Windows Azure and how they run server-side code on web servers,
and client-side code on web browsers, to help provide rich and compelling content.

Lesson Objectives
After completing this lesson, you will be able to:

o Provide an overview of Microsoft web technologies.

o Provide an overview of ASP.NET 4.5.

o Provide an overview of client-side web technologies, including AJAX and JavaScript libraries.

o Describe the role of IIS in the Microsoft web technologies stack.


o Describe the role of Windows Azure in the Microsoft web technologies stack.

Introduction to Microsoft Web Technologies


Microsoft has a broad range of technologies that
enable you to create rich web sites and publish
them on intranets and the Internet. In this topic,
you will see technologies for developing and
hosting web sites, as well and technologies that
execute code, both on the web server and on the
user’s browser.

Microsoft provides a broad range of technologies


that you can use to create rich web applications
and publish them on intranets and over the
Internet. Besides publishing web applications, you
can use these technologies to develop and host
websites, which run code both on the web server, and on the web browser.

Developer Tools
You can create simple websites that have text and images by using a text editor, such as Notepad.
However, most websites require complex actions to be performed on the server-side, such as database
operations, email delivery, complex calculations, or graphics rendering. To create such complex, highly
functional, and engaging websites quickly and easily, Microsoft provides the following tools:

o WebMatrix 2. You can use WebMatrix 2 to create static HTML pages and dynamic pages with
ASP.NET, PHP, and Node.js. WebMatrix 2is a free development tool that you can install by
downloading and using the Microsoft Web Platform Installer (Web PI) from the Microsoft
website. WebMatrix2 enables you to develop custom websites based on popular web
applications such as Orchard, Umbraco CMS, and WordPress. Using WebMatrix 2, you can create
ASP.NET Web Pages applications, but not ASP.NET Web Forms or MVC applications.

o Microsoft Visual Studio 2012. You can use Visual Studio 2012, an Integrated Development
Environment (IDE), to create custom applications based on Microsoft technologies, regardless of
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-3

whether these applications run on the web, on desktops, on mobile devices, or by using
Microsoft cloud services. Visual Studio 2012has rich facilities for designing, coding, and
debugging any ASP.NET web application, including MVC applications.

o Microsoft Visual Studio Express 2012 for Web. You can use Visual Studio Express 2012 for Web to
create Web Forms or MVC web applications. This is a free tool that does not include all the
features of Visual Studio 2012 editions. However, you can use it to create fully functional MVC
websites.

Hosting Technologies
Regardless of the tool you use to build a web application, you must use a web server to host the web
application. When users visit your site, the host server responds by rendering HTML and returning it to the
user’s browser for display. The host server may query a database before it builds the HTML, and the host
server may perform other actions such as sending emails or saving uploaded files. You need to test the
functionality of such user actions on a web server. When you build a web application by using Visual
Studio 2012, you can use Visual Studio Development Server, the built-in web server to run the application.
However, Visual Studio Development Server cannot host deployed web applications. Therefore, when you
finish building the web application and it is ready for users to access it on an intranet or over the Internet,
you must use a fully functional web server such as:

o Microsoft Internet Information Server 8. IIS is an advanced website hosting technology. You can
install IIS servers on your local network or perimeter network or employ IIS servers hosted by an
Internet service provider (ISP). IIS can host any ASP.NET, PHP, or Node.js websites. You can scale
up IIS to host large and busy websites by configuring server farms that contain multiple IIS
servers, all serving the same content.

o Windows Azure. Windows Azure is a cloud platform that provides on-demand services to build,
deploy, host, and manage web applications through Microsoft-managed data centers. When you
use Windows Azure services, you need to pay only for the data that your website serves. Also, you
need not worry about building a scalable infrastructure because Windows Azure automatically
adds resources as your website grows.

Note: There are many other non-Microsoft technologies that you can use to host web sites.
The most popular, for example, is Apache. Apache is most frequently run on Linux servers and it
often paired with MySQL to manage databases and PHP as a server-based web framework. This
configuration is often referred to as the Linux Apache MySQL PHP (LAMP) web stack.
Note: Windows Azure and IIS can host websites written in ASP.NET, PHP, or Node.js.
Apache web servers can be used to host a number of server-side web technologies, including
PHP, Node.js, and Python, but Apache web servers cannot be used to host ASP.NET websites.

Most websites require a database to manage data such as product details, user information, and
discussion topics. You can choose from the following Microsoft technologies to manage your data:

o Microsoft SQL Server 2012. SQL Server 2012 is a premium database server that you can use to
host any database from the simplest to the most complex. SQL Server can scale up to support
very large databases and very large numbers of users. You can build large SQL Server clusters to
ensure the best availability and reliability. Many of the world’s largest organizations rely of SQL
Server to host data.

o Windows Azure SQL Database. SQL Database is a cloud database platform and a part of Windows
Azure. Using SQL Database, you can deploy your database and pay only for the data that you
use. You need not worry about managing your database infrastructure because your database
scales up automatically as your website grows.
MCT USE ONLY. STUDENT USE PROHIBITED
01-4 Exploring ASP.NET MVC 4

Code Execution Technologies


The code that you write in a developer tool must run in one of two locations:

o On the web server. This code has full access to the power of the web server and any databases
attached to it. It can access database quickly, send emails, and render web pages.

o On the user’s web browser. This code responds quickly to user actions, such as mouse clicks but is
more limited it what it can accomplish without interacting with the web server.

You need to use different technologies to run server-side code and client-side code.

Server-Side Execution
Microsoft ASP.NET 4.5 is a server-side web environment that runs server-side .NET code that you write in
Visual Studio 2012 or WebMatrix 2. The code accesses the database, renders HTML pages, and returns
them to the web browser. The MVC programming model is a part of ASP.NET 4.5. Other server-side
technologies include PHP and Node.js.

Client-Side Execution
Most web browsers can run code written in the JavaScript language. This code is sent to the browser as
text within a rendered HTML page or in a separate .js file. Because JavaScript is local to the browser, it can
respond very quickly to user actions such as clicking, pointing, or dragging.

Many JavaScript libraries are available to accelerate client code development. For example, the popular
jQuery library makes it simple to access page elements and manipulate them by changing their style or
content. When you use such libraries, you can write functions in a few lines that would otherwise require
hundreds of lines of your own JavaScript code.
ASP.NET applications can also use the Asynchronous JavaScript and XML (AJAX) technology on the client
computer. You can use AJAX to update a small section of an HTML page, instead of reloading the entire
page from the server. Such partial page updates help make web pages more responsive and engaging.
If you want to animate a page element, for example by fading it in, would you write server-side or client-
side code?

Overview of ASP.NET 4.5


You can use ASP.NET 4.5 to develop database-
driven, highly-functional, and scalable dynamic
websites that use client-side and server-side code.
You can create many kinds of website with
ASP.NET 4.5, for example, web portals, online
shopping sites, blogs, and wikis.

Programming Models
When you use ASP.NET 4.5 to build an
application, you are not restricted to a single style
of programming; instead, you can choose from
three different programming models. Each
programming model has a typical structure in the
development environment and stores code in different places in the web hierarchy:

o Web Pages: When you build a site by using Web Pages, you can write code by using the C# or
Visual Basic programming language. If you write C# code, these pages have a .cshtml file
extension. If you write Visual Basic code, these pages have a .vbhtml file extension. ASP.NET runs
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-5

the code in these pages on the server to render data from a database, respond to a form post, or
perform other actions. This programming model is simple and easy to learn, and is suited for
simple data-driven sites. ASP.NET 4.5 includes Web Pages version 2.0.

o Web Forms: When you build a site by using Web Forms, you employ a programming model with
rich server-side controls and a page lifecycle that is not unlike building desktop applications.
Built-in controls include buttons, text boxes, and grid views for displaying tabulated data. You
can also add third-party controls or build custom controls. To respond to user actions, you can
attach event handlers containing code to the server-side controls. For example, to respond to a
click on a button called firstButton, you could write code in the firstButton_Click() event
handler.

o MVC: When you build a site by using ASP.NET MVC, you separate server-side code into three
parts:
 Model: An MVC model defines a set of classes that represent the object types that the web
application manages. For example, the model for an ecommerce site might include a Product
model class that defines properties such as Description, Catalog Number, Price, and others.
Models often include data access logic that reads data from, and writes data to, a database.
 Controllers: An MVC controller is a class that handles user interaction, creates and modifies
model classes, and selects appropriate views. For example, when a user requests full details
about a particular product, the controller creates a new instance of the Product model class
and passes it to the Details view, which displays it to the user.
 Views: An MVC view is a component that builds the web pages that make up the web
application’s user interface. Controllers often pass an instance of a model class to a view. The
view displays properties of the model class. For example, if the controller passes a Product
object, the view might display the name of the product, a picture, and its price.
This separation of model, view, and controller code ensures that MVC applications have a logical
structure, even for the most complex sites. It also improves the testability of the application. Ultimately,
ASP.NET MVC enables more control over the generated HTML than either Web Pages or Web Forms.

Additional Reading: In Lesson 2, Understanding ASP.NET 4.5, you will see how to choose
the most appropriate programming model for a given website project.

The ASP.NET API


Whichever programming model you choose, you have access to classes from the ASP.NET API. These
classes are included in the .NET Framework in namespaces within the System.Webnamespace and can be
used to rapidly implement common website functionalities such as:

o Configuration. Using web.config files, you can configure your web application, regardless of the
programming model. Web.config files are XML files with specific tags and attributes that the
ASP.NET 4.5 runtime recognizes. For example, you can configure database connections and
custom error pages in the web.config file. In code, you can access the configuration through the
System.Web.Configuration namespace.

o Authentication and Authorization. Many websites require users to log on by entering a user name
and password, or by providing extra information. You can use ASP.NET membership providers to
authenticate and authorize users and restrict access to content. You can also build pages that
enable users to register a new account, reset a password, recover a lost password, or perform
other account management tasks. Membership providers belong to the System.Web.Security
namespace.
MCT USE ONLY. STUDENT USE PROHIBITED
01-6 Exploring ASP.NET MVC 4

o Caching. ASP.NET may take some time to render a complex webpage that may require multiple
database queries or calls to external web services. You can use caching to mitigate this delay.
ASP.NET caches a rendered page in memory, so that it can return the same page to subsequent
user requests without having to render it again from the start. In a similar manner, .NET
Framework objects can also be cached. You can access cached pages by using the
System.Runtime.Caching namespace and configure the caches in web.config.

Compiling ASP.NET Code


Because ASP.NET server-side code uses the .NET Framework, you must write code in a .NET managed
programming language such as C# or Visual Basic. Before running the code, it must be compiled into
native code so that the server CPU can process it. This is a two-stage process:

1. Compilation to MSIL. When you build a website in Visual Studio, the ASP.NET compiler creates .dll files
with all the code compiled into Microsoft Intermediate Language (MSIL). This code is both
independent of the language you used to write the application and the CPU architecture of the
server.
2. Compilation to native code. When a page is requested for the first time, the Common Language
Runtime (CLR) compiles MSIL into native code for the server CPU.

This two-stage compilation process enables components written in different languages to work together
and allows many errors to be detected at build time. Note, however, that pages may take extra time to
render the first time they are requested after a server restart. To avoid this delay, you can pre-compile the
website.

Additional Reading: For more information about ASP.NET compilation, see:


http://msdn.microsoft.com/en-us/library/ms178466.aspx

Which of the three programming models do you think provides the most control over the HTML and
JavaScript code that is sent to the browser?

Client-Side Web Technologies


Originally, in ASP.NET, and similar technologies
like PHP, all the code ran on the web server. This
approach is often practical because the web server
usually has immediate access to the database and
more processor power and memory than a typical
client computer. However, in such an approach,
every user action requires a round trip between
the client and the web server, and most actions
require a complete reload of the page. This can
take significant time. To respond quickly and
provide a better user experience, you can
supplement server-side code with client-side code
that runs in the web browser.

JavaScript
JavaScript is a simple scripting language that has syntax similar to C#, and is supported by most web
browsers. A scripting language is not compiled. Instead, a script engine interprets the code at runtime so
that the web browser can run the code.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-7

Note: Besides JavaScript, Internet Explorer supports VBScript. There are other scripting
languages as well but JavaScript is supported by virtually every web browser. This is not true of
any other scripting language. Unless your target audience is very limited and you have control
over the browser used by your users, you should use JavaScript because of its almost universal
support.

You can include JavaScript code in your ASP.NET pages, irrespective of the programming model you
choose. JavaScript is a powerful language, but can require many lines of code to achieve visual effects or
call external services. Script libraries contain pre-built JavaScript functions that help implement common
actions that you might want to perform on client-side code. You can use a script library, instead of
building all your own JavaScript code from the start; using a script library helps reduce development time
and effort.

jQuery
jQuery is one of the most popular JavaScript libraries.It provides elegant functions for interacting with the
HTML elements on your page and with cascading style sheet (CSS) styles. For example, you can locate all
the <div> elements on a webpage and change their background color by using a single line of code. To
achieve the same result by using JavaScript only, you need to write several lines of code and a
programming loop. Furthermore, the code you write may be different for different browsers. Using
jQuery, it is easier to write code to respond to user actions and to create simple animations. jQuery also
handles browser differences. You can use jQuery to call web services on remote computers and update
the webpage with the results returned.

The jQuery project also includes two other JavaScript libraries that extend the base jQuery library:

o jQuery UI. This library includes a set of widgets that you can use to help build a user interface.
There are date pickers, spinners, dialog boxes, autocomplete text boxes, and other widgets. You
can also apply themes to your jQuery UI widgets to integrate their colors and styles with the
website branding.
o jQuery Mobile: This library makes it easy to provide a user interface to mobile devices such as
phones and tablets. It renders HTML by using progressive enhancement. For example, using
jQuery Mobile, you can display rich, advanced controls on advanced mobile devices such as
smartphones with large screens, whereas you can display a simpler UI with the same functionality
to older mobile devices with smaller screens.

Note: There are many other JavaScript libraries such as Prototype, Enyo, Ext, and Dojo
Toolkit. If you find any of these better suited to a particular task, or if you have experience
developing web applications by using them, you can include them in your ASP.NET pages,
instead of jQuery.

AJAX
AJAX is a technology that enables browsers to communicate with web servers asynchronously using the
XmlHttpRequest object without completely refreshing the page. You can use AJAX in a page to update a
portion of the page with new data, without reloading the entire page. For example, you could use AJAX to
obtain the latest comments on a product, without refreshing the entire product page.

AJAX is an abbreviation of Asynchronous JavaScript and XML. AJAX is implemented entirely in JavaScript,
and ASP.NET 4.5, by default, relies on the jQuery library to manage AJAX requests and responses. The
code is run asynchronously, which means that the web browser does not freeze while it waits for an AJAX
response from the server. Initially, developers used XML to format the data returned from the server. More
recently, however, developers use JavaScript Object Notation (JSON) as an alternative format to XML.
MCT USE ONLY. STUDENT USE PROHIBITED
01-8 Exploring ASP.NET MVC 4

Can you think of any problems that might arise if you include the jQuery library with every page in your
application?

Internet Information Server 8.0


Every website must be hosted by a web server. A
web server receives requests for web content from
browsers, runs any server-side code, and returns
webpages, images, and other content. HTTP is
used for communication between the web
browser and the web server.

Internet Information Server 8.0 is a web server


that can scale up from a small website running on
a single web server to a large website running on
a multiple web server farm. Internet Information
Server 8.0 is available with Windows Server 2012.

Internet Information Server 8.0 Features


Internet Information Server 8.0 is tightly integrated with ASP.NET 4.5, Visual Studio 2012, and Windows
Server 2012. It includes the following features:

o Deployment Protocols. The advanced Web Deploy protocol, which is built into Visual Studio 2012,
automatically manages the deployment of a website with all its dependencies. Alternatively, you
can use FTP to deploy content.

o Centralized Web Farm Management. When you run a large website, you can configure a load-
balanced farm of many IIS servers to scale to large sizes. IIS management tools make it easy to
deploy sites to all servers in the farm and manage sites after deployment.

o High Performance Caches. You can configure ASP.NET to make optimal use of the IIS caches to
accelerate responses to user requests. When IIS serves a page or other content, it can cache it in
memory so that subsequent identical requests can be served faster.

o Authentication and Security. IIS supports most common standards for authentication, including
Smart Card authentication and Integrated Windows authentication. You can also use SSL to
encrypt security-sensitive communications, such as login pages and pages containing credit card
numbers.

o ASP.NET Support. IIS is the only web server that fully supports ASP.NET.

o Other Server-Side Technologies. You can host websites developed in PHP and Node.js on IIS.

Scaling Up IIS
A single web server has limited scalability because it is limited by its processor speed, memory, disk speed,
and other factors. Furthermore, single web servers are vulnerable to hardware failures and outages. For
example, when a single web server is offline, your website is unavailable to users.

You can improve the scalability and resilience of your website by hosting it on a multiple server farm. In
such server farms, many servers share the same server name. Therefore, all servers can respond to browser
requests. A load balancing system such as Windows Network Load Balancing or a hardware-based system
such as Riverbed Cascade, distributes requests evenly among the servers in the server farm. If a server fails,
other servers are available to respond to requests, and thereby, the website availability is not interrupted.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-9

IIS servers are designed to work in such server farms and include advanced management tools for
deploying sites and managing member servers.

Perimeter Networks
Web servers, including IIS, are often located on a perimeter network. A perimeter network has a network
segment that is protected from the Internet through a firewall that validates and permits incoming HTTP
requests. A second firewall, which permits requests only from the web server, separates the perimeter
network from the internal organizational network. Supporting servers, such as database servers, are
usually located on the internal organizational network.
In this configuration, Internet users can reach the IIS server to request pages and the IIS server can reach
the database server to run queries. However, Internet users cannot access the database server or any other
internal computer directly. This prevents malicious users from running many attacks and ensures a high
level of security.

IIS Express
Internet Information Server 8.0 Express does not provide all the features of Internet Information Server 8.0
on Windows Server 2012. For example, you cannot create load-balanced server farms by using Internet
Information Server 8.0 Express. However, it has all the features necessary to host rich ASP.NET 4.5 websites
and other websites on a single server. Internet Information Server 8.0 Express is included with Windows 8.
You can also install it on Windows Server 2012, Windows Server 2008, Windows 7, and Windows Vista SP1
and later by downloading and using the Web Platform Installer (Web PI).

Other Web Servers


Apache is a popular non-Microsoft web server and there are other alternatives such as nginx. Apache can
be installed on Windows Server or Windows client computers to host websites during development or for
production deployments. However, Apache cannot host ASP.NET websites.

Visual Studio Development Server


Visual Studio 2012 includes an in-built web server, Visual Studio Development Server. When you open a
website in debugging mode, Visual Studio Development Server starts and hosts your site. If you close the
browser or stop debugging, Visual Studio Development Server automatically stops. Visual Studio
Development Server cannot host sites for production and does not provide all the functionality available
in Internet Information Server 8.0 Express. For example, you cannot configure Secure Sockets Layer (SSL)
encryption in Visual Studio Development Server.

If you wanted to host an ASP.NET site you had written for simple testing by a small team of developers,
which of the above web servers would you use as a host?
MCT USE ONLY. STUDENT USE PROHIBITED
01-10 Exploring ASP.NET MVC 4

Windows Azure
Windows Azure is a part of Microsoft cloud
services for hosting business-critical systems.
When you run code on Windows Azure, it runs on
servers at Microsoft-managed datacenters at
locations around the globe. You have two main
advantages when you use Windows Azure for
hosting and deploying your web application:

o Flexible Scaling: As the needs of your web


application and database grow, extra
server resources are automatically
allocated. You do not need to plan for
server farms or load balancing systems
because these are built into Windows Azure.

o Flexible Pricing: With Windows Azure, you can choose a pay-as-you-go pricing model, which
means that you only pay for the data that you use. This makes Windows Azure very cost-efficient
for small websites. It also makes costs predictable for large websites.

You can use Windows Azure to host the following:

o Websites. You can host an entire website on Windows Azure. Windows Azure supports websites
developed in ASP.NET, PHP, or Node.js. You can also deploy websites to Windows Azure directly
from Visual Studio 2012 or WebMatrix 2.

o Web services. A web service does not include a user interface like a website. Instead, it consists of
a set of methods that you can call from other software. You can call web services from mobile
devices, desktop applications, and from website code running on the client-side or the server-
side.
o Databases. When you use Internet Information Server 8.0 to host a website, you can use SQL
Server to store the website database. When you host a website in Windows Azure, you can use
SQL Database, which is a cloud-based database service based on SQL Server, to host your
database.

o Virtual servers. You can provision entire virtual servers in Windows Azure to run business-critical
back office software or use the virtual servers as test environments. Virtual servers in Windows
Azure can run Windows Server or Linux.

o Mobile Services. If you develop apps for mobile devices such as phones and tablets, you can use
Windows Azure to host services that underpin them. Such services can provide user preference
storage, data storage and other functions.

o Media Services. When you use Windows Azure to host media such as audio and video, it is
automatically available to many types of devices such as computers, mobile phones, and tablets
and encoded in various formats such as MP4 and Windows Media formats.

How would site visitors know if your site was hosted in Windows Azure or IIS Server?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-11

Lesson 2
Overview of ASP.NET 4.5
ASP.NET 4.5 helps you create dynamic websites that use client-side and server-side code. In addition, with
ASP.NET 4.5, you are not restricted to a single style of programming; instead, you can choose from three
different programming models: Web Pages, Web Forms, and MVC. These programming models differ
from each other, and they have their own advantages and disadvantages in different scenarios. ASP.NET
also provides many features that you can use, regardless of the programming model you choose.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the Web Pages programming model.

o Describe the Web Forms programming model.

o Describe the MVC programming model.


o Determine whether to build Web Pages, Web Forms, or MVC web applications, based on
customer needs.

o Describe the features that can be used in all ASP.NET applications, regardless of the chosen
programming model.

Web Pages Applications


Web Pages is the simplest programming model
you can choose to build a web application in
ASP.NET. You can use Web Pages to create a
website quickly and with little technical
knowledge. There is a single file associated with
each webpage that users can request. For each
page, you write HTML, client-side code, and
server-side code in the same .cshtml file. The @
symbol is used to distinguish server-side code
from HTML and JavaScript. When users request
the page, the ASP.NET runtime compiles and runs
the server-side code to render HTML and returns
that HTML to the web browser for display.

Note: If you want to write server-side code in Visual Basic, you use .vbhtml files, instead of
.cshtml files.

You can use either Visual Studio 2012 or WebMatrix 2 to create Web Pages applications. ASP.NET 4.5
includes version 2.0 of Web Pages.

The following code example shows HTML and C# code in part of a Web Pages file. The code displays data
from the item object, which is an MVC model class.

A Web Pages Code Example


<h2>Special Offers</h2>
MCT USE ONLY. STUDENT USE PROHIBITED
01-12 Exploring ASP.NET MVC 4

<p>Get the best possible value on Northwind specialty


foods by taking advantage of these offers:</p>
@foreach (var item in offers) {
<div class="offer-card">
<div class="offer-picture">
@if (!String.IsNullOrEmpty(item.PhotoUrl)){
<img src="@Href(item.PhotoUrl) alt="@item.Title" />
}
</div>
</div>
}

The Web Pages programming model has the following advantages:

o It is simple to learn.

o It provides precise control over the rendered HTML.

o It can be written in WebMatrix 2 or Visual Studio2012.

Using a Web Pages site has some disadvantages:

o It provides no control over URLs that appear in the address bar.

o Large websites require a large numbers of pages, each of which must be coded individually.

o There is no separation of business logic, input logic, and the user interface.

Why should web developers need to take control of the URLs that appear in the address bar when a
visitor browses a Web Pages site?

Web Forms Applications


Web Forms is another programming model that
you can choose in ASP.NET. WebMatrix 2 does not
support Web Forms, so you must use Visual
Studio 2012 to develop a Web Forms application.
A Web Forms application is characterized by
controls, which you can drag from the Visual
Studio toolbox onto each webpage. This method
of creating a user interface resembles the method
used in desktop applications.

Web Forms Controls


ASP.NET provides a wide variety of highly-
functional controls that you assemble on web
forms. After you add a control to a page, you can write code to respond to user events. For example, you
can use code in a button click event to process a user’s input in a form. The controls provided include:

o Input controls, such as text boxes, option buttons, and check boxes.

o Display controls, such as image boxes, image maps, and ad rotators.

o Data display controls, such as grid views, form views, and charts.

o Validation controls, which check data entered by the user.

o Navigation controls, such as menus and tree views.

You can also create your own custom controls to encapsulate custom functionality.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-13

Web Forms Code Files


In a Web Forms application, HTML and control markup is stored in files with an .aspx extension. Server-
side C# code is usually written in an associated .cs file called a code-behind file. For example, a page
called default.aspx usually has a code-behind file called default.aspx.cs.

Similarly, when you write custom controls, you store HTML and control markup in an .ascx file. A control
called customcontrol.ascx has a code-behind file called customcontrol.ascx.cs.

Web Forms applications can also contain class files that have the .cs extension.

Note: If you write server-side code in Visual Basic, code-behind files have a .vb extension,
instead of a .cs extension.

Binding Controls to Data


In Web Forms applications, you can easily display data by binding controls to data sources. This technique
removes the necessity to loop through data rows and build displays line-by-line. For example, to bind a
grid view control to a SQL Server database table, you drag a SQL data source control onto the Web Form,
and use a dialog to bind the grid view to the data source. When the page is requested, ASP.NET runs the
query on the data source and merges the returned rows of data with the Web Forms page.

Advantages and Disadvantages of Web Forms


The Web Forms programming model has the following advantages:

o Using server controls and Design View, you can design your page visually.

o You can use a broad range of highly functional controls that encapsulate a lot of functionality.

o You can display data without writing many lines of server-side code.

o The user interface, in the .aspx file is separated from input and business logic in code-behind files.

Using a Web Forms site has some disadvantages:

o The ASP.NET Web Forms page life cycle is an abstraction layer over HTTP and can behave in
unexpected ways. You must have a complete understanding of this life cycle in order to write
code in the correct event handlers.
o You do not have precise control over markup generated by server-side controls.

o Controls can add large amounts of markup and state information to the rendered HTML page.
This increases the time it takes to load pages.

Why should web developers be concerned about the markup and state information that ASP.NET Web
Forms controls add to a rendered HTML page?
MCT USE ONLY. STUDENT USE PROHIBITED
01-14 Exploring ASP.NET MVC 4

MVC Applications
MVC is the another programming model available
in ASP.NET. MVC applications are characterized by
a strong separation of business logic, data access
code, and the user interface into Models,
Controllers, and Views. ASP.NET 4.5 includes MVC
version 4.0.

You cannot use WebMatrix to create MVC


applications.

Models
Each website presents information about different
kinds of objects to site visitors. For example, a
publisher’s website may present information about books and authors. A book includes properties such as
the title, a summary, and the number of pages. An author may have properties such as a first name, a last
name, and a short biography. Each book is linked to one or more authors.

When you write an MVC website for a publisher, you would create a model with a class for books and a
class for authors. These model classes would include the properties described and may include methods
such as “buy this book” or “contact this author”. If books and authors are stored in a database, the model
can include data access code that can read and change records.
Models are custom .NET classes and store code in .cs files.

Views
Each website must render HTML pages that a browser can display. This rendering is completed by Views.
For example, in the publishing site, a View may retrieve data from the Book Model and render it on a
webpage so that the user can see the full details. In MVC applications, Views create the user interface.

Views are markup pages that store both HTML and C# code in .cshtml files. This means that they are like
Web Pages, but they include only user interface code. Other logic is separated into Models and
Controllers.

Controllers
Each website must interact with users when they click buttons and links. Controllers respond to user
actions, load data from a model, and pass it to a view, so that it will render a webpage. For example, in the
publishing site, when the user double-clicks a book, he or she expects to see full details of that book. The
Book Controller receives the user request, loads the book model with the right book ID, and passes it to
the Book Details View, which renders a webpage that displays the book. Controllers implement input logic
and tie Models to the right Views.

Controllers are .NET classes that inherit from the System.Web.Mvc.Controller class and store code in .cs
files.

Advantages and Disadvantages of MVC


The MVC programming model has the following advantages:

o Views enable the developer to take precise control of the HTML that is rendered.

o You can use the Routing Engine to take precise control of URLs.
o Business logic, input logic, and user interface logic is separated into Models, Controllers, and
Views.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-15

o Unit testing techniques and Test Driven Development (TDD) is possible.

Using an MVC site has some disadvantages:

o MVC is potentially more complex than Web Pages or Web Forms to understand.

o MVC forces you to separate your concerns (models, views, and controllers). Some programmers
may find this challenging.
o You cannot visually create a user interface by dragging controls onto a page.

o You must have a full understanding of HTML, CSS, and JavaScript to develop Views.

When a user makes a request for a particular product in your product catalog, what component receives
the request first: a model, a controller, or a view?

Discussion: ASP.NET Application Scenarios


The following scenarios describe some
requirements for websites. In each case, discuss
which programming model you would choose to
implement the required functionality.

Database Front-End
Your organization has its own customer
relationship management system that stores data
in a SQL Server database. Your team of developers
wrote the user interface in Visual Studio 2012 as a
desktop application. The directors now require
that all computers should be able to access the
application even when the desktop client
application is not installed. Because all computers have a browser, you have decided to write a web
application in ASP.NET to enable this.

E-Commerce Site
You are a consultant for a large software organization. You have been asked to architect an e-commerce
website that will enable customers to browse the entire catalog of software packages, download the
packages, and purchase licenses. The company has a large team of developers who are familiar with .NET
object-oriented programming. It is company policy to use Test Driven Development for all software.

Website for a Small Charitable Trust


Your friend works for a charitable organization and asks your advice about a website. Your friend does not
have any budget to engage a consultant, but has written websites using Microsoft FrontPage. Your friend
wants to include a database of merchandise that site visitors can browse and purchase.
MCT USE ONLY. STUDENT USE PROHIBITED
01-16 Exploring ASP.NET MVC 4

Shared ASP.NET Features


ASP.NET also includes a range of features that are
available regardless of the programming model
that you use. This means that if you are familiar
with these features from working with Web Pages
or Web Forms, your knowledge can be used in
MVC applications also.

Configuration
When you configure an ASP.NET site, you can
control how errors are handled, how the site
connects to databases, how user input is validated,
and many other settings. You can configure
ASP.NET sites by creating and editing web.config
files. The web.config file in the root folder of your site configures the entire site, but you can override this
configuration at lower levels by creating web.config files in sub-folders.

Web.config files are XML files with a set of elements and attributes that the ASP.NET runtime accepts.

An Example Web.config File


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=
.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/Error.html" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880">
</forms>
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="System.Web.Optimization"/>
</namespaces>
</pages>
</system.web>
</configuration>

If you need to access configuration values at runtime in your server-side .NET code, you can use the
System.Web.Configuration namespace.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-17

Authentication
Many websites identify users through authentication. This is usually done by requesting and checking
credentials such as a user name and password, although authentication can be done by using more
sophisticated methods such as using smart cards. Some sites require all users to authenticate before they
can access any page, but it is common to enable anonymous access to some pages and require
authentication only for sensitive or subscription content.

ASP.NET supports several mechanisms for authentication. For example, if you are using Internet Explorer
on a Windows computer, ASP.NET may be able to use Integrated Windows authentication. In this
mechanism, your Windows user account is used to identify you. When you build Internet sites, you cannot
be sure that users have Windows, a compatible browser, or an accessible account, so Forms
Authentication is often used. Forms Authentication is supported by many browsers and it can be
configured to check credentials against a database, directory service, or other user account stores.

Membership and Roles


In many Internet sites, for example, Facebook and Twitter, users can create their own accounts and set
credentials. In this manner, your site can support a large number of members without requiring a huge
amount of administrative effort because administrators do not create accounts.

In ASP.NET, a membership provider is a component that implements user account management features.
Several membership providers are supported by ASP.NET, such as the SQL Membership Provider, which
uses a SQL database to store user accounts. You can also create a custom membership provider, inheriting
from one of the default providers, if you have unique requirements.
When you have more than a few users, you may want to group them into roles with different levels of
access. For example, you might create a “Gold Members” role containing user accounts with access to the
best special offers. ASP.NET role providers enable you to create and populate roles with the minimum of
custom code.

You can enable access to pages on your website for individual user accounts or for all members of a role.
This process is known as authorization.

State Management
Web servers and web browsers communicate through HTTP. This is a stateless protocol, where each
request is separate from requests before and after it. Any values from previous requests are not
automatically remembered.

However, when you build a web application, you must frequently preserve values across multiple page
requests. For example, if a user places a product in a shopping cart, and then clicks “Check Out”, this is a
separate web request, but the server must preserve the contents of that shopping cart; otherwise, it will be
emptied and the customer will buy nothing. ASP.NET provides several locations where you can store such
values or state information across multiple requests.

Caching
An ASP.NET page is built dynamically by the ASP.NET runtime on the web server. For example, in a Web
Pages application, the runtime must execute the C# code in the page to render HTML to return it to the
browser. That C# code may perform complex and time-consuming operations. It may run multiple queries
against a database or call services on remote servers. You can mitigate these time delays by using
ASP.NET caches.

For example, you can use the ASP.NET page cache to store the rendered version of a commonly
requested page in the memory of the web server. The front page of your product catalog may be
requested hundreds or thousands of time a day by many users. If you cache the page in memory the first
MCT USE ONLY. STUDENT USE PROHIBITED
01-18 Exploring ASP.NET MVC 4

time it is rendered, the web server can serve it to most users very rapidly, without querying the database
server and building the page from scratch.
Can you think of other facilities that all ASP.NET applications might need, regardless of the programming
model they use?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-19

Lesson 3
Introduction to ASP.NET MVC 4
You need to know how models, views, and controllers work together to render HTML, and how the
structure of MVC applications determines the display of information in a Visual Studio 2012 project. You
can examine the new features of MVC 4, included in ASP.NET 4.5, to build rich and engaging applications.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe models, views, and controllers.

o Explore the main features of an ASP.NET MVC 4 web application in Visual Studio.
o Describe the new features of ASP.NET MVC 4.

Models, Views, and Controllers


Models represent data and the accompanying
business logic, controllers interact with user
requests and implement input logic, and views
build the user interface. By examining how a user
request is processed by ASP.NET MVC 4, you can
understand how the data flows through models,
views, and controllers before being sent back to
the browser.

Models and Data


A model is a set of .NET class that represent
objects handled by your website. For example, the
model for an e-commerce application may
include a Product model class with properties such as Product ID, Part Number, Catalog Number, Name,
Description, and Price. Like any .NET class, model classes can include a constructor, which is a procedure
that runs when a new instance of that class is created. You can also include other procedures, if necessary.
These procedures encapsulate the business logic. For example, you could write a Publish procedure that
marks the Product as ready-to-sell.

Most websites store information in a database. In an MVC application, the model includes code that reads
and writes database records. ASP.NET MVC works with many different data access frameworks. However,
the most commonly used framework is the Entity Framework version 5.0.

Controllers and Actions


A controller is a .NET class that responds to web browser requests in an MVC application. There is usually
one controller class for each model class. Controllers include actions, which are methods that run in
response to a user request. For example, the Product Controller may include a Purchase action that runs
when the user clicks the Add To Cart button in your web application.

Controllers inherit from the System.Web.Mvc.Controller base class. Actions usually return a
System.Web.Mvc.ActionResult object.
MCT USE ONLY. STUDENT USE PROHIBITED
01-20 Exploring ASP.NET MVC 4

Views and Razor


A view is, by default, a .cshtml or .vbhtml file that includes both HTML markup and programming code. A
view engine interprets view files, runs the server-side code, and renders HTML to the web browser. Razor
is the default view engine in ASP.NET MVC 4, but ASP.NET MVC 4 also supports the ASPX view engine.
Additionally, you can install alternate view engines such as Spark and NHaml. The razor view engine
identifies server-side code by looking for the @ symbol as the following example demonstrates.

The following lines of code are part of an ASP.NET MVC 4 view and use the Razor syntax. The @ symbol
delimits server-side code.

Part of a Razor View


<h2>Details</h2>
<fieldset>
<legend>Comment</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.Subject)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Subject)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Body)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Body)
</div>
</fieldset>

Often, the view displays properties of a model class. In the preceding code example, the Subject property
and Body property are incorporated into the page.

Request Life Cycle


The Request life cycle comprises a series of events that happen when processing a web request. The
following steps illustrate the process that MVC applications follow to respond to a typical user request.
The request is for the details of a product with the ID “1”:

1. The user requests the web address: http://www.adventureworks.com/product/display/1

2. The MVC routing engine examines the request and determines that it should forward it to the
Product Controller and the Display action.

3. The Display action in the Product Controller creates a new instance of the Product model class.

4. The Product model class queries the database for information about the product with ID “1”.
5. The Display action also creates a new instance of the Product Display View and passes the Product
Model to it.

6. The Razor view engine runs the server-side code in the Product Display View to render HTML. In this
case, the server-side code inserts properties such as Title, Description, Catalog Number, and Price into
the HTML.

7. The completed HTML page is returned to the browser for display.

If you wanted to write some code that renders data from your products catalog into an HTML table,
would you place that code in a model, a view, a controller or a JavaScript function?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-21

Demonstration: How to Explore an MVC Application


In this demonstration, you will see how to use a sample photo sharing application to explore the structure
of an MVC website.

Demonstration Steps
1. Examine how an MVC application renders the default homepage.

2. Examine the Default route that forwards requests to the Controller.

3. Examine the photo model class.


4. Examine the Photo Controller code.

5. Examine the Photo Details View code.

6. Examine the photo details rendered as a result of Models, Controllers, and Views working together.

7. Close the Internet Explorer window and the Visual Studio application.

New Features of ASP.NET MVC 4


ASP.NET MVC 4 includes several new features,
such as the ASP.NET Web API, mobile features,
display modes, asynchronous controllers, OAuth
and OpenID support, and bundling and
minification.

o ASP.NET Web API. The ASP.NET Web API


makes it easy to create a set of web
services that can respond to browser
requests using simple HTTP verbs such as
GET, POST, and DELETE. Using the Web
API, you can build the back end web
services that a client-centric web
application can call. Building web applications using client-centric HTML pages and the Web API
is an alternative to using ASP.NET MVC.

o Mobile features. As smartphones and tablets become ubiquitous, you should ensure that your
website displays well on all screen sizes, resolutions, color depths, and so on. ASP.NET MVC 4.0
includes several new features to make this easier. For example, the jQuery Mobile script library
renders a rich user interface for smartphone browsers, but simpler controls to older models of
phones. You can use the new Mobile Project Template that uses jQuery Mobile to create sites
specifically for mobile devices. Alternatively, you can add mobile-specific views to your MVC
project.
o Display modes. Display modes allow you to select a view easily based on the web browser that
made the request. Display modes help render HTML for mobile devices and they can be used to
display content on unusual desktop browsers. When you add a display mode for a particular
browser, you override the views, template views, and partial views that are used to render HTML.

o Asynchronous controllers. Usually, a Controller action renders synchronously. If the action takes a
long time to run, the user has to wait because the action uses a single thread. With asynchronous
actions, you can call a long-running method on a separate thread and wait for the results to be
MCT USE ONLY. STUDENT USE PROHIBITED
01-22 Exploring ASP.NET MVC 4

returned. This enables an action to complete and respond to the user before the results are
returned.
o OAuth and OpenID standards support. Using the OAuth and OpenID standards for identifying
users connected to the web application over the Internet, you can call the services of a third-
party site to check the credentials of a user. You can trust the identity verified by the external site,
and then use it to authorize access to internal resources. Using these technologies, you could, for
example, enable users to access internal resources by logging on with their Windows Live
account.

o Bundling and minification. When a browser requests a webpage, the server often returns the page
with a CSS style sheet and one or more JavaScript files. ASP.NET MVC 4.0 can bundle these
separate files into a single file to increase the efficiency of the response. It can also minify these
files, by removing white space, shortening variable names, and performing other actions that
save space. This saves time on downloads and both server and client-side resources, and makes
your web application faster.

You want to encourage developers to reuse data from your website in mashups with Bing Maps. Which of
the new features of MVC 4 would you use to make this possible?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-23

Lab: Exploring ASP.NET MVC 4


Scenario
You are working as a junior developer at Adventure Works. You have been asked by a senior developer to
investigate the possibility of creating a web-based photo sharing application for your customers, similar to
one the senior developer has seen on the Internet. This will promote a community of cyclists who use
Adventure Works equipment, and the community members will be able to share their experiences. This
initiative is intended to increase the popularity of Adventure Works bikes, and thereby to increase sales.
You have been asked to begin the planning of the application by examining an existing photo sharing
application and evaluating its functionality. You have also been asked to examine programming models
available to ASP.NET developers. To do this, you need to create basic web applications written with three
different models: Web Pages, Web Forms, and MVC. You manager has asked you to report on the
following specific questions for each programing model:

o How does the developer set a connection string and data provider?

o How does the developer impose a consistent layout, with Adventure Works branding and menus,
on all pages in the web application?

o How does the developer set a CSS style sheet with a consistent set of color, fonts, borders, and
other styles?

o How does the developer add a new page to the application and apply the layout and styles to it?

Objectives
After completing this lab, you will be able to:

o Describe and compare the three programming models available in ASP.NET.

o Describe the structure of each web application developed in the three programming models—
Web Pages, Web Forms, and MVC.

o Select an appropriate programming model for a given set of web application requirements.

Lab Setup
Estimated Time: 45 minutes

Virtual Machine: 20486A-SEA-DEV11

Username: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:
 Enable the Allow NuGet to download missing packages during build option in Package
Manager.

Exercise 1: Exploring a Photo Sharing Application


Scenario
In this exercise, you will begin by examining the photo sharing application.

The main tasks for this exercise are as follows:

1. Register a user account.

2. Upload and explore photos.


MCT USE ONLY. STUDENT USE PROHIBITED
01-24 Exploring ASP.NET MVC 4

3. Use slideshows.

4. Test the authorization.

 Task 1: Register a user account.


1. Start the virtual machine and log on with the following credentials:

o Virtual machine: 20486A-SEA-DEV11

o User name: Admin


o Password: Pa$$w0rd

2. On the Windows 8 Start screen, open Visual Studio 2012, and pin it to the Taskbar.

3. Navigate to the following location tp open the PhotoSharingSample.sln file:

o Allfiles (D):\Labfiles\Mod01\PhotoSharingSample

4. Run the web application in non-debugging mode.

5. Create a new user account with the following credentials:


o User name: <A username of your choice>

o Password: <A password of your choice>

 Task 2: Upload and explore photos.


1. Add the following details to the Orchard image:
o Subject: Just a Test Comment

o Comment: This is a Sample

2. Add a new photo to the application by using the following information:


o Title of the photo: Fall Fungi

o Navigation path to upload the photo: Allfiles (D):\ Labfiles\Mod01\Pictures\fungi.jpg

o Description: Sample Text.

3. Verify the description details of the newly added photo.

 Task 3: Use slideshows.


1. Use the Slideshow feature.

2. Add the following images to your favorites list:


o Fall Fungi

o Orchard

o Flower

3. View the slideshow of the images selected as favorites.

 Task 4: Test the authorization.


1. Log off from the application, and then attempt to add a comment for the Fall Fungi image.

2. Attempt to add a new photo to the Photo Index page.

3. Close the Internet Explorer window and the Visual Studio application.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-25

Results:

At the end of this exercise, you will be able to understand the functionality of a photo sharing application,
and implement the required application structure in the Adventure Works photo sharing application.

Exercise 2: Exploring a Web Pages Application


Scenario
In this exercise, you will create a simple Web Pages application and explore its structure.

The main tasks for this exercise are as follows:

1. Create a Web Pages application.


2. Explore the application structure.

3. Add simple functionality.

4. Apply the site layout.

 Task 1: Create a Web Pages application.


1. Start Visual Studio 2012 and create a new Web Pages project by using the ASP.NET Web Site (Razor
v2) template.

2. Run the new webpages application in Internet Explorer and review the Contact page.
3. Stop debugging by closing Internet Explorer.

 Task 2: Explore the application structure.


4. Open the Web.config file and verify that the database provider used by the application is .NET
Framework Data Provider for Microsoft SQL Server Compact.

1. Verify that the Default.cshtml page and the Contact.cshtml page are linked to the same layout.

2. Verify that the Site.css file is used to apply styles to all pages on the site. Note that the
_SiteLayout.cshtml page is linked to the style sheet.

 Task 3: Add simple functionality.


1. Add a new Razor v2 webpage to the application at the route level by using the following information:

o Webpage name: TestPage.cshtml

2. Add an H1 element to the TestPage.cshtml page by using the following information:

o Content: This is a Test Page

3. Add a link to the Default.cshtml page by using the following information:

o Start tag: <a>

o Attribute: href = “~/TestPage.cshtml”

o Content: Test Page

o End tag:</a>

4. Save all the changes.

5. Run the website, and view the page you added.

6. Stop debugging by closing Internet Explorer.


MCT USE ONLY. STUDENT USE PROHIBITED
01-26 Exploring ASP.NET MVC 4

 Task 4: Apply the site layout.


1. Add the Razor code block to the TestPage.cshtml file.

2. In the new code block, set the TestPage to use the following layout:

o Layout: _SiteLayout.cshtml

3. Save all the changes.

4. Run the web application in debugging mode and browse to TestPage.chstml.

5. Close all open applications.

Results: At the end of this exercise, you will be able to build a simple Web Pages application in Visual
Studio.

Exercise 3: Exploring a Web Forms Application


Scenario
In this exercise, you will create a simple Web Forms application and explore its structure.

The main tasks for this exercise are as follows:

1. Create a web forms application.

2. Explore the application structure.


3. Add simple functionality.

4. Apply the master page.

 Task 1: Create a web forms application.


1. Start Visual Studio 2012 and create a new Web Forms project, TestWebFormsApplication, by using
the ASP.NET Web Forms Application template.

2. Run the new Web Forms application in Internet Explorer and examine the Contact page.

3. Stop debugging by closing Internet Explorer.

 Task 2: Explore the application structure.


1. Open the Web.config file and verify that System.Data.SqlClient is the database provider that the
application uses.

2. Verify that the ~/Site.Master file contains a common layout for all the pages on the site. Also verify
that the Default.aspx and Contact.aspx pages are linked to the same layout.

3. Verify that the Site.css file is used to apply styles to all pages on the website. Note that the Site.Master
file uses bundle reference to package the CSS files.

 Task 3: Add simple functionality.


1. Add a new Web Forms page to the application at the route level by using the following information:

o Name of the web form: TestPage.aspx.

2. Add an H1 element to the TestPage.aspx page by using the following information:

o Content: This is a Test Page


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-27

3. Add a link to the Default.aspx page by using the following information:

o Start tag: <a>

o Attribute: href = “~/TestPage.cshtml”

o Content: Test Page

o End tag:</a>

4. Run the website in Internet Explorer and view the newly added Web Form page.

 Task 4: Apply the master page.


1. Add a new attribute to the @ Page directive in the TestPage.aspx file by using the following
information:

o Attribute name: MasterPageFile

o Attribute value: ~/Site.Master

2. Remove the static markup tags from the TestPage.aspx and replace it with a Web Forms Content
control by using the following information:

o Start tag: <asp:Content>

o Runat attribute: server


o ID attribute: BodyContent

o ContentPlaceHolderID: MainContent

o Content: <h1>This is a Test Page</h1>


o End tag: </asp:Content>

3. Save all the changes.

4. Run the created website and verify the contents of the TestPage.aspx file.

5. Close all open applications.

Results: At the end of this exercise, you will be able to build a simple Web Forms application in Visual
Studio.

Exercise 4: Exploring an MVC Application


Scenario
In this exercise, you will create a simple MVC application and explore its structure.

The main tasks for this exercise are as follows:

1. Create an MVC 4 application.


2. Explore the application structure.

3. Add simple functionality.

4. Apply the site layout.


MCT USE ONLY. STUDENT USE PROHIBITED
01-28 Exploring ASP.NET MVC 4

 Task 1: Create an MVC 4 application.


1. Start Visual Studio 2012 and create a new MVC project by using the ASP.NET MVC 4 Web
Application template.

2. Run the new MVC application in Internet Explorer, and explore the Contact page.

3. Stop debugging by closing Internet Explorer.

 Task 2: Explore the application structure.


1. Open the Web.config file and verify whether the database provider is System.Data.SqlClient.

2. Verify that the ~/Views/Shared/_Layout.cshtml file contains a common layout for all pages on the
website, and how pages link to the layout.

3. Verify that the Site.css file is used to apply styles to all pages on the website, and note how the pages
link to the style sheet.

 Task 3: Add simple functionality.


1. Add a new view to the application by using the following information:

o Name of the view: TestPage.cshtml


o Clear the Use a layout or master page check box.

2. Add an H1 element to the TestPage.cshtml view by using the following information:

o Content: This is a Test Page


3. Add an action to the HomeController.cs file by using the following information:

o Procedure name: TestPage

o Return type: ActionResult


o Procedure parameters: None

4. Add a link to the Index.cshtml page by using the following information:

o Start tag: <a>

o Attribute: href=” ~/Home/TestPage”

o Content: Test Page

o End tag: </a>

5. Save all the changes.

6. Run the website and view the page you added.

7. Stop debugging by closing Internet Explorer.

 Task 4: Apply the site layout.


1. Open the TestPage.cshtml file and remove the code that sets the Layout = null.

2. In the TestPage.cshtml file, remove all the tags except the <h1> tag and its contents.

3. Save all the changes.


4. Run the web application and browse to Test Page.

5. Close all the open applications.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 01-29

Results: At the end of this exercise, you will be able to build a simple MVC application in Visual Studio.

Question: Which of the three programming models has the simplest method of applying a
single layout across multiple pages?

Question: Which of the three programming models has the simplest method of building a
user interface?

Question: Which of the application programming models will you recommend for the photo
sharing application: Web Pages, Web Forms, or MVC?
MCT USE ONLY. STUDENT USE PROHIBITED
01-30 Exploring ASP.NET MVC 4

Module Review and Takeaways


In this module, you have seen the tools, technologies, and web servers that are available in the Microsoft
web stack, which you can use to build and host web applications for intranets and on the Internet. You
should also be able to distinguish applications written in the three ASP.NET programming models: Web
Pages, Web Forms, and MVC. You should be able to use MVC applications to render web pages by using
models, views, and controllers.

Best Practice: Use Web Pages when you have simple requirements or have developers with little
experience of ASP.NET.

    Best Practice: Use Web Forms when you want to create a user interface by dragging controls from a
tool box onto each webpage or when your developers have experience of Web Forms or Windows
Forms.

     Best Practice: Use MVC when you want the most precise control of HTML and URLs, when you want
cleanly to separate business logic, user interface code, and input logic, or when you want to perform
Test Driven Development.

Common Issues and Troubleshooting Tips

Issue Troubleshooting Tip

In MVC, views cannot


function without
controller actions. To
use the new view, you
must add a controller
action that returns
that view.

Additional Reading: For the more information about ASP.NET, including forums, blogs, and third-
party tools, visit the Official ASP.NET site: http://www.asp.net/

Review Question(s)
Question: Which of the shared features of ASP.NET can you use in Web Pages, Web Forms,
and MVC applications to increase the speed with which frequently-requested pages are
returned to the browser?

Question: You want to create a simple website that shares dates and venues for games for
your sports club with its members. The sports club has no budget to buy software. Which
development environment should you use to create the site?

Real-world Issues and Scenarios


You have written a web application for a client that sells hats. Visitors to the site will be able to register,
redeem offer vouchers, and purchase hats. You expect site traffic to be steady through most of the year
but to peak just before Christmas. Should you recommend IIS or Windows Azure for hosting the site?
MCT USE ONLY. STUDENT USE PROHIBITED
02-1

Module02
Designing ASP.NET MVC 4 Web Applications
Contents:
Module Overview 02-1 

Lesson 1: Planning in the Project Design Phase 02-2 

Lesson 2: Designing Models, Controllers, and Views 02-15 

Lab: Designing ASP.NET MVC 4 Web Applications 02-20 

Module Review and Takeaways 02-25 

Module Overview
Microsoft ASP.NET MVC 4 is a powerful programming model that you can use to create powerful and
complex web applications. However, all complex development projects, and large projects in particular,
can be challenging and intricate to fully understand. Without a complete understanding of the purposes
of a project, you cannot develop an effective solution to the customer’s problem. You need to know how
to identify a set of business needs and plan an MVC web application to address those needs. The project
plan that you create assures stakeholders that you understand their requirements and communicates the
functionality of the web application, its user interface, structure, and data storage to the developers who
will create it. By writing a detailed and accurate project plan, you can ensure that the powerful features of
MVC are used most effectively to solve a customer’s business problems.

Objectives
After completing this module, you will be able to:

o Plan the overall architecture of an MVC 4 web application and consider aspects such as state
management.

o Plan the models, controllers, and views that are required to implement a given set of functional
requirements.
MCT USE ONLY. STUDENT USE PROHIBITED
02-2 Designing ASP.NET MVC 4 Web Applications

Lesson 1
Planning in the Project Design Phase
Before you and your team of developers plan a Model-View-Controller (MVC) web application or write
any code, you must have a thorough understanding of two things: the business problem you are trying to
solve and the ASP.NET components that you can use to build a solution. Before designing a web
application architecture and its database, you should know how to identify the requirements that
potential users of a web application have before designing the web application architecture and its
database.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the various project development models.

o Describe how to gather information about project requirements when building MVC 4 web
applications.

o Determine the functional requirements and business problems when building web applications.

o Explain how to plan the database design when building a web application.
o Describe possible distributed application architectures.

o Describe the options for planning state management in a web application.

o Describe the options for planning globalization and localization of a web application.
o Determine the critical aspects of web application design.

Project Development Methodologies


Developing a web application or intranet
application is often a complex process that
involves many developers in different teams
performing various roles. To organize the
development process and ensure that everybody
in a project works together, there are a wide
range of development methodologies. These
development methodologies describe the phases
of the development project, the roles people take,
the deliverables that conclude each phase, and
other aspects of the project. You should choose a
development methodology at an early stage in a
project. Many organizations have a standard methodology that they always use for project development.

Some project development methodologies include the waterfall model, the iterative development model,
the prototyping model, the agile software development model, extreme programming, and test-driven
development.

Waterfall Model
The waterfall model is an early methodology that defines the following phases of a project:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-3

o Feasibility Analysis. In this phase, planners and developers study and determine the approaches
and technologies that can be used to build the software application.
o Requirement analysis. In this phase, planners and analysts interview the users, managers,
administrators, and other stakeholders of the software application to determine their needs.

o Application design. In this phase, planners, analysts and developers document a proposed
solution.

o Coding and unit testing. In this phase, developers code and test components that make up the
system individually.
o Integration and system testing. In this phase, developers integrate the components that they have
built and test the system as a whole.

o Deployment and maintenance. In this phase, developers and administrators deploy the solution
so that users can start using the software application.

The waterfall model classifies the development project into distinct phases with a clear definition of
deliverables for each phase. The model also emphasizes the importance of testing. However, the customer
does not receive any functional software for review until late in the project. This makes it difficult to deal
with changes to the design in response to beta feedback or manage altered circumstances.

Iterative Development Model


When you use an iterative development model, you break the project into small parts. For each part, you
perform the activities related to all the stages of the waterfall model. The project is built up stage by
stage, with thorough testing at each stage to ensure quality.
In an iterative project, you can perform corrective action at the end of each iteration. These corrections
might reflect a better understanding of the business problems, insightful user feedback, or a better
understanding of the technologies that you used to build the solution. Because requirements are added at
the end of each iteration, iterative projects require a great deal of project management effort and
frequently feature an overrun of planned efforts and schedule.

Prototyping Model
The prototyping model is suitable for a project where you begin with few or meagerly defined business
requirements. This situation occurs when the customers or stakeholders have only a vague understanding
of their needs and how to solve them. In this approach, developers create a simplified version of the
software application, and then seek feedback from stakeholders. This feedback on the prototype is used
to define detailed requirements, which developers use in the next iteration to build a solution that
matches the needs of stakeholders to better help them perform their jobs.

After two or more iterations, when both stakeholders and developers reach a consensus on the
requirements, a complete solution is built and tested. The prototyping model, however, can lead to a
poorly-designed application because at no stage in the project is there a clear focus on the overall
architecture.

Agile Software Development Model


The waterfall model, iterative development model, and prototyping model are based on the premise that
business requirements and other factors do not change from the beginning to the end of the project. In
reality, this assumption is often invalid. Agile software development is a methodology designed to
integrate changing circumstances and requirements throughout the development process. Agile projects
are characterized by:

o Incremental development: Software is developed in rapid cycles that build or earlier cycles. Each
iteration is thoroughly tested.
MCT USE ONLY. STUDENT USE PROHIBITED
02-4 Designing ASP.NET MVC 4 Web Applications

o Emphasis on people and interactions: Developers write code based on what people do in their
role, rather than what the development tools are good at.
o Emphasis on working software: Instead of writing detailed design documents for stakeholders,
developers write solutions that stakeholders can evaluate at each iteration to validate if it solves a
requirement.
o Close collaboration with customers: Developers discuss with customers and stakeholders on a day-
to-day basis to check requirements.

Extreme Programming
Extreme programming evolved from agile software development. In extreme programming, the
preliminary design phase is reduced to a minimum and developers focus on solving a few critical tasks. As
soon as these critical tasks are finalized, developers test the simplified solution and obtain feedback from
stakeholders. This feedback helps developers identify the detailed requirements, which evolve over the
project life cycle.

Extreme programming defines a user story for every user role. A user story describes all the interactions
that a user with a specific role might perform with the completed application. The collection of all the user
stories for all user roles describes the entire application.

In extreme programming, developers often work in pairs. One developer writes the code and the other
developer reviews the code to ensure that it uses simple solutions and adheres to best practices. Test-
driven development is a core practice in extreme programming.

Additional Reading: For more information about the extreme programming model, see
Extreme Programming.

Test Driven Development Model


In test-driven development (TDD), developers write test code as their first task in a given iteration. For
example, if you want to write a component that stores credit card details, you begin by writing tests that
such a component would pass. These could be whether it checks the number formats correctly, whether it
writes strings to a database table correctly, or whether it calls banking services correctly. After you define
the tests, you write the component to pass those tests.

In subsequent iterations, the credit card tests remain in place. This ensures that if you break the credit
card functionality, perhaps by refactoring code or by adding a new constructor, you find this out quickly
because the tests fail.

In Microsoft Visual Studio 2012, you can define a test project, within the same solution as the main
project, to store and run unit tests. After you write the tests, you can run them singly or in groups after
every code change. Because MVC projects have the model, view, and controller code in separate files, it is
easy to create unit tests for all aspects of application behavior. This is a major advantage of MVC over
Web Pages and Web Forms.

Unified Modeling Language


The Unified Modeling Language (UML) is an industry standard notation to document the design of any
application that uses object-oriented technology. UML is not a development model. Rather, UML
diagrams are often used for planning and documenting application architecture and components, across
all project development methodologies. When you use UML to design and document an application, you
create a range of diagrams with standard shapes and connectors. These diagrams can be classified into
three classes:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-5

o Behavior Diagrams. These diagrams depict the behavior of users, applications, and application
components.

o Interaction Diagrams. These diagrams are a subset of behavior diagrams that focus on the
interactions between objects.

o Structure Diagrams. These diagrams depict the elements of an application that are independent
of time. This means they do not change through the lifetime of the application.

What aspects of agile software development and extreme programming might be of concern to
customers? What aspects might reassure them?

Gathering Requirements
When a project is commissioned, you need a
vision of the solution. The vision can often be
vague and require in-depth investigation, before
you can add details and ensure that all
stakeholders’ requirements are covered by the
web application that you build. These
requirements can be of two types:

o Functional requirements: These


requirements describe how the
application behaves and responds to
users. Functional requirements are often
called behavioral requirements. They
include:
 User Interface Requirements: These requirements describe how the user interacts with an
application.
 Usage Requirements: These requirements describe things a user can do with the application.
 Business Requirements: These requirements describe how the application will fulfill business
functions.
o Technical requirements: These requirements describe technical features of the application and
relate to availability, security, or performance. These requirements are sometimes called non-
functional or non-behavioral requirements.

You usually gather requirements by interviewing stakeholders such as users, administrators, other
developers, board members, budget holders, and team managers. Each of these groups will have a
different set of priorities that the application needs to fulfill.

Usage Scenarios and Use Cases


A common method by which you can build a set of user interface requirements, business requirements,
and usage requirements is to ask users what they will do with the web application that you build. You can
document these actions as usage scenarios and use cases.

A usage scenario is a specific real-world example, with names and suggested input values, of an
interaction between the application and a user. The following is a simple example:

1. Roger Lengel clicks the Add Photo link on the main site menu.

2. Roger provides the input RogerL in the User name box and the password in the Password box to
authenticate on the site.
MCT USE ONLY. STUDENT USE PROHIBITED
02-6 Designing ASP.NET MVC 4 Web Applications

3. Roger types the title Sunset for the photo.

4. Roger browses to the JPEG file for his new photo.

5. Roger clicks the Upload button.

6. The web application stores the new photo and displays the photo gallery to Roger.

A use case is similar to a usage scenario, but is more generalized. Use cases do not include user names or
input values. They describe multiple paths of an interaction, which depends on what the user provides as
input or other values. The following is a simple example:

1. The user clicks the Add Photo link on the main site menu.

2. If the user is anonymous, the logon page is shown and the user provides credentials.

3. If the credentials are correct the CreatePhoto view is displayed.

4. The user types a title.

5. The user specifies the photo file to upload.

6. The user optionally types a description for the photo.

7. The user clicks the Upload button.

8. The web application stores the new photo and displays the photo gallery to the user.

Note: Similar to verbal descriptions, you can use UML Use Case diagrams to document use
cases for your web application.

By analyzing usage scenarios and use cases, you can identify functional requirements of all types. For
example, from the above use case you can identify the following user interface requirement: The webpage
that enables users to create a new photo must include Title and Description textboxes, a file input control
for the photo file and an Upload button to save the photo.

Agile Requirements Modeling


In a traditional waterfall model or iterative development model, developers and analysts investigate and
document precise and detailed functional and technical requirements at an early stage of the project,
which do not change later. By contrast, in an Agile development model-based project, developers
recognize that requirements can change at any time during development. Requirements analysis is
therefore characterized as follows:

o Initial requirement modeling: In the initial design phase, developers identify and document a few
broad use cases in an informal manner without full details.

o Just-in-time modeling: Before writing code that implements a use case, a developer discusses it
with the relevant users. At this point, the developer adds full details to the use case. In an agile
development project, developers talk to users and other stakeholders at all times, and not just at
the beginning and end of the project.

o Acceptance testing: An acceptance test is a test that the application must pass for all stakeholders
to accept and sign off the application. When you identify a functional requirement, you can also
specify a corresponding acceptance test that must be run to ensure that the requirements are
met.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-7

User Stories in Extreme Programming


In extreme programming projects, developers perform even less functional requirement analysis at the
beginning of the project compared with other development models. They create user stories instead of
use cases or user scenarios. A user story is a very broad example of an interaction between the application
and a user and is often stated in a single sentence as the following example illustrates:

o Users can upload photos and give new photos a title and a description.

User stories contain just the minimal details to enable developers to estimate the effort involved in
developing it. Extreme programmers discuss each user story with stakeholders just before they write code
to implement each user story.

Question: If a customer asks you to ensure 95% availability, is this a functional requirement or a technical
requirement?

Plan Database Design


When you have a good understanding of the
functional requirements and technical
requirements of the proposed web application,
you can start designing the physical
implementation of the application. One of the
most important physical objects to plan for is one
or more databases. Although not all web
applications use databases for information
storage, they are an underlying object for a
majority of sites and you will use them in most of
your projects.

Logical Modeling
You can begin your data design at a high level by creating UML Domain Model diagrams and Logical
Data Model (LDM) diagrams.

A domain model diagram, also known as a conceptual data model, shows the high-level conceptual
objects that your web application manages. For example, in an e-commerce web application, the domain
model includes the concepts of customers, shopping carts, and products. The domain model does not
include details of the properties each concept has, but shows the relationships between concepts. Use the
domain model to document your initial conversations with stake holders.

In essence, an LDM is a domain model diagram with extra details added. You can use LDM diagrams to fill
in more details, such as properties and data types, for the concepts that you defined in the domain model.
Note that the objects in the LDM do not correspond to tables in the database. For example, the shopping
cart object, may display data from both the customer database and product database tables.

Physical Database Structure


You should consider the following database objects in your project plan:

o Tables: These are the fundamental storage objects in a database. When you define a table, you
need to specify the columns that each table has. For each column, you must define a data type
such as integer, string, usually the nvarchar type in SQL Server, or date and time. You should
also define the primary key for the table – the value of this column uniquely identifies each
record and is essential for defining relationships with records in other tables.
MCT USE ONLY. STUDENT USE PROHIBITED
02-8 Designing ASP.NET MVC 4 Web Applications

o Views: These are common presentations of data in tables and are based on queries. For example,
a view can join two tables such as a products table and a stock levels table.
o Stored Procedures: These are common sequences of database operations that you define in the
database. Some operations are complex and might involve a complex transformation of the data.
You can define a stored procedure to implement such a complex routine.
o Security: You need to consider how the web application will authenticate with the database server
and how you will authorize access to each database table.

In UML, a Physical Data Model is a diagram that depicts tables, columns, data types and relationships
between tables.

Working with Database Administrators


Sometimes the developer team has full control over the database that underlies the web application. This
happens, for example, when the organization is small or when the web application has a separate
database server with no business-critical data. However, in larger organizations or in projects where the
database stores critical business information, there may be a dedicated team of database administrators
(DBAs). These DBAs are usually highly skilled in database design and administration and their job is to
ensure data integrity based on the organization’s data storage policy.

If your project database is administered by the DBA team, it is essential to communicate well with them.
You need to consult with DBAs for their requirements. They frequently impose a list of technical
requirements that other stakeholders may not understand. As you build and deploy the web application,
DBAs are responsible for creating databases on the right servers or clusters and assigning permissions.
DBAs are critical contributors in delivering the web application.

Database Design in Agile Development and Extreme Programming


Agile development and extreme programming are characterized by a relatively small amount of initial
planning and documentation, and acknowledge that requirements are likely to change throughout the
development project. If you are working by using these development methodologies, you will only create
domain models during the initial planning phase of your project. You do not develop LMOs or physical
data models until you write code that implements the functional requirements. Furthermore, you will
discuss with users when you create LMOs and DBAs and also when you create physical data models.

In agile development and extreme programming, the database design changes throughout the project
until deployment. Therefore, developers should be able to alter the database whenever necessary without
consulting DBAs or complying with complex data policies. For this reason, you should use a separate
database hosted on a dedicated development server.

In some cases, the development project works with a database that already exists. For example, you may
be developing a web application that presents and edits information from the company employee
database on the intranet. In such cases, the database does not change as the code is developed but
functional and technical requirements may still change. You should copy the database to an isolated
development database server to ensure that your developing code does not erroneous modify business-
critical data.

Question: You want to implement a shopping cart in your web application. How many logical data
models are required? How many database tables are required?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-9

Planning for Distributed Applications


For small web application with less user traffic
levels, you can choose to host all the components
of your web application on a single server.
However, as your web application grows, a
distributed deployment, in which different servers
host separate components of the application, is
often used. Distributed web applications often use
a layered architecture:

o Presentation Layer: Components in this


layer implement the user interface and
presentation logic. If you are building an
MVC web application, views and
controllers make up your presentation layer.

o Business Logic Layer: Components in this layer implement high-level business objects such as
products, or customers. If you are building an MVC web application, models make up your
business logic layer.

o Data Access Layer: Components in this layer implement database access operations and abstract
database objects, such as tables, from business objects. For example, a product business object
may include data from both the Products and StockLevels database tables. If you are building an
MVC web application, models often make up both business logic and data access layers.
However, with careful design and coding practices, it is possible to refactor code to separate
these layers.

o Database Layer: This layer has the database itself.

If you implement such a layered architecture in your web application, you can host each layer on separate
servers. Often, for example, the Presentation layer is hosted on one or more IIS servers, the Business Logic
and Data Access layers are hosted on dedicated middle-tier servers, and the database is hosted on
dedicated SQL Server servers. This approach has the following advantages:
o You can specify server hardware that closely matches each role. For example, a server that hosts
business logic components requires good memory and processing resources.

o You can dedicate multiple servers to each role to ensure that a single server failure does not
cause an interruption in service.

o Only the web servers must be in the perimeter network. Both middle-tier servers and database
servers can be protected by two firewalls without direct access from the Internet.

o Alternatively, you can host middle-tier layers and databases on a cloud service, such as Windows
Azure.

Communication Between Layers


When a single server hosts all the components of a web application, the presentation, business logic, and
data access components run in a single process in the web server memory. Communication between
components is not an issue. However, when you run different layers on different servers you must
consider two issues:

o How does each layer exchange information and messages?

o How does each server authenticate and secure communications with other servers?

The communication of information and security is performed in different ways between the various layers:
MCT USE ONLY. STUDENT USE PROHIBITED
02-10 Designing ASP.NET MVC 4 Web Applications

o Between the browser and presentation layer web server: In any web application, the web browser,
where the presentation layer runs, communicates with the web server by using HTTP. If
authentication is required, it is often performed by exchanging plain text credentials. You can
also use Secure Sockets Layer (SSL) to encrypt this sensitive communication.

o Between the web server and the middle tier server: The communication and security mechanisms
used for communication between the web server and the middle tier server depends on the
technology that you use to build the business logic components:
 Web Services: If you implement business objects and data access classes as web services, the
presentation layer components communicate with the web services by using HTTP. You can
perform authentication by using the Kerberos protocol that is a part of Windows integrated
authentication or by using plain text encrypted with SSL.
 Windows Communication Foundation (WCF) Services: If you implement business objects and
data access classes as WCF services, you can choose between two hosting mechanisms. You
can host the WCF services within IIS, in which case HTTP is the transport mechanism and SSL
is the security mechanism. You can also host the WCF services within a Windows Process
Activation Service (WAS), in which case you can use TCP, Microsoft Message Queuing
(MSMQ), or named pipes as the transport mechanism.
o Between middle tier server and database: The middle tier server sends T-SQL queries to the
database server, which authenticates against the database by using the required credentials,
which are often included in the connection string.

Note: Web services and WCF services are covered in detail in Course 20487A: Developing
Windows Azure and Web Services.

Question: What are the advantages of writing middle tier components as WCF services and not web
services?

Planning State Management


In application development, the application state
refers to the values and information that are
maintained across multiple operations. Hypertext
Transfer Protocol (HTTP) is fundamentally a
stateless protocol, which indicates that it has no
mechanism to retain state information across
multiple page requests. However there are many
scenarios, such as the following, that require state
to be preserved:

o User preferences: Some websites enable


users to specify preferences. For example,
a photo sharing web application might
enable users to choose a preferred size for photos. If this preference information is lost between
page requests, users have to continually reapply the preference.

o User identity: Some sites authenticate users to provide access to members-only content. If the
user identity is lost between page requests, the user must re-enter their credentials for every
page.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-11

o Shopping carts: If the content of a shopping cart is lost between page requests, the customer
cannot buy anything from your web application.
In almost all web applications, state storage is a fundamental requirement. ASP.NET provides several
locations where you can store state information and simple ways to access the state information.
However, you must plan the use of these mechanisms carefully. If you use the wrong location, you may
not be able to retrieve a value when you expect to. Furthermore, poor planning of state management
frequently results in poor performance.

In general, you should be careful about maintaining large quantities of state data because it either
consumes server memory, if it is stored on the server or slows down the transfer of the webpage to the
browser, if it is included in a web page. If you need to store state values, you can choose between client-
side state storage or server-side state storage.

Client-Side State Storage


When you store state information on the client, you ensure that server resources are not used. However,
you should consider that all client-side state information must be sent between the web server and the
web browser, and this process can slow down page load time. Use client-side state storage only for small
amounts of data:

o Cookies: Cookies are small text files that you can pass to the browser to store information. A
cookie can be stored
 In the client computer memory, in which case it preserves information only for a single user
session
 On the client computer hard disk drive, in which case it preserves information across multiple
sessions.
Most browsers can store cookies only up to 4096 bytes and permit only 20 cookies per website. Therefore,
cookies can be used only for small quantities of data. Also some users may disable cookies for privacy
purposes, so you should not rely on cookies for critical functions.

o Query strings: A query string is the part of the URL after the question mark and is often used to
communicate form values and other data to the server. You can use the query string to preserve
a small amount of data from one page request to another. All browsers support query strings but
some impose a limit of 2083 characters on the URL length. You should not place any sensitive
information in query strings because it is visible to the user, anyone observing their session, or
anyone monitoring web traffic.

Note: In ASP.NET Web Forms applications, View State, Control State, and Hidden Fields can
be used to store state information in the rendered HTML that the server sends to the client. These
mechanisms are not available in MVC web applications because they do not use Web Forms
controls.

Server-Side State Storage


State information that is stored on the server consumes server resources, so you must be careful not to
overuse server-side state storage or risk poor performance.

The following locations store state information in server memory:

o TempData: This is a state storage location you can use in MVC applications to store values
between one request and another. You can store values by adding them to the TempData
collection. This information is preserved for a single request only and is designed to help
MCT USE ONLY. STUDENT USE PROHIBITED
02-12 Designing ASP.NET MVC 4 Web Applications

maintain data across a web page redirect. For example, you can use it to pass an error message
to an error page.
o Application State: This is a state storage location you can use to store vales for the lifetime of the
application. The values stored in application state are shared among all users. You can store
values by adding them to the Application collection. If the web server or the web application is
restarted, the values are destroyed. The Application_Start() procedure in the Global.asax file is
an appropriate place to initialize application state values. Application state is not an appropriate
place to store user-specific values, such as preferences, because if you store a preference in
application state, all users share the same preference instead of having their own unique value.

o Session State: The Session collection stores information for the lifetime of a single browser
session and values stored here are specific to a single user session: they cannot be accessed by
other users. By default, if the web server or the web application is restarted the values are
destroyed. However, you can configure ASP.NET to store session state in a database or state
server. If you do this, session state can be preserved across restarts. Session state is available for
both authenticated users and anonymous users. By default, session state uses cookies to identify
users but you can configure ASP.NET to store session state without using cookies.

If you choose to use these server memory locations, be sure to estimate the total volume of state data
that may be required for all the concurrent users that you expect to manage. Application state values are
stored only once but session state values are stored once for each concurrent user. Specify server
hardware that can easily manage this load or move state data into the following server hard disk drive-
based locations.

o Profile properties: If your site uses an ASP.NET profile provider, you can store user preferences in
profiles. Profile properties are persisted to the membership database so they will be kept even if
the web application or web server restarts.

o Database tables: If your site uses an underlying database, like most sites do, you can store state
information in its tables. This is a good place to store large volumes of state data that cannot be
placed in server memory or on the client computer. For example, if you have a large volume of
session-specific state information to store, you can store a simple ID value in the Session
collection and use it to query and update a record in the database.

Bear in mind that state data is only one form of information that an ASP.NET application places in server
memory. For example, caches must share memory with state data.

Question: You show the visitors of your website a choice of countries. When they pick a state, you want
to redirect them to a page that shows a map of that state. You will not use the name of the chosen
country that the user selected after this. Which location should you use to store the name of the chosen
country?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-13

Planning Globalization and Localization


The Internet is an international network and,
unless you are sure that the audience of your web
application speaks a single language, you must
ensure that everyone can read your content. You
limit the site’s potential audience if you render
pages only in English. The process by which you
make a web application available in multiple
languages is called globalization or
internationalization. The process by which you
make a web application available in a specific
language and culture is called localization.

Managing Browsers for Languages and Regions


There is an internationally-recognized set of language codes that specify a culture on the Internet. These
codes are in two parts:

1. The language. For example, English is “en”, Russian is “ru”.

2. The region. This specifies regional variations within a language and affects spellings and formats. For
example, in United States English, “Globalize” is correct and dates are written in mm/dd/yy format,
whereas in British English, “Globalise” is correct and dates are written in dd/mm/yy format.

The full language and region code for United States English is “en-US” and the full language and region
code for British English is “en-UK”.

The preferred language that users choose is available as the language code in the HTTP Header of the
user’s browser. This is the value that you respond to so as to globalize your site. Alternatively, you can
provide a control, such as a drop-down list, in which users can choose their preferred language. This is a
good example of a user-preference that you can store in the session state.

Using Resource Files


When the user specifies a preferred language and region, you must respond by rendering pages for that
culture. One method to provide a localized response is to use resource files to insert text in the
appropriate language into the page at run time. A resource file is a simple dictionary of terms in a given
language. For each term in the file, you need to specify a name, a value, and optionally a comment. The
file has a .resx extension. The filename also includes the language code that the resources apply to. For
example, if you create a resource file for a view called Index that stored values in Chilean Spanish, you
would name the file Index.es-CL.resx.

Resource files can also have a neutral culture. This means that the file applies to any region in a given
language. For example the Index.es.resx file applies Spanish terms, regardless of the regional code that
has been chosen.

You should also create corresponding default resource files, without any language code in the file name,
such as Index.resx. These files are used when a preferred language is not specified.

When you use resource files to localize a site, each view applies regardless of the preferred language. You
must insert extra Razor code in the view to take text values from a resource file. This can reduce the
readability of view files because all the rendered text comes from resource files. However, it is easier to
support new languages, because you only need to add a new resource file for each language that can be
created by a professional translator.
MCT USE ONLY. STUDENT USE PROHIBITED
02-14 Designing ASP.NET MVC 4 Web Applications

Using Separate Views


Some developers prefer to use separate, parallel sets of views for each supported language code. If you
use this approach, you must insert code into the controllers to detect the preferred language that the user
has specified. After this, you can use this value to render the correct view.

When you use separate views to globalize and localize a site, views are more readable, because most of
the text and labels remain in the view file. However, you must create view files, which requires you or your
team members to be proficient in the target language.

Which language do you consider would be appropriate to specify in the default resource file?

Discussion: Working on a Design Scenario1


Read the following scenario:

Northwind Traders are a company that imports


and sells wholesale various exotic foods. The
company has hired you to propose a new design
for their main customer-facing web application.
The web application should present the complete
Northwind Traders product list and enable
retailers to order bulk shipments of each product.
Northwind Traders will set up an account for each
retailer partner and supply credentials for access.
Retailers should be able to order multiple product
shipments at a time by using a shopping cart tool.
Anonymous site visitors should be able to browse the product catalog and locate retailers, but not place
orders. When anonymous visitors view a product, use that information to show them thumbnails of similar
products that may interest them.

Discuss the following aspects of a potential solution:


o Domain model: List the domain model objects that you might need to build a suitable solution.

o Database design: List the database tables that you might need to build a suitable solution.

o Architecture: List the layers that you might need to host the solution. Consider whether you
require multiple servers in each layer.

o State management: List the information that you might need to store in session state.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-15

Lesson 2
Designing Models, Controllers, and Views
Models, controllers, and views are the fundamental building blocks of an MVC 4 web application. In a
complex site there may be hundreds of models, views, and controllers. You need to manage these objects
and plan your application well, so that it is easy to manage their organization and internal structure
during development. A thorough plan ensures that you detect any incorrect code and debug problems
rapidly.

Lesson Objectives
After you have completed this lesson, you will be able to:

o Describe how to design models.

o Describe how to design controllers.

o Describe how to design views.

Designing Models
A fundamental activity in the MVC design process
is designing a model. Each model class within the
model represents a kind of object that your
application manages. You cannot plan for
controllers and views until you understand the
structure and design of the model.

Identifying Model Classes and


Properties
The use cases, usage scenarios, or user stories that
you gathered during the analysis phase of the
project should enable you to determine the model
classes that you need to create. Each model class
has a range of properties. For example, here is the use case example shared earlier.
1. The user clicks the Add Photo link on the main site menu.

2. If the user is anonymous, the logon page is shown and the user provides credentials.

3. If the credentials are correct the CreatePhoto view is displayed.

4. The user types a title.

5. The user specifies the photo file to upload.

6. The user optionally types a description for the photo.

7. The user clicks the Upload button.

8. The web application stores the new photo and displays the photo gallery to the user.

This includes the following objects, each of which requires a model class:

o User: The User model class must include properties to store credentials, such as the user name
and password credentials.

o Photo: The Photo model class must include the Title and Description properties.
MCT USE ONLY. STUDENT USE PROHIBITED
02-16 Designing ASP.NET MVC 4 Web Applications

Other use cases similarly help you add new model classes or new properties to the User and Photo model
classes.

Domain Model and Logical Data Model Diagrams


You can use Domain Model Diagrams and Logical Data Model (LDM) diagrams to analyze the information
your website manages and suggest a physical data model or database design. You can return to these
diagrams to plan model classes. Each object in your Domain Model or LDM diagram should be
implemented as an MVC model class. The Domain Model diagram includes not only the names of those
classes, but also the LDM diagram, if you have created one, with property names and data types as well.

Relationships and Aggregates


When you identify the model classes that you will implement in your website, you must also consider the
relationships between them. For example, in the use case of the sample Photo Sharing application, each
Photo is associated with one, and only one, User. This is known as a one-to-one relationship. Each User,
however, can create multiple Photos. This is known as a one-to-many relationship.

Domain Model diagrams and LDM diagrams include such relationships as links between objects. Numbers
at the ends of each link show whether the relationship is one-to-one, one-to-many, or many-to-many.

Aggregates place further limits on the behavior of model classes and clarify relationships. For example, in
a photo sharing application, a Photo is created by a single User. Other users can make multiple comments
on each Photo. If a User deletes a Photo, all the Comments on that photo should also be deleted from the
database. However, the User that created the Photo should not be deleted with the Photo because she
may create other Photos or Comments in the site. In this case, Comments and Photos should be placed in
an aggregate, but users should be outside the aggregates. The Photo is the “root entity” of the aggregate
– this means that deleting a Photo deletes all the associated Comments but deleting a Comment does not
delete the associated Photo.

Entity Framework
The Entity Framework is an Object Relational Mapping (ORM) framework for the .NET Framework-based
applications. An ORM framework maps database tables and views to classes that a developer can program
against by creating instances or calling methods. The Entity Framework has been a part of ADO.NET since
the .NET Framework 3.5.

When you use the Entity Framework in your MVC web application, it maps tables or views to the model
classes that you have planned. You do not need to write SQL code to query or update database tables
because the Entity Framework does this for you. The Entity Framework is well integrated with the
Language Integrated Query (LINQ) query language.
If you plan to use the Entity Framework for data access, you should decide on how the database will be
created during the planning phase:

o Database-First: Use the Entity Framework in the database-first mode when you have a pre-
existing database to work with. This may happen because you already have data from an earlier
system or because a DBA has designed the database for you. You can also choose this mode if
you are familiar with creating databases in a database administration tool, such as Microsoft SQL
Server Management Studio. When you use this mode, you have to specify the database
connection string. The Entity Framework connects to the database and examines the database
schema. It creates a set of classes for you to use for data access.
o Model-First: Use the Entity Framework in the model-first mode when you do not have a pre-
existing database and prefer to design your model in Visual Studio. You can use the Entity
Designer tool to name, configure, and link your model classes. This creates XML files which the
Entity Framework uses both to create model classes and to create the database with its tables and
relationships.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-17

o Code-First: Use the Entity Framework in the code-first mode when you have no pre-existing
database and prefer to design your models entirely in C# code. Your code must include
DBContext and DBSet objects – these correspond to the database and its tables. When you run
the application for the first time, the Entity Framework creates the database for you.

Design in Agile and Extreme Programming


Agile and Extreme Programming projects are characterized by short design phases in which data models
are not completed. Instead, a simple design, with little detail, is created and developers fill in details as
they build code by continuously discussing requirements with users and other stakeholders.

In an MVC project, this means that you identity the model names and relationships during the design
phase. You can document these on a Domain Model UML diagram. However, you can leave details such
as property names and data types to be finalized in the development phase along with the complete LDM
diagrams.

The Entity Framework lets you work in the Agile or Extreme Programming styles. For example, the
framework can update the database when the model changes. Alternatively the framework can update
the model when the database changes. The Entity Framework can perform these updates in any Entity
Framework mode.

Designing Controllers
In an ASP.NET MVC web application, controllers
are .NET Framework-based classes that inherit
from the System.Web.Mvc.Controller base class.
They implement input logic – this means they
receive input from the user in the form of HTTP
requests and select both the correct model and
view to use to formulate a response.

Identify Controllers and Actions


In an ASP.NET MVC web application, there is
usually one controller for each model class.
Conventionally, if the model class is called
“Photo”, the controller is called “PhotoController”.
If you follow this convention in your design, you can use the MVC default routing behavior to select the
right controller for a request.

However, for each controller there can be many actions – each action is implemented as a method in the
controller and usually returns a view. You often require separate actions for GET and POST HTTP request
verbs. Similar to designing a model, you can identify the actions to write in each controller by examining
the use cases you gathered during analysis. For example, here is the use case shared earlier.

1. The user clicks the Add Photo link on the main site menu.

2. If the user is anonymous, the logon page is shown and the user provides credentials.

3. If the credentials are correct the CreatePhoto view is displayed.

4. The user types a title.

5. The user specifies the photo file to upload.


6. The user optionally types a description for the photo.
MCT USE ONLY. STUDENT USE PROHIBITED
02-18 Designing ASP.NET MVC 4 Web Applications

7. The user clicks the Upload button.

8. The web application stores the new photo and displays the photo gallery to the user.

You have already identified Photo and User model classes from this use case. Adhering to the MVC
convention, you should create a PhotoController and a UserController. The use case shows that the
following actions are needed for each controller:

Controller Action Description

Photo AddPhoto (GET) The AddPhoto action for GET requests creates a new instance
of the Photo model class, sets default values such as the
created date, and passes it to the correct view.

AddPhoto (POST) The AddPhoto action for POST requests calls Photo model
class methods to save the photo values to the database and
redirects the browser to the DisplayGallery action.

DisplayGallery (GET) The DisplayGallery action for GET requests displays all the
photos stored in the database.

User Logon (GET) The Logon action for GET requests displays a view that an
anonymous user can enter credentials into.

Logon (POST) The Logon action for POST requests checks user credentials
against the membership database. If they are correct, the
action authenticates the user and redirects to the originally
requested page.

Design in Agile and Extreme Programming


Similar to the design of models, you will only make generalized plans for controllers during the design
phase of an Agile Development or Extreme Programming project. Because you have identified the model
classes necessary, you can plan the required controllers. However, you should specify only a few actions at
this stage.

Designing Views
The user interface is a vital component of any
system because it is the part that the users,
budget holders, and other stake holders see and
interact with. Users are most interested in getting
this part of the application right and frequently
have the most to say about its design. As a
developer, you have a chance to impress your
users by designing and implementing a
sophisticated user interface, which may result in
more business.

In an ASP.NET MVC web application, the user


interface is created by building views.

Views, Template Views, and Partial Views


There is a many-to-one relationship between MVC controllers and views. For example, a controller may
use one view to display a single photo, another view to display several photos, and a third view to enable
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-19

users to upload new photos. Each view corresponds to a web page that the application can display to the
user, although it can display different data. For example, the PhotoDetails view can display different
photos, depending on the ID parameter that is passed to it.

As you plan views, you should also consider parts of the user interface that appear on all pages. For
example, the company logo, main site menu, links to legal information, and log on controls may need to
appear on every page. You can place these user interface components in a template view to create a
consistent look and feel across pages.

Note: Template views in ASP.NET MVC web applications perform the same role as master pages in
ASP.NET Web Forms applications.

Some user interface components do not appear on all pages but are reused on several pages. For
example, comments may be displayed in a single photo display, on the gallery, or on other pages. By
creating a partial view, you can create a reusable user interface element that can appear in many locations
in this manner, without duplicating code.

Creating Wireframes
A common technique to discuss and plan the user interface for your application is to create wireframe
diagrams. These are simplified layouts that show where the elements will appear on the final webpages.
They are intended to communicate to users the essential functional parts of the user interface, but do not
include graphics, logos, or colors. It is not necessary to create a wireframe model for every view in your
application but for only the most important ones.

You can begin a wireframe diagram by sketching it on a whiteboard in conversation with a user. Many
tools are available to create more formal versions—for example, Microsoft Visio has excellent wireframe
drawing capabilities.

Design in Agile and Extreme Programming

You do not design many parts of the user interface during the initial phases of Agile Development or
Extreme Programming projects. Instead, you design views and partial views in close consultation with
users during the development phase. This applies even to the template view that displays common
components of your user interface, although it is likely that the template view is one of the first user
interface components that is designed and created. You will create the template view during the early
iterations of the development phase of the project.
MCT USE ONLY. STUDENT USE PROHIBITED
02-20 Designing ASP.NET MVC 4 Web Applications

Lab: Designing ASP.NET MVC 4 Web Applications


Scenario
Your team has chosen ASP.NET MVC 4 as the most appropriate ASP.NET programming model to create
the photo sharing application for the Adventure Works web application. You need to create a detailed
project design for the application, and have been given a set of functional and technical requirements
with other information. You have to plan:

o An MVC model that you can use to implement the desired functionality.

o One or more controllers and controller actions that respond to users actions.

o A set of views to implement the user interface.

o The locations for hosting and data storage.

Objectives
After completing this lab, you will be able to:

o Design an ASP.NET MVC 4 web application that meets a set of functional requirements.
o Document the design in an accurate, precise, and informative manner.

Estimated Time: 40 minutes

Virtual Machine: 20486A-SEA-DEV11


Username: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Exercise 1: Planning Models


Scenario
You need to recommend an MVC model that is required to implement a photo sharing application. You
will propose model classes based on the results of an initial investigation into the requirements.

The main tasks for this exercise are as follows:


1. Examining the initial investigation.

2. Planning the photo model.

3. Planning the comment model.

 Task 1: Examining the initial investigation.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin


o Password: Pa$$w0rd

2. Open the InitialInvestigation document by using the following information:

o File location: Allfiles (D):\Labfiles\Mod02

3. Enable the Navigation Pane feature.

4. Read the contents of the Introduction section.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-21

5. Read the contents of the General Description section.

6. Read the Use Cases section and then examine the Use Case Summary diagram.

7. Close the InitialInvestigation document.

 Task 2: Planning the photo model.


1. Open the DetailedPlanningDocument document and locate the MVC Model section.

2. Based on your reading of the InitialInvestigation document, add and describe a model class for
photos in Table 1: MVC Model.

3. Add properties to the model class you created in Table 1: MVC Model.

4. Add data types to the photo properties.


5. Merge the rows in the Model Class and Description columns and save the document.

6. Create a new UML Logical Data Model diagram in Visio 2010.

7. Add a new Class shape to model photos in the UML diagram.

8. Add attributes to the new Class shape for each of the properties you planned for photos.

9. Save the created diagram by using the following information:

o Folder path: Allfiles (D):\Labfiles\Mod02


o File name: PhotoSharingLDM

 Task 3: Planning the comment model.


1. Open the DetailedPlanningDocuement document and locate the MVC Model section.

2. Based on your reading of the InitialInvestigation document, add and describe a model class for
photos in Table 1: MVC Model.

3. Add properties to the model class you created in Table 1: MVC Model.

4. Add data types to the comment properties.

5. Merge the rows in the Model Class and the Description columns, and then save the document.

6. Add a new Class shape to model comments in the UML diagram.

7. Add attributes to the new Class shape for each of the properties you planned for comments.

8. In the UML diagram, connect the two class shapes.

9. Hide the end names for the connector.

10. Set multiplicity for the ends of the connector, and save the diagram.

Results: After completing this exercise, you will be able to create proposals for a model and configure the
properties and data types of the model classes.

Exercise 2: Planning Controllers


Scenario
You need to recommend a set of MVC controllers that are required to implement a photo sharing
application. You will propose controllers based on the results of an initial investigation into the
requirements.
MCT USE ONLY. STUDENT USE PROHIBITED
02-22 Designing ASP.NET MVC 4 Web Applications

The main tasks for this exercise are as follows:

1. Planning the photo controller.

2. Planning the comment controller.

 Task 1: Planning the photo controller.


1. Open the DetailedPlanningDocument document and locate the MVC Controllers section.

2. Based on your reading of the InitialInvestigation document, add a controller for photos in Table 2:
MVC Controllers.

3. Add actions to the controller for photos in Table 2: MVC Controllers.

4. Add descriptions for each of the actions you have planned.


5. Merge rows in the Controller column and save the document.

 Task 2: Planning the comment controller.


1. Based on your reading of the InitialInvestigation document, add a controller for photos in Table 2:
MVC Controllers.

2. Add actions to the controller for comments in Table 2: MVC Controllers.

3. Add descriptions for each of the actions you have planned.

4. Merge rows in the Controller column and save the document.

Results: After completing this exercise, you will be able to create proposals for controllers and configure
their properties and data types.

Exercise 3: Planning Views


Scenario
You need to recommend a set of MVC views that are required to implement a photo sharing application.
You will propose views based on the results of an initial investigation into the requirement.

The main tasks for this exercise are as follows:

1. Planning the single photo view.

2. Planning the gallery view.

 Task 1: Planning the single photo view.


1. Add a controller to the Table 3: MVC Views table.

2. Add the required views to the Controllers.


3. Add a description to the views.

4. Merge rows in the Controller column and save the document.

5. Create a new wireframe diagram in Visio 2010.

6. Add a new Application Form shape to the wireframe diagram.

7. Add a menu to the wireframe diagram.

8. Add a panel for the photo to the wireframe diagram.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-23

9. Save the diagram by using the following information:

o File location: Allfiles (D):\Labfiles\Mod02

o File name: SinglePhotoWireframe

 Task 2: Planning the gallery view.


1. Create a new wireframe diagram in Visio 2010.

2. Add a new Application Form shape to the wireframe diagram.

3. Add a menu to the wireframe diagram.

4. Add multiple panels to the photo to the wireframe diagram.

5. Save the diagram by using the following information:

o File location: Allfiles (D):\Labfiles\Mod02

o File name: PhotoGalleryWireframe

Results: After completing this exercise, you will create proposals for views and their layouts.

Exercise 4: Architecting an MVC Web Application


Scenario
You need to recommend a web server and database server configuration that is required to implement a
photo sharing application. You will propose details based on the results of an initial investigation into the
requirements.
The main tasks for this exercise are as follows:

1. Hosting options.

2. Choosing a data store

 Task 1: Hosting options.


1. Based on your reading of the InitialInvestigation document, add a description of the web server
arrangements that are suited to host the photo sharing application.

 Task 2: Choosing a data store


1. Based on your reading of the InitialInvestigation document, add a description of the database
server arrangements that are suited to host the photo sharing application.

Results: After completing this exercise, you will be able to create proposals for hosting arrangements.

Question: What model classes should be created for the photo sharing application based on
the initial investigation?

Question: What controllers should be created for the photo sharing application based on
the initial investigation?

Question: What views should be created for the photo sharing application?
MCT USE ONLY. STUDENT USE PROHIBITED
02-24 Designing ASP.NET MVC 4 Web Applications

Module Review and Takeaways


In this module, you have seen how teams of developers, software architects, and business analysts
collaborate to design an MVC web application that addresses the needs of users. You have seen how
functional and technical requirements are gathered by talking to stakeholders and creating use cases,
usage scenarios, and user stories. The model, views, controllers, and other aspects of the design depend
on these requirements. You have also seen how these design activities are completed in projects that use
the agile methodology or extreme programming.

Best Practice:

In Agile Development and Extreme Programming projects, developers discuss with users and
stakeholders throughout development to ensure that their code will meet changing
requirements. Even if you are not formally using these methodologies, it is good practice to
regularly communicate with users.

Best Practice:

When you design an ASP.NET MVC web application, start with the model, and then plan
controllers, actions, and views. The controllers, actions, and views that you create each depend on
the model.

Common Issues and Troubleshooting Tips

Issue Troubleshooting Tip

When you create a Use Agile Development and Extreme Programming


very detailed project methodologies. Such methodologies, which are based
plan, much of your on the real-world assumption that requirements will
work is wasted when change frequently, are proven to prevent the time
requirements change wastage that often occurs when complete designs are
late in the project. altered during the development phase.
Delete

Review Question(s)
Question: You want to support both English and Spanish on your web application. You have
both Spanish-speaking and English-speaking developers and want to ensure that views
remain readable as easily as possible. Should you use multiple view files or multiple resource
files to globalize your site?

Real-world Issues and Scenarios


You should bear in mind that when you select a project methodology, that few projects follow a neat plan
in real situations. Of the methodologies described in this module, agile development and extreme
programming are the most flexible and respond when plans change in the middle of development.
However, even with these methodologies, changing circumstances result in wasted development time and
your project budget should include a contingency to cope with such changes.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 02-25

Furthermore, when working with agile development and extreme programming projects, project
managers must take care to avoid project creep or scope-creep. This occurs when people add new
requirements when development takes place. Project creep results in projects that are over-budget and
late.

Tools
Microsoft Office Visio: You can use Visio to create all types of UML software design diagrams, including
Domain Model diagrams and LDMs. You can also use it to create wire-frames.

Visual Studio 2012: You can create class diagrams such as LDMs in Visual Studio 2012.
MCT USE ONLY. STUDENT USE PROHIBITED
02-26 Designing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
03-1

Module03
Developing ASP.NET MVC 4 Models
Contents:
Module Overview 03-1 

Lesson 1: Creating MVC Models 03-2 

Lesson 2: Working with Data 03-11 

Lab: Developing ASP.NET MVC 4 Models 03-20 

Module Review and Takeaways 03-28 

Module Overview
Most web applications need to interact with various types of data or objects. An ecommerce application,
for example, manages products, shopping carts, customers, and orders. A social networking application
might manage users, status updates, comments, photos, and videos. A blog manages blog entries,
comments, categories, and tags. When you write an MVC web application, you create an MVC model to
model the data for your web application. Within this model, you create a model class for each type of
object. The model class describes the properties of each type of object and can include business logic that
matches business processes. Therefore, the model is a fundamental building-block in an MVC application
and a good place to start when you write code. Models interact with the information store that underlies
your site, which is usually a database. Therefore, you need to understand data access techniques and
technologies to write models. In this module, you will see how to code models and access data by using
Entity Framework and LINQ.

Objectives
After completing this module, you will be able to:

o Add a Model to an MVC application and write code in it to implement the business logic.

o Create a new SQL Azure database to store data for their web application.
MCT USE ONLY. STUDENT USE PROHIBITED
03-2 Developing ASP.NET MVC 4 Models

Lesson 1
Creating MVC Models
An MVC model is a collection of .NET Framework classes. When you create a model class, you define the
properties and methods that suit the kind of object the model class describes. You can describe these
properties in code so that MVC can determine how to render them in a webpage and how to validate
user input. You need to know how to create and describe models and how to modify the manner in which
MVC creates model class instances when it runs your web application.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe how to create MVC models and develop business logic.

o Use the display and edit data annotations to assign attributes to views and controllers.

o Validate user input with data annotations.


o Describe model binders.

o Describe model extensibility.

o Add a model to an MVC 4 web application.

Developing Models
Every website presents information about various
types of objects. An ecommerce site, for example,
manages products, shopping carts, customers, and
orders. A social networking site might manage
users, status updates, comments, photos, and
videos. A blog manages blog entries, comments,
categories, and tags.

In your web application, you need to define


model classes for these objects. When you
implement a functional requirement in your web
application, you usually start by creating a model
class. The model class will probably be revised
when you create the controller and views for the model, and then later during the iterations that happen
during the project. If you follow the Agile development model or Extreme Programming, you begin with a
simple understanding of the class—perhaps its name and a few properties. Then, you discuss with users
and add details to the planned model class with the complete set of properties and its relationships to
other classes. When developing the model, you can refer to use cases or user stories to ensure that these
model details are correct.

When you feel that you fully understand the requirements for a model, you can write model classes to
implement the requirements. The following lines of code illustrate how to create an example model class
named Photo.

Example Model Class


public class Photo
{
public int PhotoID { get; set; }
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-3

public string Title { get; set; }


public byte[] PhotoFile { get; set; }
public string Description { get; set; }
public DateTime CreatedDate { get; set; }
public string Owner { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}

Notice that the model class does not inherit from any other class. Also, notice that you have created
public properties for each property in the model and included the data type, such as integer or string in
the declaration. You can create read-only properties by omitting the set; keyword.

The Photo class includes a property called Comments. This is declared as a collection of Comment
objects and implements one side of the relationship between photos and comments.

The following lines of code illustrate how you can implement the Comment model class.

Comment Model Class


public class Comment
{
public int CommentID { get; set; }
public int PhotoID { get; set; }
public string UserName { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public virtual Photo Photo { get; set; }
}

Notice that the Comment class includes a PhotoID property. This property stores the ID of the Photo
that the user commented on and ties the comment to a single photo. Also, notice that the Comment
class includes a Photo property, which returns the Photo object that the comment relates to. These
properties implement the other side of the relationship between photos and comments. The instances of
model classes are usually created in a controller action and passed to a view to display.
This code shows how a controller action can create a new photo object from the photo model class and
pass it to the Display view.

Instantiating a Photo in a Controller Action


Photo newPhoto = new Photo();
newPhoto.Title = "This is an Example Photo";
newPhoto.Owner = User.Identity.Name;
newPhoto.CreatedDate = DateTime.Today;
return View("DisplayView", newPhoto);

In the Display view, you can render the Title property of a photo by using the Model object in the Razor
code that you write.

Rendering a Property from a Model Class


<div id="photo-title">
@Model.Title
</div>
MCT USE ONLY. STUDENT USE PROHIBITED
03-4 Developing ASP.NET MVC 4 Models

Using Display and Edit Data Annotations on Properties


The model classes usually specify three attributes
for each property:

o The name of the property, for example,


Title.

o The data type of the property, for


example, String.

o The access levels of the property, for


example, the get and set keywords to
indicate read and write access.

Additionally by using attributes, you can supply


additional metadata to describe properties to
ASP.NET MVC. The MVC runtime uses this metadata to determine how to render each property in views
for display and editing. These attributes are called display and edit annotations.

For example, property names in C# cannot contain spaces. On a rendered webpage, you may often want
to include spaces in a property label. For example, you might want to render a property called
CreatedDate with the label Created Date. To provide MVC with this information, you can use the
DisplayName annotation.

When you use MVC, you can indicate how you want a property to be named on a view by using the
DisplayName annotation as the following lines of code illustrate.

Setting the DisplayName Annotation


[DisplayName("Created Date")]
public CreatedDate { get; set; }

If you have a DateTime property, you can use display and edit data annotations to inform MVC what
format you want the property displayed in.

In the following lines of code, the CreatedDate property is a DateTime and the DisplayFormat data
annotation is used to indicate to MVC that only the day, month and year values should be displayed.

Setting the DataType and DisplayFormat


[DisplayName("Created Date")]
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yy}", ApplyFormatInEditMode = true)]
public DateTime CreatedDate { get; set; }

All the data annotations that are provided with ASP.NET MVC 4 are included in the
System.ComponentModel.DataAnnotations namespace.

Additional Reading: To read more about all the data annotations provided with MVC 4,
see http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx

Question: In the code on the slide, how can you recognize the display and edit annotations and
distinguish them from other code?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-5

Validating User Input with Data Annotations


You can use data annotations in MVC models to
set validation criteria for user input. Input
validation is the process by which MVC checks
data provided by a user or a web request to
ensure that it is in the right format. The following
example shows a webpage form that collects
some information from the user:

o Name. This is required input. The user


must enter some data in this field.
o Height. This must be an integer between
0 and 400.

o Email Address. This is required input. The value entered must be a valid email address.
In this example, when the user submits the form, you want MVC to create a new instance of the Person
model and use it to store the data in the database. However, you want to ensure that the data is valid
before it is stored.

To ensure that the data is valid, you can define the Person model class by using the following lines of
code:

Using Validation Data Annotations


public class Person
{
public int PersonID { get; set; }
[Required]
public string Name { get; set; }
[Range(0, 400)]
public int Height { get; set; }
[Required]
[RegularExpression(".+\\@.+\\..+")]
public string EmailAddress { get; set; }
}

The Required, Range, StringLength, and RegularExpression annotations implement input validation in
MVC. If users do not enter data that satisfies the criteria specified for each property, the view displays a
standard error message that prompts the user to enter the correct data. In the earlier example, you can
see that the user must enter a Name and a Height between 0 and 400. For the EmailAddress property,
the user must enter a value that matches the regular expression. The regular expression in the example is
a simple expression that requires an @ symbol and a dot.

To specify the error message that the user sees when data is not valid, you can use the ErrorMessage
property on the validation data annotations as the following code illustrates.

Setting a Validation Error Message


[Required(ErrorMessage="Please enter a name.")]
public string Name { get; set; }

Note: You will see how to ensure that the validation error message is displayed in a view.
Additional Reading: For more examples of validation data annotations see:
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-
annotation-validators-cs
MCT USE ONLY. STUDENT USE PROHIBITED
03-6 Developing ASP.NET MVC 4 Models

Additional Reading: For more information about the regular expressions that you can use
to check user input, see: http://msdn.microsoft.com/en-us/library/hs600312.aspx

Question: You want to make sure that users enter a password that is longer than 6 characters. How could
you do this by using a validation data annotation?

What Are Model Binders?


A model binder is a component of an ASP.NET
MVC application that creates an instance of a
model class, based on the data sent in the request
from the web browser. ASP.NET MVC includes a
default model binder that meets the needs of
most web applications. However, you must know
how the default model binder works with other
components to use it properly. In addition, you
may choose to create a custom model binder for
advanced situations.

What Does a Model Binder Do?


A model binder ensures that the right data is sent
to the parameters in a controller action method. This enables MVC to create instances of model classes
that satisfy the user’s request. The default model binder, for example, examines both the definition of the
controller action parameters and the request parameters to determine with request values to pass to
which action parameter.

This model binding process can save developers a lot of time and avoid many unexpected runtime errors
that arise from incorrect parameters. MVC includes a default model binder with sophisticated logic that
passes parameters correctly in almost all cases without complex custom code.

The Controller Action Invoker and The Default Model Binder


To understand the default model binding process, consider the following request from a web browser:

http://www.adventureworks.com/product/display/45
This request identifies three aspects:

o The model class that interests the user. The user has requested a product.

o The operation to perform on the model class. The user has requested that the product be
displayed.

o The specific instance of the model class. The user has requested that the product with ID 45 is
displayed.

The request is received by an object called the controller action invoker. The controller action invoker of
the MVC runtime calls a controller action and passes the correct parameters to it. In the example, the
action invoker calls the Display action in the Product controller and passes the ID “45” as a parameter to
the action, so that the right product can be displayed.

The ControllerActionInvoker class is the default action invoker. This action invoker uses model binders
to determine how parameters are passed to actions.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-7

How The Default Model Binder Passes Parameters


In a default MVC application, there is only one model binder for the ControllerActionInvoker to use.
This binder is an instance of the DefaultModelBinder class. The default model binder passes parameters
by using the following logic:

1. The binder examines the definition of the action that it must pass parameters to. In the example, the
binder determines that the action requires an integer parameter called PhotoID.

2. The binder searches for values in the request that can be passed as parameters. In the example, the
binder searches for integers because the action requires an integer. The binder searches for values in
the following locations in order:

a. Form Values: If the user fills out a form and clicks a submit button, you can find parameters in the
Request.Form collection.

b. Route Values: Depending on the routes that you have defined in your web application, the model
binder may be able to identify parameters in the URL. In the example URL, “45” is identified as a
parameter by the default MVC route.
c. Query Strings: If the user request includes named parameters after a question mark, you can find
these parameters in the Request.QueryString collection.

d. Files: If the user request includes uploaded files, these can be used as parameters.
Notice that if there are form values and route values in the request, form values take precedence. Query
string values are only used if there are no form values and no route values available as parameters.

Model Extensibility
The MVC architecture has been designed to
provide extensibility so that developers can adapt
the architecture to unusual or unique
requirements. For example, the default action
invoker, ControllerActionInvoker, can be
replaced by your own action invoker if you want
to implement your own invoking logic.

Two ways in which you can extend the MVC


handling of MVC models are to create custom
data annotations and two create custom model
binders.

Custom Validation Data Annotations


You can use data annotations to indicate to MVC how to validate the data a user enters in a form or
passes in query strings. The four built-in validation attributes in MVC 4, Required, Range, StringLength,
and RegularExpression, are very flexible. However, in some situations, such as the following examples,
you may want to run some custom validation code:

o Running a Data Store Check. You want to check the data entered against data that has already
been stored in the database or in another database store.

o Comparing Values. You want to compare two entered values with each other.

o Mathematical Checks. You want to calculate a value from the entered data and check that value
is valid.
MCT USE ONLY. STUDENT USE PROHIBITED
03-8 Developing ASP.NET MVC 4 Models

In such situations you can create a custom validation data annotation. To do this, you create a class that
inherits from the System.ComponentModel.DataAnnotations.ValidationAttribute class.
The following lines of code illustrate how to create a custom validation data annotation.

Creating Custom Validation Data Annotation


[AttributeUsage(AttributeTargets.Field)]
public class LargerThanValidationAttribute : ValidationAttribute
{
public int MinimumValue { get; set; }
//Constructor
public LargerThanValidationAttribute (int minimum)
{
MinimumValue = minimum;
}
//You must override the IsValid method to run your test
public override Boolean IsValid (Object value)
{
var valueToCompare = (int)value;
if (valueToCompare > MinimumValue)
{
return true;
}
else
{
return false;
}
}
}

After you have created a custom validation data attribute, you can use it to annotate a property in your
model as the following lines of code illustrate.

Using a Custom Validation Data Annotation


[LargerThanValidationAttribute(18)]
public VoterAge { get; set; }

Custom Model Binders


The default controller action invoker receives requests and calls the right action on the right controller to
satisfy the request. The default action invoker uses model binders to identify parameters in the request
and pass all of them to the right parameters in the action. This mechanism ensures that query strings,
route values, form values, and uploaded files are available for the action to work with.

The default model binder is sophisticated and flexible, but sometimes you may want to customize its
behavior to pass parameters in an unusual way. You can do this by creating a custom model binder and
registering it with the MVC runtime.

The following lines of code show how to create a simple custom model binder to pass parameters from
the form collection by implementing the IModelBinder interface.

A Simple Custom Model Binder


public class CarModelBinder : IModelBinder
{
public object BindModel (ControllerContext controllerContext, ModelBindingContext
bindingContext)
{
//Get the color for the car from the request form
string color = controllerContext.HttpContext.Request.Form["color"];
//Get the brand for the car from the request form
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-9

string brand= controllerContext.HttpContext.Request.Form["brand"];


//Create a new instance of the car model
Car newCar = new Car();
newCar.color = color;
newCar.brand = brand;
//return the car
return newCar;
}
}

The code example assumes that you have a model class in your MVC application called Car. It also
assumes that any request for a Car object includes values for color and brand in the form collection. This
situation can easily be handled by the default model binder. However, this example demonstrates how
model binders can locate values from the context of the request and pass those values to the right
properties in the model. You can add custom code to implement extra functionality.

Additional Reading: You can see more examples of custom model binders at the
following locations:
http://www.mgolchin.net/posts/20/dive-deep-into-mvc-imodelbinder-part-1
http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

Question: You want to ensure that, when a user types a value into the Car Model Number textbox when
adding a new car to the website, the text entered is not already used by another car in the database.
Would you use a custom validation data annotation or a custom model binder for this?

Demonstration: How to Add a Model


In this demonstration, you will see how to create a model in an ASP.NET MVC 4 web application and add
model classes to the web application. You will also see how to add data annotations and create a custom
validation data annotation.

Demonstration Steps
1. Create a new ASP.NET MVC 4 Web Application by using the following information:

o Name: OperasWebSite

o Location: Allfiles (D):\Democode\Mod03

o Template: Empty

2. Add a new model class to the OperasWebSite project by using the following information:

o Class name: Opera

3. Add the following properties and the corresponding data types to the Opera model class:

o OperaID, integer

o Title, string

o Year, integer

o Composer, string

4. Add the following data annotations to the Title property:


o Required
MCT USE ONLY. STUDENT USE PROHIBITED
03-10 Developing ASP.NET MVC 4 Models

o StringLength(200)

5. Add the Required data annotation to the Composer property.

6. Add the following custom validation data annotation to the model:

o CheckValidYear

7. In the CheckValidYear class, override the IsValid method. The if else conditions are as follows:

o Condition 1: If the year is earlier than 1598, return false.

o Condition 2: If the year is after than 1598, return true.

8. Define a constructor in the CheckValidYear class by using the following information:

o Constructor name: ErrorMessage

o Message: The earliest opera is Daphne, 1598, by Corsi, Peri, and Rinuccini.

9. Add the CheckValidYear data annotation to the Year property of the Opera class, and then build the
web application.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-11

Lesson 2
Working with Data
All web applications present information and almost all web applications require a data store for that
information. By rendering webpages using data from a data store, you can create a web application that
changes continually in response to user input, administrative actions, and publishing events. The data
store is usually a database but other data stores are occasionally used. In MVC applications, you can create
a model that implements data access logic and business logic. Alternatively, you can separate business
logic, in model classes, from data access logic, in a repository. A repository is a class that a controller can
call to read and write data from and to a data store. The .NET Framework includes the Entity Framework
and LINQ technologies, which make data access code very quick to write and simple to understand. In this
lesson, you will see how to build a database driven website in MVC.

Lesson Objectives
After completing this lesson, you will be able to:

o Connect an application to a database to access and store data.

o Describe the features of the Entity Framework.


o Use LINQ to write queries for selecting, filtering, and grouping data.

o Create separate model classes and corresponding repository classes by using Entity Framework
Code.
o Explain how to access data in models and repositories.

Connecting to a Database
Most websites use a database to store dynamic
data. By including this data in rendered HTML
pages, you can create a dynamic web application
with content that changes frequently. For
example, you can provide administrative
webpages that enable company employees to
update the product catalog and publish news
items. Products and items are stored in the
database. As soon as they are stored, users can
view and read them. The employees do not need
to edit HTML or republish the website to make
their changes.

Some websites may store data in other locations, such as a directory service, but databases are by far the
most widely used data store.

ADO.NET and Databases


When you create .NET Framework applications, including MVC web applications, you can use the
ADO.NET technology to access databases. ADO.NET classes are contained in the System.Data namespace.
ADO.NET supports a wide range of databases by using different data providers. For example:

o Microsoft SQL Server. This is Microsoft’s industry-leading database server. ADO.NET includes the
SqlClient provider for all SQL Server databases.
MCT USE ONLY. STUDENT USE PROHIBITED
03-12 Developing ASP.NET MVC 4 Models

o Microsoft SQL Server Express. This is a free version of SQL Server which includes a wide range of
database functionality and is very flexible. Some advanced capabilities, such as database
clustering, are not possible with SQL Express. The SqlClient provider is used for SQL Express.

o Microsoft SQL Server Compact. This version of SQL is also free and uses .sdf files to store data on
the hard disk. ADO.NET includes the SqlServerCe provider for SQL Compact databases.
o Oracle Databases. This is a widely-used database server. ADO.NET includes the OracleClient
provider for all Oracle databases.

o OLE DB. This is a standard that many different databases adhere to. ADO.NET includes the OleDb
provider for all OLE DB databases.

o ODBC. This is another older standard that many different databases adhere to. ADO.NET includes
the Odbc provider for all ODBC databases. In general, you should use an OLE DB provider if it is
available instead of an ODBC provider.

You can also use third-party ADO.NET providers to access other databases.

Cloud Databases
The database of a web application is usually located on the same server as the web application itself or on
a dedicated database server at the same physical site. However, if you have a fast, reliable Internet
connection you can consider using a cloud database. Furthermore, if you have chosen to host a web
applicationin the cloud, a cloud database is a logical storage solution. Microsoft’s cloud database service is
called SQL Database and is a part of Windows Azure.

Windows Azure SQL Database has the following advantages:


o Databases run in Microsoft datacenters with the best connectivity and reliability.

o Microsoft guarantees 99% up time.

o You do not need to build and maintain your own database servers or employ database
administrators.

o You can scale up the databases very easily.

o You pay only for the data that you use and distribute.

You can use Windows Azure SQL Database with ADO.NET by using the SqlClient provider.

Connecting an MVC Web Application to a Database


To use ADO.NET and connect to a database, you need to add two things to your application:

o Add a reference to the System.Data namespace.

o Add a connection string to the web.config file. This string specifies the provider, the location of
the database, security properties, and other properties depending on the provider.

The following markup shows how to add a connection string to web.config to connect to a SQL Express
database called PhotoSharingDB, by using the credentials the web application runs under.

Connecting to SQL Express


<connectionStrings>
<add name="PhotoSharingDB"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=PhotoSharingDB;" +
"Integrated Security=SSPI"
providerName="System.Data.SqlClient"/>
</connectionStrings>

The following connection string connects to a Windows Azure SQL Database.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-13

Connecting to Windows Azure SQL Database


<connectionStrings>
<add name="PhotoSharingDB"

connectionString="Server=tcp:example.database.windows.net,1433;Database=PhotoSharingDB;"
+
"User ID=Admin@example;Password=Pa$$w0rd;Trusted_Connection=False;" +
"Encrypt=True;Connection Timeout=30;PersistSecurityInfo=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>

The <connectionStrings> tag must appear within the <configuration> tag, after the <configSections>
tag.

Additional Reading: The MVC web application templates in Microsoft Visual Studio 2012
include ADO.NET references so you need not add them. However, you must add connection
strings. Some of the templates, such as the Internet site template, include SQL Server Express
membership databases with connection strings.

The Entity Framework


Developers write code that works with classes and
objects. By contrast, databases store data in tables
with columns and rows. Database administrators
create and analyze databases by running
Transact-SQL queries. You can choose to build
and run Transact-SQL queries in your ADO.NET
code. However, ADO.NET includes the Entity
Framework—this technology enables you to read
and write data to and from a database by using
classes and objects.

Entity Framework is an Object Relational Mapping


(ORM) framework. An ORM framework maps the
tables and columns found in a database to objects and their properties that you can call from .NET code.

Entity Framework Workflows


The way you use Entity Framework in your application depends on the manner in which you want to build
your database. The three Entity Framework workflows available are database-first, model-first, and code-
first:

o Database First. You can use the database-first workflow when you have a pre-existing database
or if you prefer to create a new database by defining table, columns, views, and other database
schema objects. In this workflow, Entity Framework examines the database and creates an XML
file with an .edmx extension called the model file. The model file describes classes that you will be
able to work with in code. You can adjust the model by using a designer in Visual Studio and
then start writing code against the Entity Framework classes.

o Model First. You can use the model-first workflow when you do not yet have a database and you
prefer to design your model using an ORM modeling tool. In this workflow, you create the .edmx
file in the Visual Studio designer and then write code against the model classes generated by the
designer. When you run the application, Entity Framework creates the database tables and
MCT USE ONLY. STUDENT USE PROHIBITED
03-14 Developing ASP.NET MVC 4 Models

columns to support the model. In this workflow, Visual Studio can also create a connection string
for you and insert it into web.config, based on the database that you specify.

o Code First. You can use the code-first workflow if you prefer to create a model by writing .NET
Framework classes. In this workflow, there is no model file. Instead, you create model classes in
C# or VB. When you run the application, the Entity Framework creates the database.

Adding an Entity Framework Context


When you use Entity Framework in the code first workflow, you must ensure that the framework creates
the right database and tables to store your model classes. To do this, create a class that inherits the Entity
Framework DbContext class. You will use this class in controllers when you want to manipulate data in
the database. Within this class, add a DbSet<> property for each database table you want Entity
Framework to create in the new database.
The following code shows how to add an Entity Framework context to your model.

An Entity Framework Context Class


public class PhotoSharingDB : DbContext
{
public DbSet<Photo> Photos { get; set; }
public DbSet<Comment> Comments { get; set; }
}

In the earlier example, Entity Framework looks for a connection string with the name PhotoSharingDB, to
match the name of the DbContext class. Entity Framework creates the database at the location that
connection string provides and creates two tables in the new database:
o Photos: This table will have columns that match the properties of the Photo model class.

o Comments: This table will have columns that match the properties of the Comment model class.

Question: You have a Visio diagram which a business analyst created that shows all the model classes for
your web application and their relationships. You want to recreate this diagram in Visual Studio. Which
Entity Framework workflow would you use?

Using an Entity Framework Context


Now that you have defined the Entity Framework
context and model classes, you can use them in
MVC controllers to pass data to views for display.

The following code shows how to use the Entity


Framework context in a controller to pass a single
photo, or a collection or photos, to views.

Using an Entity Framework Context in a


Controller
public class PhotoController : Controller
{
//Creating a reference to the Entity
Framework context class
private PhotoSharingDB db = new PhotoSharingDB();
//This action gets all the photos in the database and passes them to the Index view
public ActionResult Index()
{
return View("Index", db.Photos.ToList());
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-15

}
//This action gets a photo with a particular ID and passes it to the Details view
public ActionResult Details(int id = 0)
{
Photo photo = db.Photos.Find(id);
if (photo == null)
{
return HttpNotFound();
}
return View("Details", photo);
}
}
}

Using Initializers to Populate Databases


If you are using the code-first or model-first workflow, Entity Framework creates the database the first
time you run the application and access data. The database remains empty if you have created the
database schema but not populated it with data rows.

You can use an initializer class to populate the database with sample data. This technique ensures there is
sample data to work with during development.

The following lines of code show how to create an initializer class that adds two Photo objects to the
Photos table in the database.

An Example Entity Framework Initializer


public class PhotoSharingInitializer : DropCreateDatabaseAlways <PhotoSharingDB>
{
//Override the Seed method to populate the database
protected override void Seed(PhotoSharingDB context)
{
//Create a list of Photo objects
var photos = new List<Photo>
{
new Photo {
Title = "My First Photo",
Description = "This is part of the sample data",
UserName = "Fred"
},
new Photo {
Title = "My Second Photo",
Description = "This is part of the sample data",
UserName = "Sue"
}
};
//Add the list of photos to the database and save changes
photos.ForEach(s => context.Photos.Add(s));
context.SaveChanges();
}
}

After you have created an initializer, you need to ensure that it runs by adding a line of code to the
Global.asax file in the Application_Start method as the following example illustrates.

Running the Initializer in Global.asax


protected void Application_Start()
{
//Seed the database with sample data for development. This code should be removed for
production.
Database.SetInitializer<PhotoSharingDB>(new PhotoSharingInitializer());
MCT USE ONLY. STUDENT USE PROHIBITED
03-16 Developing ASP.NET MVC 4 Models

Question: You have created an Entity Framework context class in your model, added an initializer and
called Database.SetInitializer() from Global.asax. When you run the application, no database is created
and no model objects displayed in the webpages. What have you forgotten to do?

Using LINQ to Entities


Language Integrated Query (LINQ) is a set of
extensions to Visual C# and Visual Basic that
enable you to write complex query expressions.
You can use these expressions to extract data
from databases, enumerable objects, XML
documents, and other data sources. The
expressions are similar to Transact-SQL queries
but use C# or VB keywords so that you may get
IntelliSense support and error checking in Visual
Studio.

What is LINQ to Entities?


LINQ to Entities is the version of LINQ that works
with Entity Framework. LINQ to Entities enables you to write complex and sophisticated queries to locate
specific data, join data from multiple objects, update data, and take other actions on objects from an
Entity Framework context. If you are using Entity Framework, you can write LINQ queries wherever you
require a specific instance of a model class, a set of objects, or for more complex application needs. You
can write LINQ queries in query syntax, which resembles SQL syntax, or method syntax, in which
operations such as “select” are called as methods on objects.

Example LINQ Queries


In the following lines of code, you can see how to obtain a list of the most recent photos in the database.
Both query syntax and method syntax examples are included.

Using LINQ to Get Recent Photos


//This list will store the photos that are returned
List<Photo> photos;
//This is the Entity Framework context
PhotoSharingDB context = new PhotoSharingDB();
if (number == 0)
{
//If a number of photos is not specified, we’ll get all the photos in the database
//This example is in method syntax.
photos = context.Photos.ToList();
}
else
{
//The number specifies how many of the most recent photos the user requests
//Use a LINQ query with both query and method syntax to get these from the database
photos = (from p in context.Photos
orderby p.CreatedDate descending
select p).Take(number).ToList();
}
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-17

Reference Links: To read many more example LINQ queries, see


http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

Demonstration: How to Use Entity Framework Code


In this demonstration, you will see how to add a connection string, an Entity Framework context, and an
initializer to the web application.

Demonstration Steps
1. Add a connection string to the web.config file by using the following information:

o Name: OperasDB

o ConnectionString:
Data Source=(LocalDB)\v11.0;
AttachDbFilename=
|DataDirectory|\Operas.mdf;
Integrated Security=True

o Provider Name: System.Data.SqlClient

2. Use the Nuget Package Manager to add Entity Framework 5.0 to the application.

3. Add a new class called Operas DB to the Models folder and ensure that the new class inherits the
System.Data.Entity.DbContext class.

4. Add a DbSet property to the class Operas.

5. Add a new class called OperaInitializer to the Models folder and ensure that the new class inherits
the DropCreateDatabaseAlways
<OperasDB> class.

6. Override the Seed method in the OperasInitializer class.

7. Create a new list of Opera objects in the Seed method. The list should contain one opera object with
the following properties:

o Title: Cosi Fan Tutte

o Year: 1790

o Composer: Mozart

8. Build the web application.


MCT USE ONLY. STUDENT USE PROHIBITED
03-18 Developing ASP.NET MVC 4 Models

Data Access in Models and Repositories


In MVC applications, you can place the data
access code in the model along with the business
logic. However, many software architects prefer to
separate these two types of code because they
serve different purposes:

o Business Logic. This code defines the


objects that the web application
manages, their properties and their
relationships with each other.
o Data Access Logic. This code defines the
operations necessary to persist data to a
database. This includes operations to
create new records, read records, update records, and delete records in database tables. A single
object in the business logic layer may take data from multiple database tables. This abstraction is
handled by the data access logic.
MVC does not require you to separate business and data access logic and you can create MVC model
classes that implement both layers. This is often done in small or simple web applications with small
development teams. In these scenarios, Entity Framework classes are used directly in the model classes.
In more complex situations, you need to place the business logic in MVC model classes and place the data
access logic in dedicated classes called repositories. When you take this approach, model classes are
independent of the database structure and do not include code that depends on database table names,
column names, or view names. This approach makes it easier to redesign the database or move to a
different data store or data access technology, without the need to recode your entire application. Using
this approach, you employ the Entity Framework in your repository classes, but not in your model classes.

How to Separate Model Classes and Repositories


If you do choose to separate business logic and data access logic, you must take the following steps for
each model class:

1. Define an interface for the repository class. This interface declares the methods that the repository
class uses to read and write data from and to the database.

2. Create and write code for the repository class. This class must implement all the data access methods
declared in the interface.

3. Remove all data access code from the model class.

4. Modify the controller class to use the repository class. Instead create an instance of the repository
class and call its methods to create the model.

A Simple Example Model Class and Repository Class


To illustrate how to separate model classes and repository classes, the following examples implement a
comment on a photo.

The Comment model class helps users to comment on a photo as the following lines of code illustrate.

The Comment Model Class


public class Comment
{
public int CommentID { get; set; }
public int PhotoID { get; set; }
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-19

public string CommentText { get; set; }


public virtual Photo Photo { get; set; }
}

This interface for a comment repository class defines just one method to get the comments for a given
photo as the following lines of code illustrate.

The ICommentRepository Interface


public interface ICommentRepository {
ICollection<Comment> GetComments (int PhotoID);
}

The CommentRepository class implements the GetComments method as the following lines of code
illustrate.

The CommentRepository Class


public class CommentRepository : ICommentRepository
{
public ICollection<Comment> GetComments(int PhotoID)
{
//Implement entity framework calls here.
}
}

The CommentsController class uses the repository class instead of calling Entity Framework methods as
the following lines of code illustrate.

The CommentsController Class


public class CommentsController : Controller
{
ICommentRepository commentRepository = new CommentRepository;
public ActionResult DisplayCommentsForPhoto (int PhotoID)
{
//Use the repository to get the comments
ICollection<Comments> comments = commentRepository.GetComments(PhotoID);
return View("DisplayComments", comments);
}
}

Note: By using the ICommentRepository interface, the code makes it easy to replace
CommentRespository with another implementation if you need to. However, the CommentController
code still creates a CommentRespository object. You have to modify the object to make the
replacement.

In an even better architecture you can replace CommentRepository with a different implementation of
ICommentRepository without any changes to the CommentController class. This is an extremely
flexible and adaptable approach and is called a loosely coupled architecture.

Loosely coupled architectures are also essential for unit testing.


MCT USE ONLY. STUDENT USE PROHIBITED
03-20 Developing ASP.NET MVC 4 Models

Lab: Developing ASP.NET MVC 4 Models


Scenario
You are planning to create and code an MVC model that implements your plan for photos and comments
in the Adventure Works photo sharing application. The model must store data in a Windows Azure SQL
database and include properties that describe photos, comments, and their content. The model must
enable the application to store uploaded photos, edit their properties, and delete them in response to
user requests.Objectives

After completing this lab, you will be able to:

o Create a new ASP.NET MVC 4 project in Visual Studio 2012.

o Add a new model to the ASP.NET MVC 4 web application and add properties to the model.

o Use display and edit data annotations in the MVC model to assign property attributes to views
and controllers.

o Use Visual Studio to create a new Windows Azure SQL database and connect to the database.

o Add Entity Framework code to the model classes in the MVC model.

o Use display and edit data annotations in the MVC model to assign property attributes to views
and controllers.

Estimated Time: 30 minutes

Virtual Machine: 20486A-SEA-DEV11

Username: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Exercise 1: Creating an MVC Project and Adding a Model


Scenario
In this exercise, you will:
o Create a new MVC 4 web application in Visual Studio 2012.

o Add model classes to the web application.

The main tasks for this exercise are as follows:


1. Create a new MVC project.

2. Add a new MVC model.

 Task 1: Create a new MVC project.


1. Start the virtual machine, and log on with the following credentials:
o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Start Visual Studio 2012 and create a new ASP.NET MVC 4 Web Application by using the following
information:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-21

o Name: PhotoSharingApplication

o Location: Allfiles (D):\Labfiles\Mod03

o Solution name: PhotoSharingApplication

o Create directory for solution: True

o Project Template: Empty

 Task 2: Add a new MVC model.


1. Add a new model class to the PhotoSharingApplication project by using the following information:

o Class name: Photo

2. Add another model class to the PhotoSharingApplication project by using the following information:
o Class name: Comment

Results: After completing this exercise, you will be able to create an MVC 4 web application and add
model classes to the web application.

Exercise 2: Adding Properties to MVC Models


Scenario
In this exercise, you will:

o Add properties to the Photo and the Comment model classes.

o Implement a relationship between model classes.

The main tasks for this exercise are as follows:

1. Add properties to the Photo model class.

2. Add properties to the Comment model class.

3. Implement a relationship between model classes.

 Task 1: Add properties to the Photo model class.


1. Add a primary key property to the Photo model class by using the following information:

o Property name: PhotoID

o Data type: integer

o Access: Read and write

2. Add a title property to the Photo model class by using the following information:
o Property name: Title

o Data type: string

o Access: Read and write


3. Add an image property to the Photo model class and store the MIME type of image by using the
following information:

o Property names: PhotoFile, ImageMimeType

o Data type for the image: byte


MCT USE ONLY. STUDENT USE PROHIBITED
03-22 Developing ASP.NET MVC 4 Models

o Data type for MIME type: string

o Access: Read and write

4. Add a description property to the Photo model class by using the following information:

o Property name: Description

o Data type: String

o Access: Read and write

5. Add a date property to the Photo model class by using the following information:

o Property name: CreatedDate

o Data type: DateTime

o Access: Read and write

6. Add a user name property to the Photo model class by using the following information:
o Property name: UserName

o Data type: string

o Access: Read and write

 Task 2: Add properties to the Comment model class.


1. Add a primary key to the Comment model class by using the following information:

o Property name: CommentID

o Data type: integer

o Access: Read and write

2. Add a PhotoID property to the Comment model class by using the following information:

o Property name: PhotoID

o Data type: integer

o Access: Read and write

3. Add a user name property to the Comment model class by using the following information:

o Property name: UserName

o Data type: string

o Access: Read and write

4. Add a subject property to the Comment model class by using the following information:

o Property name: Subject

o Data type: string

o Access: Read and write

5. Add a body text property to the Comment model class by using the following information:

o Property name: Body

o Data type: string

o Access: Read and write


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-23

 Task 3: Implement a relationship between model classes.


1. Add a new property to the Photo model class to retrieve comments for a given photo by using the
following information:

o Property name: Comments

o Data type: a collection of Comments

o Access: Read and write


o Include the virtual keyword

2. Add a new property to the Comment model class to retrieve photo for a given comment by using the
following information:

o Property name: Photo

o Property type: Photo

o Access: Read and write


o Include the virtual keyword

Results: After completing this exercise, you will be able to add properties to classes to describe them to
the MVC runtime. You will also be able to implement a one-to-many relationship between classes.

Exercise 3: Using Data Annotations in MVC Models


Scenario
In this exercise, you will:

o Add data annotations to the properties to help MVC web application render them in views and
validate user input.

The main tasks for this exercise are as follows:

1. Add display and edit data annotations to the model.


2. Add validation data annotations to the model.

 Task 1: Add display and edit data annotations to the model.


1. Add a display data annotation to the Photo model class to ensure that the PhotoFile property is
displayed with the name Picture.

2. Add an edit data annotation to the Photo model class that ensures the Description property editor is
a multiline textbox.

3. Add the following data annotations to the Photo model class to describe the CreatedDate property:

o Data type: DateTime

o Display name: Created Date

o Display format: {0:dd/MM/yy}

4. Add an edit data annotation to the Comment model class that ensures that the Body property editor
is a multiline textbox.
MCT USE ONLY. STUDENT USE PROHIBITED
03-24 Developing ASP.NET MVC 4 Models

 Task 2: Add validation data annotations to the model.


1. Add a validation data annotation to the Photo model class to ensure that the users complete the Title
field.

2. Add validation data annotations to the Comment model class to ensure that the users complete the
Subject field and enter a text that is shorter than 250 characters.

Results: After completing this exercise, you will be able to add property descriptions and data
annotations to the two model classes in the MVC web application.

Exercise 4: Creating a New Windows Azure SQL Database


Scenario
In this exercise, you will:

o Add Entity Framework code to the Photo Sharing application in code-first mode.

o Create a new SQL database in Windows Azure.

o Use SQL database to create a connection string to use in the application.

The main tasks for this exercise are as follows:

1. Add an Entity Framework Context to the model.


2. Add an Entity Framework Initializer.

3. Create a Windows Azure SQL database and obtain a connection string.

 Task 1: Add an Entity Framework Context to the model.


1. Use the NuGet Package Manager to add Entity Framework 5.0 to the application.
2. Add a new class called PhotoSharingContext to the Models folder and ensure that the new class
inherits the System.Data.Entity.DbContext class.

3. Add DbSet properties to Photos and Comments to enable Entity Framework to create database
tables called Photos and Comments.

 Task 2: Add an Entity Framework Initializer.


1. Add a new class called PhotoSharingInitializer to the Models folder and ensure that the new class
inherits the DropCreateDatabaseAlways
<PhotoSharingContext> class.

2. Open the getFileBytes.txt file from the following location and add all the text of the file as a new
method to the PhotoSharingInitializer class:
o File path: Allfiles (D):\Labfiles\Mod03\CodeSnippets

3. Override the Seed method in the PhotoSharingInitializer class.

4. Create a new list of Photo objects in the Seed method. The list should contain one photo object with
the following properties:

o Title: Test Photo

o Description: <A description of your choice>

o UserName: NaokiSato
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-25

o PhotoFile: getFileBytes("\\Images\\flower.jpg")

o ImageMimeType: image/jpeg

o CreatedDate: <Today’s date>

5. Add photo list to the Entity Framework context and save the photo to the database.

6. Create a new list of Comment objects in the Seed method. The list should contain one Comment
object with the following properties:

o PhotoID: 1

o UserName: NaokiSato

o Subject: Test Comment

o Body: This comment should appear in photo 1.

7. Add the comment list to the Entity Framework context and save the comment to the database.
8. Open Global.asax and add a line of code that calls Database.SetInitializer, passing the
PhotoSharingInitializer class you created. Also add the following namespaces:

o using System.Data.Entity;

o using PhotoSharingApplication.Models;

 Task 3: Create a Windows Azure SQL database and obtain a connection string.
1. Log on to the Windows Azure portal by using the following information:

o Portal address: http://www.windowsazure.com/


o User name: <your Windows Live user name>

o Password: <password>

2. Create a new database server and a new database by using the following information:
o Database name: PhotoSharingDB

o Database server: New SQL Database Server

o Login name: <your Windows Azure account ID>


o Login password: Pa$$w0rd

o Login password confirmation: Pa$$w0rd

o Region: <a region close to you>

3. In the list of allowed IP addresses for the PhotoSharingDB database, add the following IP address
ranges:

o Rule name: First Address Range

o Start IP Address: <first address in range>

o End IP Address: <last address in range>

4. Obtain the connection string for the PhotoSharingDB database and add it to the web.config file.

5. Build the Photo Sharing application.


MCT USE ONLY. STUDENT USE PROHIBITED
03-26 Developing ASP.NET MVC 4 Models

Results: After completing this exercise, you will be able to create an MVC application that uses Windows
Azure SQL Database as its data store.

Exercise 5: Testing the Model and Database


Scenario
In this exercise, you will:

o Add a controller and views to the MVC web application.

o Run the web application.

The main tasks for this exercise are as follows:

1. Add a controller and views.

2. Add an image and run the application.

 Task 1: Add a controller and views.


1. Add a new controller to the PhotoSharingApplication project by using the following information:
o Name: PhotoController

o Template: MVC Controller with read/write actions and views, using Entity Framework

o Model class: Photo


o Data context class: PhotoSharingContext

o Views: Razor(CSHTML)

 Task 2: Add an image and run the application.


1. Create a new top-level folder, and copy an image to the new folder by using the following
information:

o New folder name: Images

o Image to be copied: flower.JPG


o Location of the image: Allfiles (D):\Labfiles\Mod03\Images

2. Run the application by debugging, and access the following relative path:

o /photo/index

Results: After completing this exercise, you will be able to add controllers, views, and images to an MVC
web application and test the application by displaying data from a Windows Azure SQL database.

Question: You are building a site that collects information from customers for their
accounts. You want to ensure that customers enter a valid email address in the Email
property. How would you do this?

Question: You have been asked to create an intranet site that publishes a customer
database, created by the sales department, to all employees within your company. How
would you create the model with Entity Framework?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 03-27

Module Review and Takeaways


The heart of an MVC web application is the model. This is because the model classes describe the
information and objects that your web application manages. In this module you have seen how to create
your model, define relationships between the model classes, describe how to display, edit, and validate
properties, and how to extend MVC model handling capabilities. You have also seen how to bind model
classes to database tables by using Entity Framework and how to query object in the model by writing
LINQ code.

Best Practice: If you have a pre-existing database for a web application, use Entity Framework in the
database-first workflow to import and create your model and its classes.

Best Practice: If you want to create a new database for a web application and prefer to draw your
model in a Visual Studio designer, use Entity Framework in the model-first workflow to create your model
and its classes.

Best Practice: If you want to create a new database for a web application and prefer to write code
that describes your model, use Entity Framework in the code-first workflow to create your model and
its classes.

Best Practice: If you want to separate business logic from data access logic, create separate model
classes and repository classes.

Common Issues and Troubleshooting Tips

Issue Troubleshooting Tip

The website cannot The connection string contains many parameters and is a
connect to or create a common source of errors. Incorrect values in the string may
database. prevent the website locating the database server or
authenticating properly. For full information on building
connection strings in ADO.NET, see
http://msdn.microsoft.com/en-us/library/ms254978.aspx

Review Question(s)
Question: At the end of the first iteration of your project, you have a website that displays
photos users upload. However, during development the database is empty and users must
upload several photos to the site so they can test functionality. Your manager wants you find
some way to populate the database whenever it is deployed to the test server. How can you
do this?
MCT USE ONLY. STUDENT USE PROHIBITED
03-28 Developing ASP.NET MVC 4 Models
MCT USE ONLY. STUDENT USE PROHIBITED
04-1

Module04
Developing ASP.NET MVC 4 Controllers
Contents:
Module Overview 04-1 

Lesson 1: Writing Controllers and Actions 04-2 

Lesson 2: Writing Action Filters 04-12 

Lab: Developing ASP.NET MVC 4 Controllers 04-16 

Module Review and Takeaways 04-23 

Module Overview
MVC controllers respond to browser requests, create model objects, and pass them to views for rendering
and displaying in the web browser. If required, controllers can also perform other actions, such as saving
model class changes to the database. Controllers are central to MVC applications. You need to understand
the functioning of controllers to be able to create the right model objects, manipulate them, and pass
them to the right views.

To maximize the reuse of code in controllers, you must know how to program action filters. You can use
action filters to run code before or after every action in your web application, on every action in a
controller, or on other combinations of controller actions.

Objectives
After completing this module, you will be able to:

o Add a controller to a web application that responds to user actions specified in the project
design.
o Write code in action filters that executes before or after a controller action.
MCT USE ONLY. STUDENT USE PROHIBITED
04-2 Developing ASP.NET MVC 4 Controllers

Lesson 1
Writing Controllers and Actions
A controller is a .NET Framework class that inherits from the System.Web.Mvc.Controller base class.
Controllers respond to user requests. Within a controller class, you create actions to respond to user
requests. Actions are methods within a controller, which return an ActionResult object. The ActionResult
object is often a view that displays a response to the user request; however, it can also yield other types of
results. To process incoming user requests, manage user input and interactions, and implement relevant
application logic, you need to know how to create controllers and actions. You must also know how to
create parameters in action code blocks and pass objects to actions.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe how a controller responds to user actions in an MVC 4 web application.


o Write controller actions to respond to web browser requests, create model classes, and call views.

o Explain how to use parameters passed in a browser request to a controller action and use them to
change the action result.

o Explain how to pass information to views that have model classes.

o Create a controller and actions.

o Describe controller factories.

Responding to User Requests


When an MVC web application receives a request
from a web browser, the following events happen
in sequence:

1. An MvcHandler object creates a controller


factory. The controller factory is the object
that instantiates a controller to respond to the
request. Usually, this factory is a
DefaultControllerFactory object, but you
can create a custom controller factory, if
necessary. The MVCHandler object chooses
the controller factory based on the
RequestContext object, which has
information about the request that the user made.

2. The controller factory creates a Controller object, and the MvcHandler calls the Execute method in
that controller.

3. The ControllerActionInvoker examines the RequestContext object and determines the action in
the Controller object to call.

4. The ControllerActionInvoker uses a model binder to determine the values to be passed to the
action as parameters.

5. The ControllerActionInvoker runs the action. Often, the action creates a new instance of a model
class, perhaps by querying the database with the parameters that the invoker passed to it. This model
object is passed to a view, to display results to the user. Action methods can do many other things
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-3

such as rendering views and partial views, redirecting to other websites, displaying snippets of
content, or displaying other files.

Note: The routing engine determines which controller and action receives a request.
Routing is not covered in this section.

The User Request


Users of web browsers make requests either by typing a URL into the address bar of the browser, or by
clicking a link to some address within your website. Such links can either be within your website, in which
case you can control how they are rendered, or from an external website. Whether the request originates
from within the website or from an external website, it can include information that controller actions can
use as parameters. Consider the following examples:
o http://www.adventureworks.com/: This URL is to the home page of the website and specifies no
further information.

o http://www.adventureworks.com/photo: This URL specifies an extra value, photo. By default, the


MvcHandler interprets this as the name of a controller.

o http://www.adventureworks.com/photo/index: This URL specifies a second value, index. By


default, the MvcHandler interprets this as the name of an action within the controller.
o http://www.adventureworks.com/photo/display/1: This URL specifies a third value, 1. By default,
the ControllerActionInvoker interprets this as a parameter to pass to the action method.

o http://www.adventureworks.com/photo/display?id=1: This URL includes a query string, id=1. The


model binder examines the Display actions in the Photo controller. If it finds an action with a
parameter called id, it calls that action and passes 1 as a parameter.

Note: You can modify the above logic in several ways. For example, you can create routes
that interpret the above URLs differently. The examples are true when only the default route
exists.

Let us consider that a user requests a controller called photo, by typing the URL in the address bar of a
web browser. By default, the MVC DefaultControllerFactory, names this controller class as
PhotoController. You should keep to this convention when you create and name controllers. Otherwise,
you will receive unexpected 404 errors and controllers will not work as intended. If you create a custom
controller factory, you can define your own naming convention for controller classes.

The Microsoft Visual Studio project templates include a folder named Controllers. This is a good location
to create your controllers. Microsoft Visual Studio places controllers in the projectname.Controllers
namespace, by default.
The following example shows the code in an MVC controller class called PhotoController. The controller
has a single action called Index, which returns a list of Photo items from the Entity Framework context.

A Simple Controller Class


public class PhotoController : Controller
{
private ContextDB context = new ContextDB();
public ActionResult Index ()
{
return View("Index", context.Photos.ToList());
}
}
MCT USE ONLY. STUDENT USE PROHIBITED
04-4 Developing ASP.NET MVC 4 Controllers

Question: What is the convention that you should follow while creating controllers?

Writing Controller Actions


Controllers encapsulate user interaction logic for
an MVC web application. You specify this logic by
writing actions. An action is a method within the
controller class. The code you write within an
action method determines how the controller
responds to a request and the model class and
view that MVC uses to display a webpage in the
browser.

Note:
When you add a new controller to an MVC
application, Visual Studio presents scaffolding
options to help you create action methods and associated views. For example, if you specify a
model class and entity framework context class, Visual Studio can create scaffold index, details,
create, edit, and delete action methods in your new controller. You can use these as a starting
point for your code. As you become more experienced with action methods, you may prefer to
select the Empty MVC Controller template and write your own methods without scaffold code.

Controller actions are public methods that return an ActionResult object. Alternatively, actions can return
objects of many other classes that derive from the ActionResult class. For example, you can code a
controller with an Index action to obtain all Photo objects and pass them to the Index view.

When you want to display a specific model object, you must obtain the correct instance from the
database. The following code shows how to display the first Photo object.

A Details Action
public ActionResult First ()
{
Photo firstPhoto = context.Photos.ToList()[0];
if (firstPhoto != null)
{
return View("Details", firstPhoto);
}
else
{
return HttpNotFound();
}
}

Some user actions are in two parts. For example, to create a new Photo a user can make an HTTP GET
request to the following URL: http://www.adventureworks.com/photo/create.

The following code shows an action that responds to a GET request, to display a new photo form.

A Create Action for the GET Request


public ActionResult Create ()
{
Photo newPhoto = new Photo();
return View("Create", newPhoto)
}
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-5

The Create view displays a form where users can fill photo details, such as the title, description, and so on.
When a user clicks the Submit button, the web browser makes an HTTP POST request.

The following action method responds to a POST request. Note that the method name is the same, but
the [HttpPost] annotation is used to specify that this action responds to the HTTP POST verb.

A Create Action for the POST Request


[HttpPost]
public ActionResult Create (Photo photo)
{
if (ModelState.IsValid)
{
context.Photos.Add(photo);
context.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View("Create", photo);
}
}

Note that the ModelState.IsValid property is used to check whether the user has submitted valid data.
You can specify data validation by using validation data annotations in the model class. If the data is valid,
the model object is added and saved. Otherwise, the application displays the Create view again so that
the user can correct the invalid data.

Note: For the above code to work, the Create view must contain a form that uses the
POST method.

Possible Return Classes


Action methods are usually defined with the ActionResult class as the return type. ActionResult is a base
class, and you can use a range of derived classes to return different responses to the web browser.

Controller actions usually return a view and pass a model class to it, for display. You can create an action
that calls the View() helper, and to create and return a ViewResult object. The View() helper is available
when you derive from the base Controller class.

Alternatively, you can return an HTTP error. For example, you can create an action such that if no Photo
object is found, the code creates a 404 not found error by using the HttpNotFound() helper.

Sometimes, you may want to return a file from an action method. For example, let us consider that in a
Photo model the image file is stored as a byte array in the database. To display this byte array as an
image on a webpage, the action must return it as a .jpeg file, which can be used for the src attribute of an
<img> HTML tag. To return files, you can use the File() helper to return a FileContentResult object. You
can use this technique in the GetImage action.
Other possible action results include:

o PartialViewResult: You can use this action to generate a section of an HTML page, but not a
complete HTML page. Partial views can be reused in many views throughout a web application.

o RedirectToRouteResult: You can use this action result to redirect the web browser to another
action method or another route.

o RedirectResult: You can use this action result to redirect to a specific URL, either inside your web
application or to an external location.
MCT USE ONLY. STUDENT USE PROHIBITED
04-6 Developing ASP.NET MVC 4 Controllers

o ContentResult: You can use this action result to return text to the web browser. You can return
plain text, XML, a comma-separated table, or other text formats. This text can be rendered in the
web browser or parsed with client-side code.

Child Actions
When an action returns a complete view, MVC sends a new complete webpage to the web browser for
display. Sometimes, you may want to call an action from within a view, to return a piece of content for
display within a webpage. A child action is an action method that can return a small piece of content in
this manner. The FileContentResult is often a good example of a child action, because the image
returned usually forms part of a webpage. Partial views also support child actions.

To declare an action method as a child action, you can use the [ChildActionOnly] annotation. This
annotation ensures that the action method can be called only from within a view by using the
Html.Action() helper. Using this way, you can prevent a user from calling the child action directly by
typing the correct URL into the address bar.

Question: What are the various ActionResult return types that you can code while creating a controller?

Using Parameters
When users request webpages, they often specify
information other than the name of the web page
itself. For example, when they request a product
details page, they may specify the name or
catalog number of the product to display. Such
extra information is referred to as parameters. You
must understand how to determine in code what
parameters the user sent in their request.

The ControllerActionInvoker and the


DefaultModelBinder classes obtain parameters
from a user request and pass them to action
methods. The DefaultModelBinder can locate
parameters in a posted form, the routing values, the query string, or in the posted files. If the model
binder finds a parameter in the action method that matches the name and type of a parameter from the
request, the action method is called and the parameter is passed from the request. This arrangement
enables you to obtain and use parameters in your actions. For example, if a user requests the URL
http://www.adventureworks.com/photo/getphotobytitle/?title=myfirstphoto, you can easily obtain title
values in your action method.

This example code shows how to determine the value of the title parameter in a controller action:

Using a Query String Parameter


public ActionResult GetPhotoByTitle (string title)
{
var query = from p in context.Photos
where p.Title == title
select p
Photo requestedPhoto = (Photo)query.FirstOrDefault();
if (requestedPhoto != null)
{
return View("Details", requestedPhoto);
}
else
{
return HttpNotFound();
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-7

}
}

Note that the action method code uses the parameter title to formulate a LINQ to Entities query. In this
case the query searches for a Photo with the specified Title. Parameters in action methods are frequently
used in this manner.

Note: The example works if the DefaultModelBinder passes parameters. If you create a
custom model binder in your application, you must ensure that it passes parameters in the
correct manner. Otherwise, the action method cannot access the parameters that the user
specified in the request.

Question: How does DefaultModelBinder pass parameters?

Passing Information to Views


You can pass a model object, such as a Photo,
from the action method to the view by using the
View() helper method. This is a frequently-used
method to pass information from a controller
action to a view. This is because this method
adheres closely to the Model-View-Controller
pattern, in which each view renders the properties
found in the model class, which the view receives
from the controller. You should use this approach
wherever possible.

However, in some cases, you may want to


augment the information in the model class with
some extra values. For example, you may want to send a title, which needs to be inserted in the page
header, to the view. Furthermore, some views do not use model classes. The home page of a website, for
example, often does not have a specific model class. To help in these situations, you can use two other
methods to provide extra data: ViewBag and ViewData.

Using The ViewBag


The ViewBag is a dynamic object that is part of the base controller class. Because it is a dynamic object,
you can add properties that are of any type to it, in the action method. In the view, you can use the
ViewBag object to obtain the values added in the action.

You can add properties to the ViewBag object in the action method as illustrated in the following lines of
code.

Adding Properties to the ViewBag object


ViewBag.Message = "This text is not in the model object";
ViewBag.ServerTime = DateTime.Now;

To obtain and use the same properties in a view, you can use the following Razor code:

Using ViewBag Properties


<p>
The message of the day is: @ViewBag.Message
</p>
MCT USE ONLY. STUDENT USE PROHIBITED
04-8 Developing ASP.NET MVC 4 Controllers

<p>
The time on the server is: @ViewBag.ServerTime.ToString()
</p>

Using The ViewData Dictionary


The ViewBag object was added to MVC in version 3. In the earlier versions, you could pass extra data to
views by using the ViewData dictionary. This feature is still available in MVC 4 for backward compatibility
and for developers who prefer to use dictionary objects. In fact, ViewBag is a dynamic wrapper above the
ViewData dictionary. This means that, for example, you could save a value in a controller action using
ViewBag and read the same value back out using ViewData.
In action methods, you can add data to the ViewData dictionary by using key/value pairs as the following
lines of code illustrate.

Adding Data to the ViewData


ViewData["Message"] = " This text is not in the model object"
ViewData["ServerTime"] = DateTime.Now;

To obtain and use the same values in a view, you can use the following Razor code:

Using ViewData values


<p>
The message of the day is: @ViewData["Message"]
</p>
<p>
The time on the server is: @((DateTime)ViewData["ServerTime"])
</p>

In the examples, note that you can cast any ViewData values other than strings.
Question: Do ViewBag and ViewData serve different purposes?

Demonstration: How to Create a Controller


In this demonstration, you will see how to create a controller and write common actions in the controller.

Note: At the end of this demonstration, the application will not include views. Therefore,
the application cannot display web pages.

Demonstration Steps
1. Add a new controller to the web application by using the following information:
o Folder name: Controllers

o Controller name: OperaController

o Template: Empty MVC Controller

2. Add using statements to the controller for the following namespaces:

o System.Data.Entity

o OperasWebSite.Models

3. In the OperaController class, add a new variable by using the following information:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-9

o Scope: private

o Type: OperasDB

o Name: contextDB

Instantiate the contextDB object by calling the OperaDB constructor.

4. Remove all the code within the Index action. Replace it with a line of code that returns the Index
view. Pass a List of all Opera objects in contextDB to the view.

5. Add a new action to the OperaController class by using the following information:

o Scope: public

o Return Type: ActionResult

o Action name: Details

o Parameters: An Integer called id


6. Add code to the Details action method to find the Opera with the right ID. If the Opera is found,
pass it to the Details view. Otherwise return HttpNotFound.

7. Add a new action to the OperaController class by using the following information:
o Scope: public

o Return Type: ActionResult

o Action name: Create

8. In the new Create action, create a new Opera and pass it to a view by using the following
information:

o Object name: newOpera


o View name: Create

9. Add a new action to the OperaController class by using the following information:

o HTTP verb: POST

o Scope: public

o Return type: ActionResult

o Action name: Create

o Parameter: An Opera object called newOpera.

10. In the Create action code block, if ModelState.IsValid is true, add the new opera, save changes, and
redirect to the Index action. Otherwise, pass the newPhoto back to the Create view.

11. Save the file and close Microsoft Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
04-10 Developing ASP.NET MVC 4 Controllers

What are Controller Factories?


A controller factory is an MVC component that
instantiates the controller classes that you create.
For example, when a user requests a list of Photo
model objects, a controller factory should create
an instance of the PhotoController class. An
action invoker then calls one of the action
methods in that class, and a model binder passes
parameters to it.

The MVC framework includes a built-in


DefaultControllerFactory class that is suitable for
most web applications. However, you must
understand how DefaultControllerFactory
determines the controller class that it needs to create. Occasionally, you may need to create a custom
controller factory to implement your own controller creation logic.

How the Default Controller Factory Locates a Controller Class


The default controller factory identifies controller classes by using the following criteria:

o The class scope must be public.

o The class must not be marked as abstract.

o The class must not take generic parameters.

o The class must have a name that ends with Controller.

o The class must implement the IController interface.

When the MVC web application starts, the default controller factory creates a list of all the classes in the
application that satisfy these criteria. This list helps to create the correct controller rapidly. To write a
controller, you must ensure that all the above mentioned criteria are implemented. Usually, you meet the
IController interface criterion by inheriting from the base Controller class.

By default, the DeafultControllerFactory mandates all controller classes to end with the word Controller.
For example, following this convention, for the Photo model class, you would create a controller called
PhotoController.

Creating a Custom Controller Factory


Occasionally, you might want to implement a custom controller factory. There are two common reasons
for doing this:

o To modify the criteria for selecting controllers. The criteria described earlier are suitable for most
web applications but sometimes you may want to change them. For example, you may not want
to name controllers with Controller at the end, or you may want to add extra criteria of your
own.

o To support direct injection for testing. Direct injection is a programming technique which lets you
specify classes at run time, instead of specifying classes when writing code. This is helpful for unit
testing because you can inject a test class with mock data, instead of real data. The
DefaultControllerFactory class does not support direct injection.

The following code shows how to create a custom controller factory by implementing the
IControllerFactory interface:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-11

A Custom Controller Factory


public class AdWorksControllerFactory : IControllerFactory
{
public IController CreateController (RequestContext requestContext, string
ControllerName)
{
Type targetType = null;
if (ControllerName == "Photo")
{
targetType = typeof(PhotoController);
}
else
{
targetType = typeof(GeneralPurposeController);
}
return targetType == null ? null :
(IController)Activator.CreateInstance(targetType);
}
public SessionStateBehavior GetControllerSessionBehavior(RequestContext
requestContext,
string controllerName)
{
return SessionStateBehavior.Default;
}
public void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}

You must implement the CreateController, GetControllerSessionBehavior, and ReleaseController


methods for any custom controller factory you create.

For example, if the controller name passed to a controller factory is “Photo”, then the PhotoController is
used. Otherwise, the GeneralPurposeController is used. The logic in a real custom controller should be
more sophisticated than in this example. However the example illustrates the minimal required code to
create a custom controller factory.

Registering a Custom Controller Factory


Even if you create a custom controller factory in your application, MVC will still use the
DefaultControllerFactory class unless you register your custom factory.

You register a custom controller factory by using the ControllerBuilder class in the Global.asax file, as
the following lines of code show.

Registering a Custom Controller Factory


protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new AdWorksControllerFactory());
}

Question: Can you create a controller that does not end with “Controller”?
MCT USE ONLY. STUDENT USE PROHIBITED
04-12 Developing ASP.NET MVC 4 Controllers

Lesson 2
Writing Action Filters
In some situations, you may need to run code before or after controller actions run. Before a user runs any
action that modifies data, you might want to run the code that checks the details of their user account. If
you add such code to the actions themselves, you will have to duplicate the code in all the actions where
you want the code to run. Action filters provide a convenient way to avoid code duplication. You need to
know how to create and use action filters in your web application and when to use them.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe action filters.

o Create action filters.

o Determine when to use action filters.

What are Filters?


The MVC programming model enforces the
separation of concerns. For example, the business
logic in model classes is separate from the input
logic in controllers and the user interface logic in
views. Each model class is also clearly distinct from
other model classes. However, there are scenarios
where requirements may be relevant to many
parts of your application and cut across logical
boundaries. For example, authorization must be
done for many sensitive actions and controllers
regardless of the model and views that they
return. These sorts of requirements are known as
cross-cutting concerns. Some common examples of cross-cutting concerns include authorization, logging,
and caching.

Filters

Filters are MVC classes that you can use to manage cross-cutting concerns in your web application. You
can apply a filter to a controller action by annotating the action method with the appropriate attribute.
For example, an action annotated with the [Authorize] attribute, can be run only by authenticated users.
You can also apply a filter to every action in a controller by annotating the controller class with the
attribute.

Filter Types
There are four types of filters that you can use in MVC. These filters run at slightly different stages in the
request process:

Filter Type Interface Default Class Description

Authorization IAuthorizationFilter AuthorizeAttribute Runs before any other filter


and before the code in the
action method. Used to
check a user’s access rights
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-13

Filter Type Interface Default Class Description


for the action.

Action IActionFilter ActionFilterAttribute Runs before and after the


code in the action method.

Result IResultFilter ActionFilterAttribute Runs before and after a


result is returned from an
action method.

Exception IExceptionFilter HandleErrorAttribute Runs only if the action


method or another filter
throws an exception. Used
to handle errors.

Question: Which filter type will you use for the following actions?

1. Intercepting an error
2. Modifying a result

3. Authorizing users

4. Inspecting a returned value

Creating and Using Action Filters


If you have a cross-cutting concern in your web
application, you can implement it by creating a
custom action filter or a custom result filter. You
can create custom filters by implementing the
IActionFilter interface or the IResultFilter
interface. However, the ActionFilterAttribute
base class implements both the IActionFilter and
IResultFilter interfaces for you. By deriving your
filter from the ActionFilterAttribute class, you
can create a single filter that can run code both
before and after the action runs and both before
and after the result is returned.

The following code shows how an action filter writes text to the Visual Studio Output window in the order
in which the IActionFilter and IResultFilter events run. Place this code in a class file within your web
application:

A Simple Custom Action Filter


public class SimpleActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine("This Event Fired: OnActionExecuting");
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine("This Event Fired: OnActionExecuted");
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
MCT USE ONLY. STUDENT USE PROHIBITED
04-14 Developing ASP.NET MVC 4 Controllers

Debug.WriteLine("This Event Fired: OnResultExecuting");


}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
Debug.WriteLine("This Event Fired: OnResultExecuted");
}
}

Note: You can also create a custom authorization filter by implementing the
IAuthorizationFilter interface. However, the default AuthorizeAttribute implementation is
highly useful and satisfies almost all authentication requirements as well. You should be careful
when overriding the default security code in MVC or any other programming model. If you do so
without a full understanding of the implications, you can introduce security vulnerabilities that a
malicious user can exploit.

Using a Custom Action Filter


After you have created a custom action filter, you can apply it to any action method or class in your web
application by annotating the method or class with the action filter name.

In the following lines of code, the SimpleActionFilter is applied to the Index action of the Photo
controller:

Using A Custom Action Filter


public class PhotoController : Controller
{
ContextDB contextDB = new ContextDB();
[SimpleActionFilter]
public ActionResult Index()
{
return View("Index", contextDB.Photos.ToList());
}
}

Question: What are the advantages of custom action filters?

Discussion: Action Filter Scenarios


Consider the following scenarios. In each case,
discuss with the rest of the class whether the
scenario requires a custom filter, or can be solved
with a built-in filter type, or cannot be solved with
filters.

1. You are creating a photo sharing application,


and you want to enable each user to discuss
photos and cycling trips with other users,
whom they have marked as their friends.
Other users should be prevented from seeing
these discussions.

2. You want to ensure that when MVC calls the


GetImage action method, the ID in the query string is passed as a parameter.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-15

3. You are writing a photo sharing application and you want to prevent unauthenticated users from
adding comments to a photo.

4. You want to prevent malicious users from intercepting the credentials entered by users in the logon
form for your web application. You want to ensure that the credentials are encrypted.
MCT USE ONLY. STUDENT USE PROHIBITED
04-16 Developing ASP.NET MVC 4 Controllers

Lab: Developing ASP.NET MVC 4 Controllers


Scenario
You have been asked to add a controller to the photo sharing application that corresponds to the Photo
model class that you have created in an earlier module. The controller should include actions that respond
when users upload photos, list all photos, display a single photo, and delete photos from the application.
You should also add an action that returns the photo as a .jpg file to show in a web page.

The members of your development team are new to ASP.NET MVC and they find the use of controller
actions confusing. Therefore, you need to help them by adding a component that displays action
parameters in the Visual Studio Output window whenever an action runs. You will add an action filter to
achieve this.

Note: The controllers and views that you have added in Lab 2 were to test your new model classes.
They have been removed from the project to create the actual controllers. You will create temporary views
to test these controllers at the end of this lab.

Objectives
After completing this lab, you will be able to:

o Add an MVC controller to a web application.

o Write actions in an MVC controller that respond to user operations such as create, index, display,
and delete.

o Write action filters that run code for multiple actions.

Lab Setup
Estimated Time: 30 minutes

Virtual Machine: 20486A-SEA-DEV11

User name: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:

o Enable the Allow NuGet to download missing packages during build option in Package Manager.

Exercise 1: Adding an MVC Controller and Writing the Actions


Scenario
In this exercise, you will create the MVC controller that handles photo operations. You will also add the
following actions:

o Index. This action gets a list of all the Photo objects and passes the list to the Index view for
display.

o Display. This action takes an ID to find a single Photo object. It passes the Photo to the Display
view for display.

o Create (GET). This action creates a new Photo object and passes it to the Create view, which
displays a form that the visitor can use to upload a photo and describe it.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-17

o Create (POST). This action receives a Photo object from the Create view and saves the details to
the database.

o Delete (GET). This action displays a Photo object and requests confirmation from the user to
delete the Photo object.

o DeleteConfirmed (POST). This action deletes a Photo object after confirmation.

o GetImage: This action returns the photo image from the database as a JPEG file. This method is
called by multiple views to display the image.

The main tasks for this exercise are as follows:


1. Create a photo controller.

2. Create the Index action.

3. Create the Display action.

4. Write the Create actions for GET and POST HTTP verbs.

5. Create the Delete actions for GET and POST HTTP verbs.

6. Create the GetImage action.

 Task 1: Create a photo controller.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Open the PhotoSharingApplication.sln file from the following location:

3. File path: Allfiles (D):\Labfiles\Mod04\Starter\PhotoSharingApplication

4. Create a new controller for handling photo objects by using the following information:

o Controller name: PhotoController

o Template: Empty MVC Controller

5. Add using statements to the controller for the following namespaces:

o System.Collections.Generic

o System.Globalization

o PhotoSharingApplication.Models

6. In the PhotoController class, create a new private object by using the following information:

o Scope: private

o Class: PhotoSharingContext

o Name: context

Instantiate the new object by calling the PhotoSharingContext constructor.

 Task 2: Create the Index action.


1. Edit the code in the Index action by using the following information:

o Return class: View

o View name: Index


MCT USE ONLY. STUDENT USE PROHIBITED
04-18 Developing ASP.NET MVC 4 Controllers

o Model: context.Photos.ToList()

 Task 3: Create the Display action.


1. Add a method for the Display action by using the following information:

o Scope: public

o Return Type: ActionResult

o Name: Display

o Parameters: One integer called id

2. Within the Display action code block, add code to find a single photo object from its ID.

3. If no photo with the right ID is found, return the HttpNotFound value.

4. If a photo with the right ID is found, pass it to a view called Display.

 Task 4: Write the Create actions for GET and POST HTTP verbs.
1. Add a method for the Create action by using the following information:

o Scope: public
o Return Type: ActionResult

o Name: Create

2. Add code to the Create action that creates a new Photo and sets its CreatedDate property to today’s
date.

3. Pass the new Photo to a view called Create.

4. Add another method for the Create action by using the following information:

o HTTP Verb: HTTP Post

o Scope: public

o Return Type: ActionResult


o Name: Create

o First Parameter: a Photo object called photo.

o Second Parameter: an HttpPostedFileBase object called image.

5. Add code to the Create action that sets the photo.CreatedDate property to today’s date.

6. If the ModelState is not valid, pass the photo object to the Create view. Else, if the image parameter
is not null, save the file that the user posted to the photo.PhotoFile property.

7. Add the photo object to the context, save the changes, and redirect to the Index view.

 Task 5: Create the Delete actions for GET and POST HTTP verbs.
1. Add a method for the Delete action by using the following information:

o Scope: public
o Return Type: ActionResult

o Name: Delete

o Parameter: an integer called id

2. In the Delete action, add code to find a single photo object from its ID.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-19

3. If no Photo with the right ID is found, return the HttpNotFound value

4. If a Photo with the right ID is found, pass it to a view called Delete.

5. Add a method called DeleteConfirmed by using the following information:

o HTTP Verb: HTTP Post

o ActionName: Delete

o Scope: public

o Return Type: ActionResult

o Name: DeleteConfirmed
o Parameter: an integer called int

6. Find the correct photo object from the context by using the id parameter.

7. Remove the photo object from the context, save your changes and redirect to the Index action.

 Task 6: Create the GetImage action.


1. Add a method for the GetImage action by using the following information:

o Scope: public

o Return Type: FileContentResult

o Name: GetImage

o Parameter: an integer called id

2. Find the correct photo object from the context by using the id parameter.

3. If the photo object is not null, return a File result constructed from the photo.PhotoFile and
photo.ImageMimeType properties, else return the null value.

4. Save the file.

Results: After completing this exercise, you will be able to create an MVC controller that implements
common actions for the Photo model class in the Photo Sharing application.

Exercise 2: Writing the Action Filters in a Controller


Scenario
Your development team is new to MVC and is having difficulty in passing the right parameters to
controllers and actions. You need to implement a component that displays the controller names, action
names, and parameter names, and values in the Visual Studio Output window to help with this problem.
In this exercise, you will create an action filter for this purpose.

The main tasks for this exercise are as follows:

1. Add an action filter class.

2. Add a logValues method to the action filter class.

3. Add a handler for the OnActionExecuting event.

4. Register the Action Filter with the Photo Controller.


MCT USE ONLY. STUDENT USE PROHIBITED
04-20 Developing ASP.NET MVC 4 Controllers

 Task 1: Add an action filter class.


1. Create a new class for the action filter by using the following information:

o Name: ValueReporter

o Folder: Controllers

2. Add using statements to the controller for the following namespaces:

o System.Diagnostics

o System.Web.Mvc

o System.Web.Routing

3. Ensure that the ValueReporter class inherits from the ActionFilterAttribute class.

 Task 2: Add a logValues method to the action filter class.


1. Add a method to the ValueReporter class by using the following information:

o Scope: private

o Return type: void


o Name: logValues

o Parameter: a RouteData object called routeData.

2. Within the logValues method, call the Debug.WriteLine method to send the name of the controller
and action to the Visual Studio Output window. For the category, use the string “Action Values”.

3. Within the logValues method, create a foreach loop that loops through the var items in
routeData.Values.

4. In the foreach loop, call the Debug.WriteLine method to send the key name and value to Visual
Studio Output window.

 Task 3: Add a handler for the OnActionExecuting event.


1. In the ValueReporter action filter, override the OnActionExecuting event handler.
2. In the OnActionExecuting event handler, call the LogValues method, and pass the
filterContext.RouteData object.

3. Save the file.

 Task 4: Register the Action Filter with the Photo Controller.


1. Open the PhotoController class and add the ValueReporter action filter to the PhotoController
class.

2. Save the file.

Results: After completing this exercise, you will be able to create an action filter class that logs the details
of actions, controllers, and parameters to the Visual Studio Output window whenever an action is called.

Exercise 3: Using the Photo Controller


Scenario
In this exercise, you will:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-21

o Create a temporary index and display views by using the scaffold code that is built into the Visual
Studio MVC application template.

o Use the views to test controllers, actions, and action filters, and run the Photo Sharing application.

The main tasks for this exercise are as follows:

1. Create the Index and Display views.


2. Use the GetImage action in the Display view.

3. Run the application and display a photo.

 Task 1: Create the Index and Display views.


1. Add a new view to the Index action method of the PhotoController class by using the following
information:

o Name: Index

o View type: Strong


o Model class: Photo

o Scaffold template: List

2. Add a new view to the Display action method of the PhotoController class by using the following
information:

o Name: Display

o View type: Strong

o Model class: Photo

o Scaffold template: Details

 Task 2: Use the GetImage action in the Display view.


1. In the Display.cshtml code window, after the code that displays the model.Title property, add a code
that runs if the Model.PhotoFile property is not null.

2. Within the if code block, render an <img> tag. Use the following information:

o Tag: <img>

o Width: 800px

o Source: Blank

3. In the src attribute of the <img> tag, add a call to the Url.Action helper by using the following
information:

o Controller: Photo

o Action: GetImage

o Parameters: Model.PhotoID

4. Save the file.

5. Build the solution.

 Task 3: Run the application and display a photo.


1. Start debugging the application and access the following relative path:

o Path: /photo/index
MCT USE ONLY. STUDENT USE PROHIBITED
04-22 Developing ASP.NET MVC 4 Controllers

2. In the Output pane of the PhotoSharingApplication - Microsoft Visual Studio window, locate the
last entry in the Action Values category to verify whether there are any calls to the Display and the
GetImage actions.

3. Display an image.

4. In the Output pane of the PhotoSharingApplication - Microsoft Visual Studio window, locate the
last entry in the Action Values category to verify whether there are any calls to the Display and the
GetImage actions.

5. Stop debugging and close Microsoft Visual Studio.

Results: After completing this exercise, you will be able to create an MVC application with views that you
can use to test controllers, actions, and action filters.

Question: What will happen if you click the Edit or Delete links in the Index view in the Lab?
Question: Why did you use the ActionName annotation for the DeleteConfirmed action in
the PhotoController class?

Question: In the lab, you added two actions with the name Create. Why is it possible to add
these actions without using the ActionName annotation?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 04-23

Module Review and Takeaways


In this module, you have seen the pivotal role that controllers and actions take in the construction of an
MVC web application. Controllers and actions ensure that the application responds correctly to each user
request, create instances of model classes, and pass the model class to a view for display. In the next
module, you will see how to create and code views to implement the user interface for your application.

Best Practice: Unless you have a good reason not to, keep to the convention that each controller
should be named to match the corresponding model class, with “Controller” appended. For example, for
the Photo model class, the controller should be called PhotoController. By maintaining this convention,
you create a logically named set of model and controller classes and can use the default controller factory.

Best Practice: Take great care if you choose to override the built-in AuthorizeAttribute filter because
it implements permissions and security for the web application. If you carelessly modify the security
infrastructure of the MVC framework, you may introduce vulnerabilities in your application. Instead, use
the built-in filter wherever possible.

Review Question(s)
Question: You want to ensure that the CreatedDate property of a new Photo object is set
to today’s date when the object is created. Should this value be set in the Photo model class
constructor method or in the PhotoController Create action method?
MCT USE ONLY. STUDENT USE PROHIBITED
04-24 Developing ASP.NET MVC 4 Controllers
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-1

Module05
Developing ASP.NET MVC 4 Views
Contents:
Module Overview 05-1

Lesson 1: Creating Views with Razor Syntax 05-2

Lesson 2: Using HTML Helpers 05-13

Lesson 3: Reusing Code in Views 05-21

Lab: Developing ASP.NET MVC 4 Views 05-25

Module Review and Takeaways 05-33

Module Overview
Views are one of the three major components of the MVC programming model. You can construct the
user interface for your web application by writing views. A view is a mixture of HTML markup and code
that runs on the web server. You need to know how to write the HTML and code found in a view and use
the various helper classes built into MVC. You also need to know how to write partial views, which render
sections of HTML that can be reused in many places in your web application. The knowledge of creating
views will help you develop webpages that present dynamic content.

Objectives
After completing this module, you will be able to:
o Create an MVC view and add Razor markup to it to display data to users.

o Use HTML helpers in a view to render controls, links, and other HTML elements.

o Reuse Razor markup in multiple locations throughout an application.


MCT USE ONLY. STUDENT USE PROHIBITED
05-2 Developing ASP.NET MVC 4 Views

Lesson 1
Creating Views with Razor Syntax
When a user makes a request to an MVC web application, a controller responds. Often, the controller
action instantiates a model object, for example, a Product. The controller might get the Product by
querying the database, by creating a new Product, or by taking some other step. To display the Product,
the controller creates a view and passes the Product to it. The view builds a webpage by inserting
properties from the Product, and sometimes, from other sources, into HTML markup. MVC sends the
completed webpage to the browser. To create views, you must understand how MVC interprets the code
you place in views and how a completed HTML page is built. By default, the Razor view engine performs
this interpretation, but you can also use other view engines. If you take time to understand Razor, you will
find it to be an excellent and versatile view engine that gives a great degree of control over the rendered
HTML. Other view engines have very different syntaxes, which you may prefer if you have worked with
earlier versions of ASP.NET or other server-side web technologies, such as Ruby on Rails.

Lesson Objectives
After completing this lesson, you will be able to:
o Add views to an MVC 4 web application.

o Use the @ symbol in a Razor view to treat code as content.

o Describe the features of Razor syntax.


o Bind views to model classes to display data.

o Explain how to render HTML that is accessible to visitors with low vision.

o List a few alternative view engines to the Razor view engine.

Adding Views
In an MVC application, there is usually one
controller for every model class. For example, a
model class named Product usually has a
controller called ProductController that contains
all the actions relevant to products. There may be
some controllers that do not correspond to any
model classes, such as the HomeController.
However, each controller can have multiple views.
For example, you may want to create the
following views for Product objects:

o Details. The details view can display a


Product, its price, catalog number, and
other details.

o Create. The create view can enable users to add a new Product in the catalog.

o Edit. The edit view can enable users to modify the properties of an existing Product.

o Delete. The delete view can enable users to remove a Product from the catalog.
o Index. The index view can display all the Product objects in the catalog.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-3

By convention, an MVC web application creates all views within the top-level Views folder. Within this
folder, there is a folder for each controller in the application. In the preceding example, the Views folder
would contain a Product folder. This folder would contain the Details, Create, Edit, Delete, and Index
views.

If you use the Razor view engine and the C# language, views are files with a .cshtml extension. If you use
the Razor view engine and the Visual Basic language, views are files with a .vbhtml extension. Other view
engines use different extensions.

How to Create a View File


You can add a view file in Visual Studio by performing the following steps:

1. In the Solution Explorer pane, select the folder where you want to create the view. For example, for
a controller named ProductController, MVC expects view files to be located in the Views/Product
folder.

2. Right-click the selected folder, point to Add, and then select View.

Alternatively, you can create a view file for a particular controller action by opening the controller code
file, right-clicking the action, and then clicking Add View. If you create a view by using the controller
code file, some properties in the Add View dialog box are filled in by default.

The following table describes the properties that you must complete in the Add View dialog box.

Property Description

View name This is the name of the view. The view file name is this name with the
appropriate extension added. The name you choose should match the
name returned by the corresponding controller action. If the action
controller does not specify the name of the view to use, MVC assumes
the name of the view matches the name of the controller action.

View engine By default, you can select the Razor or ASPX view engines in this list.
Other engines may be available if you have installed them.

Create a strongly-typed view When you select this check box, you bind the view to a specific model
class, such as a Product class. When you use strongly-typed views, you
get additional IntelliSense and error checking help in Visual Studio as
you write code in the view.

Model class If you create a strongly-typed view, you need to specify the model class
to bind to the view. Visual Studio will use this class when it formulates
IntelliSense prompts and checks for compile time errors.

Scaffold template A scaffold template is a basic view that Visual Studio can use to create
the view. If you specify a model class for the view, Visual Studio can
create simple templates for Create, Edit, Details, Delete, and List views.
When you become more experienced in using views, you may prefer to
build a view from the empty scaffold template, rather than use
populated scaffold templates.

Reference script libraries When you select this check box, links to common client-side script files
are included in the view. These links include the jQuery JavaScript
library.

Create as a partial view A partial view is a section of Razor code that you can reuse in multiple
views in your application.

Use a layout or master page A layout or master page can be used to impose a standard layout and
MCT USE ONLY. STUDENT USE PROHIBITED
05-4 Developing ASP.NET MVC 4 Views

Property Description
branding on many pages within the web application.

Note: You will see other view engines later in this module.

Question: You are using the Razor view engine and Visual Basic to create views. You right-click the
Delete action in the CustomerController.vb file. What is the name of the view file that Visual Studio will
create by default?

Differentiating Server Side Code from HTML


The Razor view engine interprets view files and
runs any server-side code contained in the view
files. To do this, Razor must distinguish server-side
code from HTML content that should be sent to
the browser unchanged.
The Razor view engine looks for the @ symbol to
identify server-side code.

In the following code example, Razor runs the line


with the @ symbol as C# code on the web server.
This code example displays the value of the Title
property of the model class.

Using @ to Declare Server-Side Code


<p>Razor will not interpret any character in this line as code, because there is no “at”
symbol</p>
<p>
The photo title is:
@Model.Title
</p>

A section of code marked with the @ symbol is referred to as a Razor code expression. In Razor syntax,
you mark the start of a Razor code expression with the @ symbol, but unlike the ASPX view engine, Razor
lacks an expression ending symbol. Instead, Razor infers the end of a code expression by using a fairly
sophisticated parsing engine.

Modifying the Interpretation of Code and Content


Sometimes, you may need to modify the logic that Razor uses to interpret code expressions. For example,
if you want to display an @ symbol in the browser, you use @@. Razor interprets this as an escape
sequence and renders a single @ symbol. This technique is useful for rendering email addresses.

Razor has sophisticated logic to distinguish code from content and often the @ symbol is all that is
required. However, occasionally you may find that an error occurs because Razor misinterprets content as
code. To fix such errors you can use the @: delimiter, which explicitly declares a line as content and not
code.

If you want to declare several lines as content, use the <text> tag instead of the @: delimiter. Razor
removes the <text> and </text> tags before returning the content to the browser.

In the following code example, Razor interprets the @: delimiter to display plain text.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-5

Using the @: delimiter


@(
@: This text is in a code block but the delimiter declares it as content.
)

HTML Encoding
For security reasons, when Razor runs any server-side code that returns a string, it encodes the string as
HTML before rendering it to the browser. For example, you may have a Comment model class with the
following properties:

o Subject. This property contains a subject for the comment as a simple string with no formatting
information.

o HtmlComment. This property contains the comment text itself. More important, it includes the
formatting information as HTML tags. For example, it might include <i> tags to italicize a word.

If you have used the normal @Model.HtmlComment syntax to return the formatted comment text,
Razor encodes “<” as “&lt” and “>” as “&gt” in the HTML source. This results in the user seeing “<i>” in
the webpage. This is not the behavior you intend because you want the users to see italicized text.

To render text without HTML encoding, use the Html.Raw() helper. For example, use
@Html.Raw(Model.HtmlComment).

Note: Razor encodes text as HTML for a specific security reason—to stop malicious users
from injecting malicious code into your web application. If such attacks are successful, injected
code may circumvent security restrictions in your web application and deny service to users, or it
may access sensitive information. Therefore, you should only use the Html.Raw() helper when it
is absolutely necessary. Also, if you must use the Html.Raw() helper, you must ensure that it does
not operate on unchecked strings entered by users.

Features of Razor Syntax


Razor includes many useful features that you can
use to control the manner in which ASP.NET MVC
renders your view as HTML. These features include
the following:

Razor Comments
You may want to include comments in your Razor
code to describe it to other developers in your
team. This is an important technique that
improves developer productivity by making it
easier to understand code.

You can declare a Razor comment by using the


@* delimiter, as shown in the following code example.

A Razor Comment
@* This text will not be rendered by the Razor view engine, because this is a comment. *@
MCT USE ONLY. STUDENT USE PROHIBITED
05-6 Developing ASP.NET MVC 4 Views

Implicit Code Expressions and Parentheses


Razor uses implicit code expressions to determine parts of a line that are server-side code. Usually implicit
code expressions render the HTML you want but occasionally you might find that Razor interprets an
expression as HTML when it should be run as server-side code.

For example, if the Product.Price property contains the value 2.00, the following code renders “Price
Including Sales Tax: 2.00 * 1.2”.

An Implicit Code Expression


<span>Price Including Sales Tax: @Model.Price * 1.2</span>

You can control and alter this behavior by using parentheses to enclose the expression for Razor to
evaluate.

For example, if the Product.Price property contains the value 2.00, the following code renders “Price
Including Sales Tax: 2.40”

Using Parentheses to Explicitly Delimit Expressions


<span>Price Including Sales Tax: @(Model.Price * 1.2)</span>

Razor Code Blocks, Loops, and Conditions


If you want to write multiple lines of server-side code, you can do it without prefacing every line with the
@ symbol. by using a Razor code block.

A Razor Code Block


@ {
//Razor interprets all text within these curly braces as server-side code.
}

Razor also includes code blocks that run conditional statements or loop through collections.

For example, Razor runs the following code only if the Product.InStock Boolean property returns true:

A Razor If Code Block


@if (Model.InStock)
{
Html.ActionLink("Buy This Product", "AddToCart")
}

Razor loops are useful for creating index views, which display many objects of a particular model class. A
product catalog page, which displays many products from the database, is a good example of an index
view. To implement an index view, a controller action passes an enumerable collection of objects to the
view.

You can loop through all the objects in an enumerable collection by using the foreach code block:

A Razor Foreach Code Block


@foreach (var item in Model)
{
<div>@item.ProductName</div>
}

Question: You want to describe a code block to developers in your view file. You do not want your
description to be passed to the browser. What syntax should you use?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-7

Binding Views to Model Classes and Displaying Properties


Many Razor views are designed to display
properties from a specific model class. If you bind
such views to the class they display, you get extra
help such as IntelliSense feedback as you write the
Razor code. Other views may display properties
from different model classes in different cases, or
may not use a model class at all. These views
cannot be bound to a model class and are called
dynamic views. You must understand how to write
Razor code for both these situations

Strongly-Typed Views

In many cases, when you create a view, you know


that the controller action will always pass an object of a specific model class. For example, if you are
writing the Display view for the Display action in the Product controller, you know from the action code
that the action will always pass a Product object to the view.
In such cases, you can create a strongly-typed view. A strongly-typed view is a view that includes a
declaration of the model class. When you declare the model class in the view, Visual Studio helps you with
additional IntelliSense feedback and error-checking as you write the code. This is possible because Visual
Studio can check the properties that the model class includes. Troubleshooting run-time errors is also
easy. You should create strongly-typed views whenever you can so that you benefit from this extra
IntelliSense and error-checking. If you follow this as a best practice, fewer you will make fewer coding
errors. A strongly-typed view only works with an object of the model class in the declaration.

To create a strongly-typed view in Visual Studio, select Create a strongly typed view in the Add View
dialog box, and then choose the model class to bind to the view.

You can choose the Product model class so that Visual Studio can bind the view to the class you specify
by adding the following line of code at the top of the view file.

The Model Declaration


@model MyWebSite.Models.Product

Later in the view file, you can access properties of the model object by using the Model helper. For
example, you might access a product catalog number in this manner: @Model.CatalogNumber.

Binding to an Enumerable List


Some views display many instances of a model class. For example, a product catalog page displays many
instances of the Product model class. In such cases, the controller action passes a list of model objects to
the view, instead of a single model object. You can still create a strongly-typed view in this situation, but
you must use a different model declaration. You usually loop through the items in the list by using a
Razor foreach loop.

The following code example shows a multiple-item view with the model declaration and the loop that
enumerates all the Product objects passed by the action.

A View of a List of Objects


@model IEnumerable<MyWebSite.Models.Product>
<h1>Product Catalog</h1>
@foreach (var Product in Model)
{
<div>Name: @Product.Name</div>
MCT USE ONLY. STUDENT USE PROHIBITED
05-8 Developing ASP.NET MVC 4 Views

Using Dynamic Views


Sometimes, you might want to create a view that can display more than one model class. For example,
you might want to create a view that can display your own products and products from a different
supplier together. These model classes might be named Product and ThirdPartyProduct. Some
properties can be similar while others differ. For example, both Product and ThirdPartyProduct might
include the Price property while only the ThirdPartyProduct model class includes the Supplier property.

Furthermore, sometimes, the controller action does not pass any model class to the view. The most
common example of such a classless view is the site home page.
In such cases, you can create a dynamic view. A dynamic view does not include the @model declaration
at the top of the page. You can choose to add the @model declaration to change the view into a
strongly-typed view, later.

When you create dynamic views, you receive less helpful IntelliSense feedback and error checking because
Visual Studio cannot check the model class properties to verify the code. In such scenarios, you have to
ensure that you access only the properties that exist. To access a property that may or may not exist, such
as the ThirdPartyProduct.Supplier property in the preceding example, check the property for null
before you use it.
Question: You want to write a view that displays ten objects of the Photo model class. What model
declaration should you use?

Rendering Accessible HTML


The Internet is for everyone, regardless of any
disabilities an individual user may have.
Furthermore, if users with disabilities cannot easily
browse your website, they may visit your
competitors’ websites and your company may lose
business. You should therefore ensure that people
with disabilities—such as those who have low
vision or are hard-of-hearing—can use your web
application and that their web browsers can parse
the HTML content your site presents. Because
MVC views are responsible for rendering HTML in
an MVC web application, you render accessible
HTML by writing views that follow accessibility guidelines. When you write views, keep the following
challenges and best practices in mind.

Users have different requirements depending on their abilities and disabilities. For example, consider the
following factors:

o Users with low vision may use a standard browser, but they may increase text size with a screen
magnifier so that they can read the content.

o Profoundly blind users may use a browser with text-to-speech software or text-to-Braille
hardware.

o Color-blind users may have difficulty if a color difference is used to highlight text.

o Deaf users may not be able to access audio content.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-9

o Users with limited dexterity may find it difficult to click small targets.

o User with epilepsy may have seizures if presented with flashing content.

You can ensure that your content is accessible to the broadest range of users by adhering to the following
guidelines:

o Do not rely on color differences to highlight text or other content. For example, links should be
underlined or formatted in bold font to emphasize them to color-blind users.

o Always provide equivalent alternative content for visual and auditory content. For example,
always complete the alt attribute for images. Use this attribute to describe the image so that
text-to-speech software or text-to-Braille hardware can render meaningful words to the user.

o Use markup and style sheets to separate content from structure and presentation code. This helps
text interpretation software to render content to users without being confused by structural and
presentation code. For example, you should apply the following best practices to display content
on your webpage:
 Avoid using tables to display the content. You should use tables only to present tabulated
content. Tables can be used to render graphics and branding on a webpage, but in an
accessible site, use positional style sheets to display content. Text readers do not read style
sheets.
 Avoid using nested tables. In a nested table, a table cell contains another table. These are
particularly confusing to text readers because they read each table cell in a sequential order.
The user is likely to become disoriented and unable to determine which cell is being read
and what it means.
 Avoid using images that include important text. Text readers cannot render text from within
an image file. Instead, use markup to render this text.

Additional Reading: The World Wide Web Consortium (W3C) has a project called the
Web Accessibility Initiative (WAI) that promotes accessible web content. This project has
published the Web Content Accessibility Guidelines (WCAG). These guidelines are accepted by
the web publishing industry as definitive. To read the full guidelines, go to:
http://www.w3.org/TR/WCAG10/
Note: In ASP.NET MVC, the developer has complete control over the HTML that the web
server sends to the web browser. However, you must understand accessibility principles to write
accessible MVC views. Ensure that your entire development team is familiar with the
requirements of accessibility and the related best practices.

Question: You want to present your company logo at the uppermost part of the page. The logo includes
the name of the company in an unusual font. How can you ensure that the logo is accessible?
MCT USE ONLY. STUDENT USE PROHIBITED
05-10 Developing ASP.NET MVC 4 Views

Alternative View Engines


A view engine is a component of the MVC
framework that is responsible for locating view
files, running the server-side code they contain,
and rendering HTML that the browser can display
to the user. Many views work with the Razor view
engine. Razor is the default view engine in MVC 4
and is a highly flexible and efficient view engine.

However, some developers may prefer to use a


different view engine for the following reasons:
o View Location Logic. Razor assumes that
all views are located in the Views top-
level folder. Within this, Razor assumes
that each controller has a separate folder. For example, views for the ProductController are
located in the Product folder within Views. Razor also looks for some views in the Shared
subfolder. These views can be used by more than one controller. If you do not like this view
location logic, you can use an alternate view engine, or create a custom view engine.

o View Syntax. Each view engine uses a different syntax in view files. For example, in Razor, the @
symbol delimits server-side code. By contrast, in the ASPX view engine, <% %> delimiters are
used. This is familiar to developers who have worked with Web Forms. You may prefer one syntax
over another for the following reasons:
 Experience. The technologies that you or your team have previously worked with may make it
easier for you to learn the syntax of a specific view engine.
 Readability. Some view engines work with view files that are easily readable for anyone who
knows HTML. Web designers, who do not know C# or other programming languages, may
be able to learn readable syntaxes.
 Succinctness. Some view engines work with view files that are terse and generate HTML with
relatively small amounts of code.

Common View Engines


The following table describes and compares four popular view engines.

View
Description Code Example
Engine

Razor This is the default view engine


for MVC 4. It is easy to learn @model
for anyone who has IEnumerable<MyWebSite.Models.Produc
t>
experience in HTML and C# or
Visual Basic. Visual Studio <ul>
provides good IntelliSense @foreach (var p in Model)
feedback for Razor code and it {
is compatible with unit testing. <li>Name: @p.Name</li>
}

ASPX This was the default view


engine for MVC 1 and 2. It is <%@ Control Inherits=
easy to learn for developers "System.Web.Mvc.ViewPage<IEnumerabl
e<Product>>" %>
with experience in ASP.NET <ul>
Web Forms and it is also <% foreach(var p in model){%>
known as the Web Forms View <li><%=p.Name%></li>
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-11

View
Description Code Example
Engine
Engine. It includes good <%}%>
IntelliSense feedback. </ul>

NHaml NHaml is a .NET Framework


version of the Haml view @type=IEnumerable<Product>
engine used with Ruby on %ul
- foreach (var p in model)
Rails, a competitor to ASP.NET
%li= p.Name
MVC. Developers who have
developed websites with Ruby
on Rails may prefer this view
engine. Visual Studio does not
provide IntelliSense feedback
for this view engine. NHaml
syntax is terse and efficient,
but often less intelligible to
inexperienced developers.

Spark This view engine uses view


files that are easily readable <viewdata
and similar to static HTML products="IEnumerable[[Product]]"/>
<ul if="products.Any()">
files. This design results in view <li each="var p in
files that are readable for products">${p.Name}</li>
anyone who knows HTML. You </ul>
can obtain IntelliSense support
for Spark in Visual Studio.

Additional Reading: For more information about alternative view engines, go to:
http://stackoverflow.com/questions/1451319/asp-net-mvc-view-engine-comparison
http://channel9.msdn.com/coding4fun/articles/Developer-Review-Four-ASPNET-MVC-View-
Engines

Creating Custom View Engines


It is also possible to create your own view engine to implement custom logic for locating views or to
implement custom syntax for view files. Custom view engines are rarely created because the existing view
engines are powerful, flexible, and easy to learn. The default logic for finding view files is also simple and
rarely needs modification. To create a custom view engine, you must take the following steps:

o Create a View Class. This class must implement the IView interface and have a Render() method.
You need to write the custom logic in the Render() method for interpreting custom syntax. This
code is likely to include regular expressions and other sophisticated string-parsing code.

o Create a View Engine. This class should inherit the VirtualPathProviderViewEngine class and
include the CreateView() and CreatePartialView() methods. By inheriting from the
ViewPathProviderViewEngine class, you can modify the ViewLocationFormat property to
implement custom logic for locating views.

o Register the Custom View Engine. To register your custom view engine with MVC, call
ViewEngines.Engines.Add() in the Global.asax file.
MCT USE ONLY. STUDENT USE PROHIBITED
05-12 Developing ASP.NET MVC 4 Views

Additional Reading: For more information about creating custom view engines, see:
http://www.singingeels.com/Articles/Creating_a_Custom_View_Engine_in_ASPNET_MVC.aspx

Question: Is there any HTML markup that the Razor view engine cannot render?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-13

Lesson 2
Using HTML Helpers
The ASP.NET MVC 4 Framework includes many HTML helper functions that you can use in views. HTML
helpers are C# methods that you can call to render values, labels, and input controls such as text boxes.
Helpers include common logic that makes it easier for you to render HTML for MVC model class
properties and other tasks. Some HTML helpers examine the definition of a property in a model class and
then render appropriate HTML. Some HTML helpers call controller actions and others validate user input
data. You can also create custom HTML helpers. You need to know the appropriate HTML helper to use in
any scenario and how to pass the correct parameters to relevant HTML helpers.

Lesson Objectives
After completing this lesson, you will be able to :

o Use the Html.ActionLink and Url.Action helpers to call a controller action.


o Use the Html.DisplayNameFor and HtmlDisplayFor helpers to display the value of a model
class property.

o Use the Html.LabelFor and HTML.EditorFor helpers to render an editor control for a model
class property.

o Use the Html.BeginForm helper to render a form for a controller action.

o Use HTML helpers to validate data typed by users.


o Use various HTML helpers to build a user interface for a web application.

Using Action Helpers


HTML helpers are simple methods that in most
cases return a string. This string is a small section
of HTML that the view engine can insert into the
view file to generate the completed webpage. You
can write views that render any type of HTML
content without using a single helper if you prefer
not to use HTML helpers. However, HTML helpers
make the task of managing HTML content easier
by rendering common HTML strings for often-
used scenarios.

The Html.ActionLink() and Url.Action() helpers


enable you to render HTML that calls controller
actions.

The Html.ActionLink() Helper


When the user makes a request to an MVC web application, the ASP.NET MVC 4 framework forwards the
request to the correct controller and action, thereby passing the correct parameters. It is possible for users
to make such requests by typing a URL into the address bar of a web browser, but they rarely know the
name of the controller, action, or parameters. Instead, they usually click a link. The Html.ActionLink()
helper can be used to render links to actions for you. The helper returns an <a> element with the correct
href parameter value when users click a link in a webpage.
MCT USE ONLY. STUDENT USE PROHIBITED
05-14 Developing ASP.NET MVC 4 Views

In the following code example, the Html.Actionlink() helper renders an <a> element that calls the
Display action in the Photo controller, and passes an integer for the photo ID.

Using the Html.ActionLink() Helper


@Html.ActionLink("Click here to view photo 1", "Display", new { id = 1 })

By default, the example renders the following HTML: <a href="/photo/display/1">Click here to view
photo 1</a>. Notice that you pass parameters to the helper by using an anonymous object. In this
manner, you can pass multiple parameters.

Note: The Html.ActionLink() helper works with the routing engine to formulate the
correct URL for the link. The helper will render the href value above, assuming it is called from a
PhotoController view. There are several overloads of the ActionLink helper, however, if you wish
to use it to call actions in other controllers.

The Url.Action Helper


The Url.Action() helper renders a URL by using the routing engine without rendering the enclosing <a>
element.

You can use the Url.Action() helper to populate the src attribute of an <img> element or <script>
element. This helper is useful whenever you want to formulate a URL to a controller action without
rendering a hyperlink.

The following code example shows how to render an image by calling the Url.Action helper within the src
attribute of an img element:

Rendering an Image from an Action


<img alt="This image came from an action" src="@Url.Action("GetImage", new { id = 1 })"
/>

Additional Reading: For more information about the Html.ActionLink() and Url.Action()
helpers, see:
http://msdn.microsoft.com/en-us/library/dd505040(v=vs.108).aspx
http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.108).aspx

Question: You want to render an HTML5 <audio> tag to play a sound file from an action. Would you use
the Html.ActionLink() helper or the Url.Action() helper?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-15

Using Display Helpers


MVC includes several helpers that display
properties from model classes. You can use these
helpers to build views that display product details,
comment details, users details and so on.
Html.DisplayNameFor() renders the name of a
model class property. Html.DisplayFor() renders
the value of a model class property. Both these
helpers examine the definition of the property in
the model class, including the data display
annotations, to ensure that they render the most
appropriate HTML.

The Html.DisplayNameFor() Helper


You use the Html.DisplayNameFor() helper to render the display name for a property from the model
class. If your view is strongly-typed, Visual Studio checks whether the model class contains a property with
the right name as you type the code. Otherwise, you must ensure that you use a property that exists or
verify that it is not null before you use it. You specify the property of the model class to the
Html.DisplayNameFor() HTML helper by using a lambda expression.

The following code example renders the display name of the CreatedDate property by using the
Html.DisplayNameFor() HTML helper .

Using the Html.DisplayNameFor() Helper


@Html.DisplayNameFor(model => model.CreatedDate)

The text rendered by the Html.DisplayNameFor() helper depends on the model class. If you used a
DisplayName annotation to give a more descriptive name to a property, then Html.DisplayNameFor() will
use that DisplayName annotation value; otherwise, it will render the name of the property.

The Html.DisplayFor() Helper


The Html.DisplayFor() helper considers any display annotations that you specify in the model class, and
then renders the value of the property.

In the following code example, the Html.DisplayFor() helper is used to render a date string.

Using the Html.DisplayFor() Helper


This was created on: @Html.DisplayFor(model => model.CreatedDate)

If you used the DisplayFormat annotation in the model class with DataFormatString set to
“{0:dd/MM/yy}”, then the Html.DisplayFor() helper ensures that the date is displayed in short date
format.

Question: You want to ensure that a view displays “This product was last changed on” before the
ModifiedDate property. This text is declared in the class with the DisplayName annotation. What code
would you write in the view?
MCT USE ONLY. STUDENT USE PROHIBITED
05-16 Developing ASP.NET MVC 4 Views

The Begin Form Helper


To accept user input, you must provide a form in
your webpage. A typical form consists of a set of
labels and input controls. The labels indicate to
the user the property for which they must provide
a value. The input controls enable the user to
enter a value. Input controls can be text boxes,
check boxes, radio buttons, file selectors, drop-
down lists, or other types of control. There is
usually a submit button and cancel button on
forms.

Rendering HTML Forms


To build a form in HTML, you must start with a
<form> element in the HTML webpage. All labels and input controls must be within the <form>
element. In an MVC view, you can use the Html.BeginForm() helper to render this element and set the
controller action to which the form sends data.
You can also specify the HTTP method that the form uses to submit data. If the form uses the POST
method, which is the default, the browser sends form values to the web server in the body of the form. If
the form uses the GET method, the browser sends form values to the web server in the query string in the
URL.

In the rendered HTML, the <form> element must be closed with a </form> tag. In Razor views, you can
ensure that the form element is closed with an @using code block. Razor renders the </form> tag at the
closing bracket of the code block.

The following code example shows how to render a form that sends data to the Register action in the
CustomerController. The form uses the POST HTTP verb:

Using the Html.BeginForm() Helper


@using (Html.BeginForm("Register", "Customer", FormMethod.Post))
{
@* Place input controls here *@
}

Using Forms to Post Files


In HTML, if you want to enable website users to upload files, you should use an HTML form with special
properties. Specifically the <form> element should include the enctype attribute set to the value
multipart/form-data and the form must use the POST HTTP verb.

You can specify additional attributes for the <form> element by passing them in an htmlAttributes
parameter as new objects. This is a good method to use to add the enctype attribute. If the form includes
any uploaded files, you must set the enctype attribute to multipart/form-data.

The following code example shows how to render a file upload form that sends data to the Create action
in the ProductController. The form uses the POST verb and sets the enctype attribute:

Using the Html.BeginForm() Helper


@using (Html.BeginForm("Create", "Photo", FormMethod.Post, new { enctype =
"multipart/form-data" }))
{
@* Place input controls here *@
}
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-17

Question: You have created a form with a file selector control that uses the GET method. You have set the
enctype attribute to multipart/form-data, but when you try to access the file in the action method, an
exception is thrown. What have you possibly done incorrectly?

Using Editor Helpers


Within HTML forms, there are many HTML input
controls that you can use to gather data from
users. In Razor views, the Html.LabelFor() and
Html.EditorFor() helpers make it easy to render
the most appropriate input controls for the
properties of a model class. To understand how
these helpers render HTML, you must first
understand the HTML input controls. Some
common HTML controls are described in the
following table:

Control Example Description

Text <input type="text" A single-line text box in which the user can enter a string. The
box name="Title"> name attribute is used to identify the entered value in the
query string or to send form data by using the POST method.

Multi- <textarea A multi-line text box in which the user can enter longer
Line name="Description" strings.
text box rows="20"
cols="80">

Check <input A check box submits a Boolean value.


box type="checkbox"

name="ContactMe">
Please send me
new offer details

Additional Reading: For more information about all HTML form elements, go to:
http://www.w3.org/TR/html401/interact/forms.html#h-17.4

The Html.LabelFor() Helper


This helper is similar to the Html.DisplayNameFor() helper because it renders the name of the property
that you specify, taking into account the DisplayName annotation if it is specified in the model class.
However, unlike the Html.DisplayNameFor() helper, the Html.LabelFor() helper renders a <label>
element.

The following code example renders a label for the CreatedDate property.

Using the Html.LabelFor() Helper


@Html.LabelFor(model => model.CreatedDate)
MCT USE ONLY. STUDENT USE PROHIBITED
05-18 Developing ASP.NET MVC 4 Views

The Html.EditorFor() Helper


The Html.EditorFor() helper renders HTML <input> elements and other form controls for properties
from an model class. This helper renders the most appropriate element for each property data type. For
example, the Html.EditorFor() helper renders <input type="text"> for a string property. If the string
property is annotated with [DataType(DataType.MultilineText)], the Html.EditorFor() helper renders a
<textarea> element instead. The following table describes the HTML that Html.EditorFor() renders for
different model classes:

Control Model Class Property HTML Rendered by EditorFor()

Text box public string Title { get; set; } <input type="text" name="Title">

Multi-Line text [DataType(DataType.MultilineText)] <textarea name="Description"


box public string Description { get; set;} rows="20" cols="80">

Check box [Display(Name = "Please send me new <input type="checkbox"


offer details")] name="ContactMe">
public bool ContactMe { get; set; } Please send me new offer details.

If the action passes an existing model object to the view, the Html.EditorFor() helper also populates each
control with the current values of each property.

The following code example renders a check box for the ContactMe property.

Using the Html.EditorFor() Helper


@Html.EditorFor(model => model.ContactMe)

Question: You have a property in the Product model class named ProductID. You want to include this in
the HTML page so that client-side script can use the ProductID value. However, you do not want the
value to be displayed to users. In the model class, you have annotated the ProductID property with the
[HiddenInput(DisplayValue=false)] attribute. How will the Html.EditorFor() helper render this
property?

Using Validation Helpers


When you request information from users, you
often want the data they enter in a certain format
for further use in the web application. For
example, you might want to ensure that users
enter a value for the LastName property. You
might also want to ensure that users enter a valid
email address for the EmailAddress property. You
can set these requirements by using validation
data annotations in the model class. In controller
actions, you can check the ModelState.IsValid
property to verify if a user has entered valid data.

When users submit a form, if they have entered


invalid data, most websites display validation messages. These messages are often highlighted in red, but
other colors or formats can be used. Often, there is a validation summary at the top of the page such as
“Please enter valid information for the following fields: Last Name, Email Address”. The form may display
detailed validation messages next to each control in which the user entered invalid data, or highlight the
problems in user supplied data with an asterisk.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-19

When your MVC view displays a form, you can easily show validation messages by using the validation
helpers. Validation helpers render HTML only when users have entered invalid data.

The Html.ValidationSummary() Helper


Use the Html.ValidationSummary() helper to render a summary of all the invalid data in the form. This
helper is usually called at the top of the form. When the user submits invalid data, the validation messages
are displayed for each invalid field in a bulleted list. You can control the validation messages in the model
class by using this syntax in the annotation: [Required(ErrorMessage="Please enter your last name")]

The following code example renders a summary of validation messages.

Using the Html.ValidationSummary() Helper


@Html. ValidationSummary()

The Html.ValidationMessageFor() Helper


Use the Html.ValidationMessageFor() helper to render validation messages next to each input in the
form.

The following code example renders a validation message for the LastName property of the model class.

Using the Html.ValidationMessageFor() Helper


@Html.ValidationMessageFor(model => model.LastName)

In this demonstration, you will see how to create a view and use HTML helpers to render controls in it.
You will also see how validation requirements, which were set with data annotations in the model class,
take effect in a view.

Demonstration Steps
1. Add a Create view for the Opera model class by using the following information:

o Controller name: OperaController


o Action name: Create

o View Name: Create

o View Type: Strongly-typed


o Model class: Opera

o Scaffold template: Empty

o Layout or master page: None


2. Add a title to the webpage by using the following information:

o Start tag: <h2>

o Text: Add an Opera

o End tag: </h2>

3. Add a using statement to the view, to render an HTML form, by using the following information:

o Helper: Html.BeginForm

o Action name: Create

o Controller name: Opera

o Form method: POST


MCT USE ONLY. STUDENT USE PROHIBITED
05-20 Developing ASP.NET MVC 4 Views

4. Add a P element to render controls for the model.Title property, by using the following helpers:

o Html.LabelFor()

o Html.EditorFor()

o Html.ValidationMessageFor()

5. Add a P element to render controls for the model.Year property, by using the following helpers:

o Html.LabelFor()

o Html.EditorFor()

o Html.ValidationMessageFor()
6. Add a P element to render controls for the model.Composer property, by using the following
helpers:

o Html.LabelFor()
o Html.EditorFor()

o Html.ValidationMessageFor()

7. Add a Create button at the end of the form, by using the following information:

o Tag: <input>

o Type attribute: submit

o Value: Create

8. Add a link to the Index action, at the end of the form, by using the following information:

o Helper: Html.ActionLink()

o Link Text: Back to List


o Action Name: Index

9. Start the web application with debugging, and navigate to the list of Operas.

10. Attempt to create an Opera with the following values:

o Title: Blank

o Year: 1597

o Composer: Blank

11. Create an opera with the following values:

o Title: Rigoletto

o Year: 1851

o Composer: Verdi

12. Stop debugging and close Microsoft Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-21

Lesson 3
Reusing Code in Views
In a web application, you frequently render similar blocks of HTML content in different webpages. For
example, in an e-commerce web application, you might want to display the most popular products on the
home page, on the front page of the product catalog, at the top of the product search page, and perhaps
in other locations. To render such HTML content in an MVC application, you may copy and paste code
from one Razor view into others. However, to change the display of top products later in the project, you
would then have to make identical changes in many locations. A better practice is to use a partial view. A
partial view renders a section of HTML content that can be inserted into several other views at run time.
Because the partial view is a single file that is reused in several locations, when you implement a change in
a single location, the changes are updated in other locations in the web application. Partial views increase
the manageability of MVC web applications and facilitate a consistent presentation of content throughout
a web application.

Lesson Objectives
After completing this lesson, you will be able to:
o Describe how partial views enable you to reuse Razor code.

o Create partial views in an MVC 4 web application.

o Use partial views in an MVC 4 web application.


o Determine when to use partial views.

Creating Partial Views


You can use partial views to render identical
HTML or similar HTML in different locations in
your web application. Consider a web application
in which users can comment on articles. In the
article view, you want to display the comments for
that specific article at the end of the page. You
want a similar display on the home page, but you
want to display the most popular comments on
any article. You can build a single comments
partial view for this scenario as follows:

o First, create a partial view that displays a


collection of comments in the required
format.

o Next, create a controller action that retrieves the most popular comments, and then passes them
to the partial view.

o Then, create another controller action that uses the article ID to find comments specific to that
article. The controller action then passes this collection of comments to the partial view.

o Finally, call the appropriate actions from the home page and the article view.
Because the partial view renders a collection of comments, you can reuse it for various situations by
passing different collections to it from controller actions.
MCT USE ONLY. STUDENT USE PROHIBITED
05-22 Developing ASP.NET MVC 4 Views

Creating and Naming Partial Views


In Visual Studio, you can create a partial view by using the Add View dialog, in the same manner as
creating any other view. Ensure that you select the Create as a Partial View check box. Because partial
views render a small section of HTML rather than a complete webpage, Visual Studio does not add
<head>, <body>, and other tags to the partial view. By convention, partial views are named prefixed
with an underscore character, for example, _CommentsList.cshtml. This convention is optional but is
often helpful to distinguish partial views in Solution Explorer.

Partial views are often created inside the /Views/Shared folder in your site. Views in the Shared folder
are accessible to many controllers, whereas views in the /Views/Comment folder are accessible only from
the CommentController.

Strongly-typed and dynamic partial views


Similar to other views, you can create strongly-typed partial views if you are certain that the partial view
will always display the same model class. Visual Studio can provide the most informative IntelliSense
feedback and error-checking for strongly-typed partial views, when compared with other types of views. A
strongly-typed view has the declaration of the @model directive at the top of the file. Alternatively, you
can create a dynamic partial view if you are not certain that the partial view will always display the same
model class. You can create dynamic partial views by omitting the @model directive declaration.
In the following example partial view, you can see the @model declaration that makes the partial view
strongly-typed. You can also see that the partial view renders only an unordered list and not a complete
HTML webpage as the following lines of code indicate.

A Strongly-Typed Partial View


@model IEnumerable<Adworks.Models.Comment>
<ul>
@foreach (var item in Model) {
<li>
Subject: @Html.DisplayFor(modelItem => item.Subject)
</li>
}
</ul>

Question: You want to create a partial view that displays a list of comments. The comments partial view
will be called by actions in the PhotoController and the HomeController. In which folder should you
create the partial view file?

Using Partial Views


Besides knowing how to create partial views, you
need to know how to use them within multiple
other views in your site. You can reuse partial
views by using the Html.Partial() and
Html.Action() HTML helpers.

The Html.Partial() Helper


You can use the Html.Partial() helper to render a
partial view within another view file. MVC inserts
the HTML that the partial view renders into the
parent view and returns the complete webpage to
the browser. The first parameter is the name of
the partial view file without its file extension. The
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-23

second optional parameter is a model object for the partial view to render. This is the same model object
that the parent view uses because other model objects are not available in this context.

The following lines of code call the _Comments.cshtml partial view without passing a model object.

Using the Html.Partial() Helper


@Html.Partial("_Comments")

Remember that you can use the ViewBag and ViewData collections to pass data between the action
method and the view. A parent view will share the same ViewBag or ViewData objects with the partial
view. This is a good way to share data between the controller action, view, and partial view.

The Html.Action() Helper


You can pass a model object from the parent view to a partial view when you use Html.Partial().
However, the parent view can only pass the same model object available in the parent view. Sometimes,
you may want to use a model object different from the parent view, often of a different model class, with
the partial view. In such cases, you can use the Html.Action() helper. This calls an action method that
returns the correct partial view. The action method can create an instance of any model class.

Consider an Article view that displays the text and title of an Article object. You want to display
comments at the end of this view in a partial view. You can use the Html.Action() helper to call an action
in the Comments controller that returns such a partial view. The partial view can work with a collection of
Comment objects.
In the following example, the Html.Action() helper calls an action in the CommentController controller
and passes the ID of the current article model object.

Using the Html.Action() helper


@Html.Action("_CommentsForArticle", "Comment", new { ArticleID = Model.ID })

Question: You want to display user reviews of a product to each product page. You have created a
_ReviewsForProduct partial view. Would you use Html.Partial() or Html.Action() to render this view?

Discussion: Partial View Scenarios


Consider the following scenarios when creating a
web application:

o You want to display the ten latest


comments in the web application. When
users click the Latest Comments link on
the home page, they will see a new
webpage with the comments displayed.

o You want to display the comments for an


article on the Article view. You also want
to display the comments for a product on
the Product view. There are separate
ArticleComment and ProductComment
classes in your model, but they have similar properties.

o You have a photo sharing web application and you want to display a gallery of thumbnail images
on both the AllPhotos view and the home page Index view. The AllPhotos view should show
MCT USE ONLY. STUDENT USE PROHIBITED
05-24 Developing ASP.NET MVC 4 Views

every Photo object, but the home page Index view should display only the three most recent
photos uploaded.

For each scenario, discuss with the class:

o Whether you need to create a view or a partial view.

o Whether you need to create a strongly-typed view or a dynamic view.


o If you decide to create a partial view, whether you need to use the Html.Partial() or the
Html.Action() HTML helper in the parent view.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-25

Lab: Developing ASP.NET MVC 4 Views


Scenario
You have been asked to add the following views to the photo sharing application:

o A Display view for the Photo model objects. This view will display a single photo in a large size,
with the title, description, owner, and created date properties.

o A Create view for the Photo model objects. This view will enable users to upload a new photo to
the gallery and set the title and description properties.

o A Photo Gallery partial view. This view will display many photos in thumbnail sizes, with the title,
owner, and created date properties. This view will be used on the All Photos webpage to display
all the photos in the application. In addition, this view will also be used on the home page to
display the three most recent photos.

After adding these three views to the photo sharing application, you will also test the working of the web
application.

Objectives
After completing this lab, you will be able to:

o Add Razor views to an MVC application and set properties such as scaffold and model binding.
o Write both HTML markup and C# code in a view by using Razor syntax.

o Create a partial view and use it to display re-usable markup.

Lab Setup
Estimated Time: 50 minutes

Virtual Machine: 20486A-SEA-DEV11

User Name: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:

• Enable the Allow NuGet to download missing packages during build option in Package Manager.

Exercise 1: Adding a View for Photo Display


Scenario
In this exercise, you will:

o Create a new view in the Photo Sharing web application to display single photos in large size.
o Display the properties of a photo such as title, description, and created date.

The main tasks for this exercise are as follows:

1. Add a new display view.

2. Complete the photo display view

 Task 1: Add a new display view.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11


MCT USE ONLY. STUDENT USE PROHIBITED
05-26 Developing ASP.NET MVC 4 Views

o User name: Admin

o Password: Pa$$w0rd

2. Open the Photo Sharing Application solution from the following location:

o File location: Allfiles (D):\ Labfiles\Mod05\Starter\PhotoSharingApplication

3. Add a new view to the Display action in the PhotoController by using the following information:

o Name: Display

o View Engine: Razor (CSHTML)

o View Type: Strongly typed


o Model Class: Photo

o Scaffold Template: Empty

o Layout or master page: None

 Task 2: Complete the photo display view


1. Add a title to the display view by using the Title property of the Model object.

2. Add an H2 element to the body page to display the photo title on the page by using the Title
property of the Model object.
3. Add an <img> tag to display the photo on the page by using the following information:

o Width: 800

o src: Empty

4. Add the URL.Action helper to the src attribute of the <img> tag by using the following information:

o Method: Url.Helper()

o Action Name: GetImage


o Controller Name: Photo

o Route Values: new { id=Model.PhotoID }

5. Add a P element to display the Description property from the model by using the following
information:

o Helper: Html.DisplayFor

o Lamda Expression: model =>


model.Description

6. Add a P element to display the UserName property from the model by using the following
information:

o Helpers:
 Html.DisplayNameFor
 HtmlDisplayFor
o Lamda Expression: model => model.UserName

7. Add a P element to display the CreatedDate property from the model by using the following
information:

o Helpers:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-27

 Html.DisplayNameFor
 HtmlDisplayFor
o Lamda Expression: model => model.CreatedDate

8. Add a P element to display a link to the Index controller action by using the following information:

o Helper: HTML.ActionLink

o Content: Back to List

9. Save the Display.cshtml file.

Results: After completing this exercise, you will be able to add a single display view to the Photo Sharing
web application and display the properties of a photo.

Exercise 2: Adding a View for New Photos


Scenario
In this exercise, you will
o Create a view to upload new photos for display in the Photo Sharing application.

o Display the properties of a photo such as title, description, and created date.

The main tasks for this exercise are as follows:


1. Add a new create view.

2. Complete the photo create view.

 Task 1: Add a new create view.


1. Create a new view for the Create action of the PhotoController class by using the following
information:

o Name: Create

o View: Razor (CSHTML)

o View type: Strongly Typed

o Model class: Photo

o Scaffold template: Empty

o Partial view: None

o Layout or master page: None

 Task 2: Complete the photo create view.


1. Add the following title to the Create view:

o Title: Create New Photo

2. Add an H2 element to the body page to display the heading as Create New Photo.

3. Create a form on the page by using the following information:

o Helper: Html.BeginForm

o Action: Create

o Controller name: Photo


MCT USE ONLY. STUDENT USE PROHIBITED
05-28 Developing ASP.NET MVC 4 Views

o Form method: FormMethod.Post

o Parameter: Pass the HTML attribute enctype = "multipart/form-data"

4. In the form, use the Html.ValidationSummary helper to render validation messages.

5. After the ValidationSummary, add a P element to display controls for the Title property of the
model by using the following information:
o Helpers:
 LabelFor
 EditorFor
 ValidationMessageFor
6. After the controls for the Title property, add a P element to display a label for the PhotoFile
property and an <input> tag by using the following information:
o Helper: LabelFor

o Input type: file

o Name: Image

7. After the PhotoFile controls , add a P element to display controls for the Description property of the
model by using the following information:

o Helpers:
 LabelFor
 EditorFor
 ValidationMessageFor
8. After the Description controls, add a P element to display read-only controls for the UserName
property of the model by using the following information:

o Helpers:
 LabelFor
 DisplayFor
9. After the UserName controls, add a P element to display read-only controls for the CreatedDate
property of the model by using the following information:

o Helpers:
 LabelFor
 DisplayFor
10. After the CreatedDate controls, add a P element that contains an <input> tag by using the
following information:

o Input type: submit

o Value: Create

o Add an action link to the Index action with the text Back to List.

11. Save the Create.cshtml file.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-29

Results: After completing this exercise, you will be able to create a web application with a Razor view to
display new photos.

Exercise 3: Creating and Using a Partial View


Scenario
In this exercise, you will:

o Add a gallery action to the Photo Controller.

o Add a photo gallery partial view.

o Complete the photo gallery partial view.

o Use the photo gallery partial view.

The main tasks for this exercise are as follows:

1. Add a gallery action to the Photo Controller.

2. Add a photo gallery partial view.

3. Complete the photo gallery partial view.

4. Use the photo gallery partial view.

 Task 1: Add a gallery action to the Photo Controller.


1. Add a new action to the PhotoController.cs file by using the following information:

o Annotation: ChildActionOnly

o Scope: public

o Return Type: ActionResult

o Name: _PhotoGallery

o Parameter: an Integer called number with a default value of 0

2. Create a new List of Photo objects named photos. Add an if statement, to set photos to include all
the Photos in the context object, if the number parameter is zero.

3. If the number parameter is not zero, set photos to list the most recent Photo objects. The number of
Photo objects in the list should be the number attribute.

4. Pass the photos object to the partial view _PhotoGallery and return the view.

5. Save the PhotoController.cs file.

 Task 2: Add a photo gallery partial view.


1. Create a new partial view for the _PhotoGallery action in the PhotoController.cs file by using the
following information:

o Name: _PhotoGallery

o View Type: Strongly Typed

o Model Class: Photo

o Scaffold Template: Empty


o Layout or master page: None

2. Create a new folder in the PhotoSharingApplication project by using the following information:
MCT USE ONLY. STUDENT USE PROHIBITED
05-30 Developing ASP.NET MVC 4 Views

o Parent folder: Views

o Folder name: Shared

3. Move the _PhotoGallery.cshtml view file from the Photo folder to the Shared folder.

 Task 3: Complete the photo gallery partial view.


1. In the _PhotoGallery.cshtml partial view file, bind the view to an enumerable list of Photo model
objects.

2. In the _PhotoGallery.cshtml partial view file, add a For Each statement that loops through all the
items in the Model.

3. In the For Each statement, add an H3 element that renders the item.Title property.

4. After the H3 element, add an if statement that checks that the item.PhotoFile value is not null.

5. If the item.PhotoFile value is not null, render an <img> tag. Call the UrlAction helper to set the src
attribute by using the following information:
o Action: GetImage

o Controller: Photo

o Parameters: for the id parameter, pass item.PhotoID

6. After the if statement, add a P element, and call the @Html.DisplayFor helper to render the words
Created By: followed by the value of the item.UserName property.

7. After the UserName display controls, add a P element, and call the @Html.DisplayFor helper to
render the words Created On: followed by the value of the item.CreatedDate property.

8. After the CreatedDate display controls, call the Html.ActionLink helper to render a link by using the
following information:
o Link Text: Display

o View Name: Display

o Parameters: pass the item.PhotoID value as the id parameter

9. Save the _PhotoGallery.cshtml file.

 Task 4: Use the photo gallery partial view.


1. Modify the Index action in the PhotoController.cs so that no model class is passed to the Index view.

2. Create a view for the Index action in the PhotoController.cs file by using the following information:
o Name: Index

o View type: Not strongly typed

o Layout or master page: None

3. In the Index.cshtml file, change the title to All Photos.

4. Add an H2 element to the page body to display the heading as All Photos

5. Add a P element to add a link to the Create action in the Photo controller by using the following
information:

o Helper: Html.ActionLink

o Link Text: Add a Photo


o Action Name: Create
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-31

o Contoller Name: Photo

6. Insert the _PhotoGallery partial view by using the following information:

o Helper: Html.Action

o Action Name: _PhotoGallery

o Controller Name: Photo

7. Save the Index.cshtml file.

Results: After completing this exercise, you will be able to create a web application with a partial view to
display multiple photos.

Exercise 4: Adding a Home View and Testing the Views


Scenario
In this exercise, you will create a home page that re-uses the photo gallery object, but displays only the
three most recent photos.

The main tasks for this exercise are as follows:


1. Add a Controller and View for the home page.

2. Use the web application.

 Task 1: Add a Controller and View for the home page.


1. Add a new Controller to the home page by using the following information:
o Controller name: HomeController

o Template: Empty MVC Controller

2. Add a new view to the Index action in HomeController by using the following information:
o View Name: Index

o View type: Not strongly typed

o Layout or master page: None

3. Change the title of the page to Welcome to Adventure Works Photo Sharing.

4. Add the following text to the home page:

o Welcome to Adventure Works Photo Sharing! Use this site to share your adventures.
5. Add an H2 element to display the heading as Latest Photos.

6. Insert the _PhotoGallery partial view by using the following information:

o Helper: Html.Action
o Action Name: _PhotoGallery

o Controller Name: Photo

o Parameters: for the number parameter, pass the value 3

7. Save the Index.cshtml file.


MCT USE ONLY. STUDENT USE PROHIBITED
05-32 Developing ASP.NET MVC 4 Views

 Task 2: Use the web application.


1. Start the Photo Sharing web application with debugging.

2. Verify the number of photos displayed on the home page.

3. Display a photo of your choice to verify whether the display shows the required information.

4. Verify the number of photos displayed on the All Photos page.

5. Add a new photo of your choice to the application by using the following information:

o Title: My First Photo

o Description: This is the first test of the Create photo view.

o File path: Allfiles (D):\ Labfiles\Mod05\SamplePhotos


6. Close Internet Explorer and Microsoft Visual Studio.

Results: After completing this exercise, you will be able to create a web application in which users can
upload and view the photos.

Question: How can you improve the accessibility of the HTML that your photo views render?

Question: In the lab, how did you ensure that the Create view for Photo model objects
could upload photo files when the user clicked the Create button?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 05-33

Module Review and Takeaways


In this module, you have learned how to build the user interface for your web application by creating
views with the Razor view engine. Views can display properties from a model class and enable users to
create, read, update and delete objects of that model class. Some view display many objects by looping
through a collection of model objects. The HTML Helper methods that are built into ASP.NET MVC 4
facilitate the rendering of displays, controls, and forms and to return validation messages to users. Partial
views can be reused several times in your web application to render similar sections of HTML on multiple
pages.

Best Practice: Use Razor comments, declared with the @* *@ delimiters, throughout your Razor
views to help explain the view code to other developers in your team.
Best Practice: Only use @: and <text> tags when Razor misinterprets content as code. Razor has
sophisticated logic for distinguishing content from code, so this is rarely necessary.

Best Practice: Use strongly-typed views wherever possible because Visual Studio helps you to write
correct code by displaying IntelliSense feedback.

Common Issues and Troubleshooting Tips


Common Issue Troubleshooting Tip

When a controller tries to access a partial


view, an exception is thrown.

Review Question(s)
Question: You want to display the name of the Comment.Subject property in an MVC view
that renders an Edit form for comments. You want the label Edit Subject to appear to the left
of a textbox so that a user can edit the Subject value. Which HTML helper would you use to
render the field name?

Verify the correctness of the statement by placing a mark in the column to the right.

Statement Answer

If you Razor generates errors by wrongly interpreting content


as server-side code, this indicates that you have explicitly
declared the content by using the @* *@ delimiters.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Views
05-34
MCT USE ONLY. STUDENT USE PROHIBITED
06-1

Module06
Testing and Debugging ASP.NET MVC 4 Web Applications
Contents:
Module Overview 06-1 

Lesson 1: Unit Testing MVC Components 06-2 

Lesson 2: Implementing an Exception Handling Strategy 06-14 

Lab: Testing and Debugging ASP.NET MVC 4 Web Applications 06-23 

Module Review and Takeaways 06-31 

Module Overview
Software systems such as web applications are complex and require multiple components, often written
by different developers, to work together. Incorrect assumptions, inaccurate understanding, coding errors,
and many other sources can create bugs that result in exceptions or unexpected behavior. To improve the
quality of your web application and create a satisfying user experience, you must identify bugs from any
source and eliminate them. Traditionally, most testing has been performed at the end of a development
project. However, it has recently become widely accepted that testing throughout the project lifecycle
improves quality and ensures that there are no bugs in production software. You need to understand how
to run tests on small components of your web application to ensure that they function as expected before
you assemble them into a complete web application. You also need to know how to use the tools
available in Microsoft Visual Studio 2012 to debug and trace exceptions when they arise.

Objectives
After completing this module, you will be able to:

o Run unit tests against Model–View–Controller (MVC) components, such as model classes, and
locate potential bugs.

o Build an MVC application that handles exceptions smoothly and robustly.


MCT USE ONLY. STUDENT USE PROHIBITED
06-2 Testing and Debugging ASP.NET MVC 4 Web Applications

Lesson 1
Unit Testing MVC Components
A unit test is a procedure that instantiates a component that you have written, calls one aspect of the
functionalities of the component, and checks that the component responds correctly according to the
design. In object-oriented programming, unit tests usually instantiate a class and call one of its methods.
In an MVC web application, unit tests can instantiate model classes or controllers and call their methods
and actions. As a web developer, you need to know how to create a testing project and add tests to the
testing project. The tests check individual aspects of your code without relying on database servers,
network connections, and other infrastructure. This is because unit tests focus on the code you write.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the use of unit tests increase the quality of code you write.
o List the principles of Test Driven Development (TDD).

o Describe how loosely-coupled components increase the testability and maintainability of your
application.
o Write unit tests against model classes, controllers, and views in an MVC web application.

o Describe how Inversion of Control (IoC) containers can instantiate controllers and model classes.

o Run unit tests in Visual Studio 2012.

o Use a mocking tool to automate mock creation in unit tests.

Why Perform Unit Tests?


There are three types of tests you can use to
identify bugs in your application:

o Unit Tests: Unit tests check small aspects


of functionality. For example, a unit test
may verify the return type of a single
method. By defining many unit tests for
your project, you can ensure they cover
all functional aspects of your web
application.

o Integration Tests: Integration tests check


how two or more components work
together. They can be used to check how
two or more classes interact with each other. Integration tests can also be used to check how the
entire web application, including the database and external web services, works to deliver
content.

o Acceptance Tests: Acceptance tests focus on a functional or technical requirement that must work
for the stake holders to accept the application. Similar to integration tests, acceptance tests
usually test multiple components working together.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-3

Note: This lesson concentrates on unit tests because they can be defined early in
development. Integration and acceptance tests are usually run later when several components
are approaching completion.

What is a Unit Test?


A unit test is a procedure that verifies a specific aspect of functionality. Multiple unit tests can be
performed for a single class and even for a single method in a class. For example, you can write a test that
checks that the Validate method always returns a Boolean value. You might write a second test that
checks that, when you send a string to the Validate method, the method returns a true value. You can
assemble many unit tests into a single class called a test fixture. Often, a test fixture contains all the tests
for a specific class. For example, ProductTestsFixture may include all tests that check methods in the
Product class.

A single unit test usually consists of code that runs in three phases:

1. Arrange: In this phase, the test creates an instance of the class that it will test. It also assigns any
required properties and creates any required objects to complete the test. Only properties and
objects that are essential to the test are created.

2. Act: In this phase, the test runs the functionality that it must check. Usually in the act phase, the test
calls a single procedure and stores the result in a variable.

3. Assert: In this phase, the test checks the result against the expected result. If the result matches what
was expected, the test passes. Otherwise, the test fails.

How Do Unit Test Help Diagnose Bugs?


Because unit tests check a small and specific aspect of code, it is easy to diagnose the problem when they
fail. Unit tests usually work with a single class and isolate the class from other classes wherever possible. If
other classes are essential, the smallest number of classes are created in the arrange phase. This approach
enables you to fix issues rapidly because the number of potential sources of a bug is small in number.
Unit tests should check the code that you write and not any infrastructure that the production system will
rely on. For example, unit tests should run without connecting to a real database or web service because
network problems or service outages may cause a failure. Using this approach you can distinguish bugs
that arise from code, which must be fixed by altering code, from the bugs that arise from infrastructure
failures, which must be fixed by changing hardware, reconfiguring web servers, reconfiguring connection
strings or making other configuration changes.

Automated Unit Testing


It is important to understand that unit tests, after they are defined, can be rerun quickly and easily
throughout the rest of the project lifecycle. In fact, Microsoft Visual Studio reruns tests automatically
whenever you run a project with debugging. You can also manually initiate tests any time.

This is important because new code can cause bugs at any stage of the development process. As an
example, consider a project that proceeds through three phases. A unit test defines for phase 1 helps
highlight problems in phases 2 and 3 that might otherwise go unnoticed:

1. Phase 1: The ProductController class is defined and the Test_Index_Action unit test is written. The
test checks that the Index action works with an integer parameter. When you call the Index action
with an integer, the action returns a collection that includes the same number of Product objects.
The test passes.

2. Phase 2: A developer modifies the Index action so that it returns a partial view. The developer
renames the action _Index to conform to the team’s partial view naming convention. The test fails
because the name has changed. The developer modifies the test so that it calls the renamed action
and the test passes.
MCT USE ONLY. STUDENT USE PROHIBITED
06-4 Testing and Debugging ASP.NET MVC 4 Web Applications

3. Phase 3: A developer modifies the Index action by writing a different Language Integrated Query
(LINQ) query to implement a new functional requirement. However, the developer makes a mistake in
the LINQ query. The Index action now returns zero products whatever integer is passed as a
parameter. The test fails.

In Phase 2, the test failure arose because the action name had changed, not because of a mistake in the
action code. The solution was to change the name of the action called in the test method but this can
remind developers to alter calls to renamed actions throughout the solution.

In Phase 3, the test failure arose because the developer misunderstood LINQ or made a typing error in the
LINQ query. The unit test highlighted the error as soon as it arose. This helps the developer focus on a
solution to resolve the test failure and ensure that new functionality does not cause failures in code
written earlier in the project.
Question: Board members want you to ensure that your web application correctly calculates sales tax on
every product in the catalog. Is this an example of a unit test, an integration test, or an acceptance test?

Principles of Test Driven Development


You can use unit tests in any development
methodology, including waterfall projects,
iterative projects, and Agile projects to spot
potential bugs and improve the quality of the final
application. Any project can benefit from unit
testing, regardless of the development
methodology used in the project. However, a
specific development methodology, called Test
Driven Development (TDD) places unit testing at
the center of working practices. TDD is often
described as a separate development
methodology. Some authors consider it to be a
core principle of the Extreme Programming methodology.

Write The Test, Pass The Test, Refactor


In TDD, developers repeat the following short cycle to implement a functional requirement:

1. Write The Test: The developer starts by creating and coding the unit test. This step requires a full
understanding of the functional requirement, which can be obtained from use cases or user stories.
Because the developer has not written any code in the application, the test fails.

2. Pass The Test: The developer writes some quick and simple code in the application so that it passes
the test. During the first iteration, this code is frequently inelegant and may include false assumptions
such as hardcoded values.

3. Refactor: The developer cleans up the application code, removes duplicate code, removes hardcoded
values, improves readability, and makes other technical improvements. The developer reruns the test
to ensure that refactoring has not caused a failure.

The cycle is then repeated. In each iteration, the developer adds a small new element of the final
functionality with a corresponding test.

Test Driven Development Principles


TDD is different from traditional approaches to application development. To use TDD effectively, you
must understand the its fundamental principles:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-5

o Write tests before code: In the TDD development cycle, the test is created in step one before any
code in the application. This means the test must fail the first time it is run. You can understand
the test as a specification for the functionality you are building. By writing the test first, you
ensure that you begin with a thorough understanding of the problem you are trying to solve.

o Move in small steps: By breaking a large application down into small elements of functionality,
you can improve developer productivity. You can do this by giving a developer a small problem
to solve in each iteration. The developer can solve the simple problem quickly and understand all
the possible circumstances in which their code runs.
o Only write enough code to pass the test: In each iteration, do not be tempted to add extra code
that is not related to the test. For example, if you know that users will call an action with other
parameters than the ones in the test, do not write code to handle these parameters. Instead,
during the next iteration, write a second test for those parameters. Then write and refactor that
code.

Developers can refer to tests as examples of how to use a class or method. This can increase developer
productivity because developers can view the code that demonstrates the method or class.

Note: In the lab, you will use some TDD principals as you develop the solution. However,
the lab does not conform to a strict reading of the TDD methods.

Question: You have written a controller for the Comment model class. You write a unit test that checks
that the Index action returns a collection of Comment objects. Have you conformed to TDD principles?

Writing Loosely Coupled MVC Components


A loosely coupled application is one in which each
component requires few or no details of the other
components of the system. In object-oriented
programming, for example, two classes can be
described as loosely-coupled if one class can call
methods on the other class without any code that
is specific to the other class. When system
components are loosely-coupled in this manner, it
is easy to replace a class with another
implementation of the same functionality. Loosely
coupled components are essential for thorough
unit testing because classes that deal with real
data, such as data from a database, can be easily be replaced with classes that deal with test data.

Using Loose Coupling in Tests


A loosely-coupled application is easy to test because you can make tests simpler by replacing a fully
functional instance of a class with a simplified instance that is specifically designed for the test. Replacing
classes in tests in this manner can only be done when components are loosely coupled. Replacement
instances used for unit tests are known as test doubles or fakes. A test double or fake includes just enough
code and properties to pass the relevant test and prove the functionality.

Other Benefits of Loose Coupling


Loose coupling has other benefits besides testing. Loose coupling makes it easier to replace simple
components with more sophisticated ones—for example, in version one of an application you might write
a class that calculates a simple arithmetical mean. In version two, you might replace this class with a new
MCT USE ONLY. STUDENT USE PROHIBITED
06-6 Testing and Debugging ASP.NET MVC 4 Web Applications

class that calculates a weighted mean based on a more complex algorithm. If the components are loosely
coupled, you can perform this replacement without modifying any code outside of the averaging classes.

Using Interfaces for Loose Coupling


In object-oriented programming, an interface defines a set of properties and methods. Any class that
implements that interface must implement all the properties and methods it defines as a minimum. This
creates loose coupling because you need not specify a class in code. Instead, you can specify any
implementation of a particular interface.

Loose Coupling in an MVC Web Application


To show how loose coupling can be used in an MVC web application, consider the following scenario:

You are writing an MVC web application that displays a product catalog and enables users to click a
product to view the full details. On the product details page, users can view and add reviews of that
product. You want to ensure during testing that the Product.Reviews property, which is a collection of
Review objects, only includes reviews with the right ProductID value.

Recall that unit tests should not rely on infrastructure such as network connections or database servers,
but only on test code. By contrast, in production, the application will obtain all product reviews from a
database. To satisfy the needs of testing and production, you can build the following components:

o An IProduct interface. This includes a definition of the Reviews property, together with other
properties and methods. This interface is known as a repository.

o A Product class. This is the implementation of the IProduct repository that the application uses
in production. When the user calls the Reviews property, all the reviews for the current product
are obtained from the database.

o A TestProduct class. This is the test double or fake implementation of the IProduct repository
that the test uses. The test sets up a TestProduct object and fake reviews with different
ProductID values. The test calls the TestProduct.Reviews property and checks that only the
right reviews are returned.

Notice that, in the test, the TestProduct double uses in-memory data set up during the Arrange phase. It
does not query the database. Therefore, you can test your code without relying on the network
connection to the database or the database server itself. This approach also ensures that the test runs
quickly, which is important because slow tests discourage developers from running tests regularly.
Question: In a test, you create a fake collection of BlogEntry objects. Is this an example of an interface or
a test double?

Writing Unit Tests for MVC Components


The Microsoft ASP.NET MVC 4 programming
model is easy to integrate with the principles of
unit testing and TDD because of its separation of
concerns into model, controllers, and views.
Models are simple to test because they are
independent classes that you can instantiate and
configure during the arrange phase of a test.
Controllers are simple classes which you can test
but it is complex to test controllers with in-
memory data, rather than using a database. To
test controllers with in-memory data, you create a
test double class, also known as a fake repository.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-7

Objects of this class can be populated with test data in memory without querying a database. You need to
understand how to write test doubles and how to write a typical test for MVC classes.

Add and Configure a Test Project


In Microsoft Visual Studio 2012, you can test an MVC web application project by adding a new project to
the solution, based on the Unit Test Project template. You must add references to the test project so that
test code can access classes in the MVC web application project. You will also need a reference to
System.Web.Mvc and other namespaces from the Microsoft .NET Framework.

In a Visual Studio test project, test fixtures are classes marked with the [TestClass] annotation. Unit tests
are methods marked with the [TestMethod] annotation. Unit tests usually return void but call a method
of the Assert class, such as Assert.AreEqual() to check results in the test assert phase.

Note: There are many other test frameworks available for MVC web applications and you
can choose the one you prefer. Many of these frameworks can be added by using the NuGet
package manager available in Visual Studio. If you are using Visual Studio Express, you cannot
use the Visual Studio Unit Test Project template, but you can add other testing frameworks by
using NuGet.

Test Model Classes and Business Logic


In MVC, model classes do not depend on other components or infrastructure. You can easily instantiate
them in-memory, arrange their property values, act on them by calling a method, and assert that the
result was as expected. Sometimes you create business logic in the model classes, in which case the act
phase involves calling a method on the model class itself. If, by contrast, you have created a separate
business logic layer, code in the act phase must call a method on the business object class and pass a
model class.
The code in this test creates an instance of the Product model class and sets its Type property. The test
checks that, in these circumstances, the GetAccessories() method returns a collection of BikeAccessory
objects.

Testing a Model Class


[TestMethod]
public void Test_Product_Reviews()
{
//Arrange phase
Product testProduct = new Product();
testProduct.Type = "CompleteBike";
//Act phase
var result = testProduct.GetAccessories();
//Assert phase
Assert.AreEqual(typeof(IEnumerable(BikeAccessory)), result.GetType());
}

Create Repository Interfaces


A repository is an interface that defines properties and methods that MVC can use to store data in a web
application. It is common to create a single repository interface for your entire web application.

In this code example, the IWebStoreContext interface is a repository that defines methods such as Add()
and Delete() and properties such as Products.

A Repository Interface
public interface IWebStoreContext
{
IQueryable<Product> Products { get; }
MCT USE ONLY. STUDENT USE PROHIBITED
06-8 Testing and Debugging ASP.NET MVC 4 Web Applications

T Add<T>(T entity) where T : class;


Product FindProductById(int ID);
T Delete<T>(T entity) where T : class;
}

Implement and Use a Repository in the Application


The repository interface defines methods for interacting with data but does not fix how that data will be
set and stored. You must provide two implementations of the repository:

o An implementation of the repository for use in production and development. This


implementation will use data from the database or some other storage mechanism.

o An implementation of the repository for use in tests. This implementation will use data from
memory set during the arrange phase of each test.

The code example shows how to implement the repository interface in your MVC project for use in
production and development.

Implementing a Repository in the MVC Project


public class WebStoreContext : DbContext, IWebStoreContext
{
public DbSet<Product> Products { get; set; }
IQueryable<Product> IWebStoreContext.Products { get { return Photos; } }
T IWebStoreContext.Add<T>(T entity)
{
return Set<T>().Add(entity);
}
Product IWebStoreContext.FindProductById(int ID)
{
return Set<Product>().Find(ID);
}
T IWebStoreContext.Delete<T>(T entity)
{
return Set<T>().Remove(entity);
}
}

Note that the WebStoreContext class still inherits from the Entity Framework DbContext class but also
implements the IWebStoreContext interface that you just created. In fact, the interface methods such as
Add(), Delete(), and FindProductById() just wrap methods from the DbContext class such as
Remove().

Implement a Repository Test Double


The second implementation of the repository interface is the implementation that you will use in unit
tests. This implementation uses in-memory data and a keyed collection of objects to function just like an
Entity Framework context, but avoids working with a database.

This code example, shows how to implement a repository class for tests.

Implementing a Fake Repository


class FakeWebStoreContext : IWebStoreContext
{
//This object is a keyed collection we use to mock an
//entity framework context in memory
SetMap _map = new SetMap();
public IQueryable<Product> Products
{
get { return _map.Get<Product>().AsQueryable(); }
set { _map.Use<Product>(value); }
}
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-9

public T Add<T>(T entity) where T : class


{
_map.Get<T>().Add(entity);
return entity;
}
public Product FindProductById(int ID)
{
Product item = (from p in this.Products
where p.ProductID == ID
select p).First();
return item;
}
public T Delete<T>(T entity) where T : class
{
_map.Get<T>().Remove(entity);
return entity;
}
class SetMap : KeyedCollection<Type, object>
{
public HashSet<T> Use<T>(IEnumerable<T> sourceData)
{
var set = new HashSet<T>(sourceData);
if (Contains(typeof(T)))
{
Remove(typeof(T));
}
Add(set);
return set;
}
public HashSet<T> Get<T>() { return (HashSet<T>) this[typeof(T)]; }
protected override Type GetKeyForItem(object item)
{
return item.GetType().GetGenericArguments().Single();
}
}
}

Using the Test Double to Test a Controller


Now that you have implemented a test double class for the repository, you can use it to test a controller
in a unit test. In the arrange phase of the test, create a new object from the test double class and pass it to
the controller constructor. The controller uses this object for its Entity Framework context during the test
act phase. This enables you to check results.

This code example shows how to test a controller action by using a test double for the Entity Framework
context.

Using a Test Double to Test a Controller


[TestMethod]
public void Test_Index_Model_Type()
{
var context = new FakeWebStoreContext();
context.Products = new[] {
new Product(),
new Product (),
new Product ()
}.AsQueryable();
var controller = new ProductController(context);
var result = controller.Index() as ViewResult;
Assert.AreEqual(typeof(List<Product>), result.Model.GetType());
}

Note that the test creates a new instance of the FakeWebStoreContext class and adds three Product
objects to it. In this case, you can run the test without setting any properties on the Product objects. This
MCT USE ONLY. STUDENT USE PROHIBITED
06-10 Testing and Debugging ASP.NET MVC 4 Web Applications

context is passed to the ProductController constructor and the test proceeds to the act and assert
phases.

Question: For what purpose would you use a fake repository when you write unit tests against an MVC
controller?

Specifying the Correct Context


When you use test doubles for unit testing MVC
controllers, you must ensure that the controller
uses the correct context in two different scenarios:
unit tests and at all other times. If you use the test
double production code, for example, no data will
be stored in the database. If you use the actual
Entity Framework context for unit testing, all the
tests will fail. The following table gives further
details:

Scenario Context

In unit tests In a unit test, you should test code without relying on underlying
infrastructure such as databases. You can use a test double class that
behaves like an Entity Framework context, but uses in-memory data.

At other times When you are not unit testing code, you use an actual Entity
Framework context in your controller code, which interacts with a
database.

Using Constructors to Specify a Repository


You can specify the correct repository to use in a test by using controller constructors. During a unit test,
call a controller constructor that takes an instance of the repository interface as a parameter. At other
times, the MVC controller factory will call the controller constructor without any parameters.

This code example shows the constructors in an MVC controller that you can use for production and unit
testing.

Using Constructors to Specify Repositories


public class ProductController : Controller
{
private IWebStoreContext context;
//The parameterless version of the constructor is used by the MVC controller factory
public ProductController()
{
//Instantiate an actual Entity Framework context
context = new WebStoreContext();
}
//This constructor is used by unit tests. They pass a test double context
public ProductController(IWebStoreContext Context)
{
//Use the context passed to the constructor
context = Context;
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-11

}
//Add action methods here
}

Using Inversion of Control Containers


You can also ensure that the correct repository class is used in each context by using an Inversion of
Control (IoC) container. An IoC container is a framework you can add to your application that instantiates
classes. You must configure the IoC container so that it can identify the kind of object to create when the
application expects a specific interface. For example, when code is used to create a new instance of the
IWebStoreContext interface, the IoC container checks its configuration and finds that it should
instantiate the WebStoreContext class, which is an Entity Framework context that implements the
IWebStoreContext interface.

Note: IoC frameworks are also known as Dependency Injection (DI) frameworks because
they inject classes that implement the correct interfaces into class constructors.

By using an IoC container, you avoid the need to create two constructors for controller classes. During
tests, you can instantiate the test double and pass it to the controller constructor. At other times, the IoC
container instantiates the actual Entity Framework context and passes it to the controller constructor.

Note: There are many IoC containers designed for use with ASP.NET MVC web
applications. These replace certain components of the MVC framework to support IoC. For
example, they replace the default controller factory with a factory that instantiates controllers
with the right context class. Many IoC containers are available in the NuGet package library, such
as NinJect and StructureMap.

Question: Which approach is more loosely-coupled: using constructors to specify the context or using IoC
containers to specify the context?

Demonstration: How to Run Unit Tests


In this demonstration, you will see how to add a new test project to your ASP.NET MVC 4 solution, and
how to create and run a simple unit test against the home page controller.

Demonstration Steps
1. Add a new test project to the OperasWebSite.sln solution by using the following information:

o Project Template: Unit Test Project

o Name: OperasWebSiteTests.
2. Add a reference to the OperasWebSite project in the OperasWebSiteTests project.

3. Add a reference to the System.Web.Mvc assembly in the OperasWebSiteTests project by using the
following information:

o MVC version: 4.0.0.0

4. Rename the UnitTest1 class as HomeControllerTests.

5. Rename the TestMethod1 method as Test_Index_Return_View.


6. Add using statements for the following namespaces:
MCT USE ONLY. STUDENT USE PROHIBITED
06-12 Testing and Debugging ASP.NET MVC 4 Web Applications

o System.Web.Mvc

o OperasWebSite.Models

o OperasWebSite.Controllers

7. In the Test_Index_Return_View test, create a new HomeController, call the Index action, and assert
that the name of the result view is WrongName.
8. Run all the tests in the OperasWebSiteTests project. Examine the results and discuss with the
students why the test has failed.

9. Alter the Test_Index_Return_View test method to assert that the name of the result view is Index.
10. Run all the tests in the OperasWebSiteTests project and examine the results.

11. Close the Microsoft Visual Studio application.

Using Mocking Frameworks


When you write a unit test, you must, during the
arrange phase, create test data on which the test
will run. By using a test double or mock object to
supply test data, you can test your code in
isolation from other classes and from the
infrastructure elements such as databases and
network connections on which the application will
rely. You can create test doubles by manually
coding their instantiation, setting their properties,
and populating collections. Such test doubles are
known as manual mock objects.
Alternatively, instead of creating mock objects
manually with your own code, you can use a mocking framework to automate this work. A mocking
framework automatically creates mock object by using the interfaces that you specify. In the case of IoC
containers, there are many mocking frameworks that can be used in testing MVC web applications. You
can find many mocking frameworks using the NuGet package manager.

Note: Mocking frameworks for ASP.NET MVC 4 include Moq, RhinoMocks, and
NSubstitute. All these frameworks are available in NuGet and there are alternatives. Choose the
framework that best suits your testing needs.

Why Use a Mocking Framework?


There are many situations in which mocking frameworks can significantly ease unit testing. Even for
simple tests, mocking frameworks reduce the amount of setup code that you have to develop. After you
become familiar with the mocking framework that you choose, and have learned how to write
arrangement code for it, you will begin to save time. In more complex situations, such as the following,
mocking frameworks have great advantages:

o Creating multiple mock objects of a single type: You should try to keep each unit test as simple as
possible but inevitably some tests require many mock objects to work with. For example, if you
are testing a method that underlies paging functionality in your web application, you must create
enough mock objects to populate multiple pages. Mocking frameworks can automate the
creation of multiple mock objects of the same type.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-13

o Mocking objects with multiple interfaces: In some tests, where there are several dependencies
between classes of different types, you must create many mock objects of different classes. In
such situations it is tedious to manually code many mock objects. Mocking frameworks can help
by automatically generating the objects from the interfaces that you specify.

In each unit test, you are interested in a small and specific area of functionality. Some properties and
methods of an interface will be crucial to your test, while others will be irrelevant. A mocking framework
enables you to specify irrelevant properties and methods in a given test. When the framework creates a
mock object for your test, it creates stubs for the irrelevant properties and methods. A stub is a simple
implementation with little code. In this way, the mocking framework frees you from having to implement
all the requirements of the interface laboriously.

Question: What is the difference between an IoC container and a mocking framework?
MCT USE ONLY. STUDENT USE PROHIBITED
06-14 Testing and Debugging ASP.NET MVC 4 Web Applications

Lesson 2
Implementing an Exception Handling Strategy
Unexpected events are likely to occur from time to time in any complex system including MVC web
applications. Occasionally, such unexpected runtime events cause an error. When this happens, ASP.NET
or the .NET framework generates an exception, which is an object that you can use to diagnose and
resolve the error. The exception contains information that you can use to diagnose the problem.
Exceptions that are not handled in your code will cause the web application to halt and an error message
to be displayed to the user. As a web developer, you need to know how to detect, handle, and raise
exceptions and identify the cause of the problem. Visual Studio provides a broad range of tools for
debugging exceptions and improving the robustness of your code. You can also log exceptions to
databases and other stores and use the .NET Framework code contracts to reduce the frequency of
exceptions.

Lesson Objectives
After completing this lesson, you will be able to:

o Explain how to raise and catch exceptions.


o Explain how to configure exception handling by using Web.config.

o Explain how to trace exceptions by using the IntelliTrace tool.

o Describe how to log exceptions.


o Describe the health monitoring options for an MVC 4 web application.

Raising and Catching Exceptions


An error is an unexpected runtime event that
prevents an application from completing an
operation. When a line of code causes an error,
ASP.NET or the common language runtime
creates an exception. This exception is an object
of a class that inherits from the System.Exception
base class. There are many exception classes.
Often the object class identifies what went wrong.
For example, if there is an
ArgumentNullException, it indicates that a null
value was sent to a method that does not accept a
null value as an argument.

Unhandled Exceptions
When an exception is not explicitly handled by an application, the application stops and the user sees an
error message. In ASP.NET MVC applications, this error message is in the form of a web page. You can
override ASP.NET default error pages to display your own error information to users.

If an unhandled exception arises while you are debugging the application in Visual Studio, execution
breaks on the line that generated the exception. You can use the Visual Studio debugging tools to
investigate what went wrong, isolate the problem, and debug your code.

Sometimes, you may also want to raise your own exceptions as a form of communication between parts
of your application. For example, consider an application that displays products on a website. In the
repository, you implement a FindProduct method that returns the product with a name that matches the
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-15

search string passed as an attribute. If the FindProduct method does not locate a product with the
specified name you may wish to raise an exception. This approach enables code in the controller, which
calls the FindProduct method, to handle the event that no products are found. You can use the throw
keyword to raise errors in C# code.

Catching Errors with Try/Catch Blocks


The most familiar way to catch an exception, which works in any .NET Framework code, is to use the
try/catch block. Code in the try block is run. If any of that code generates an exception, the type of
exception is checked against the type declared in the catch block. If the type matches, or is of a type
derived from the type declared in the catch block, the code in the catch block runs. You can use the code
in the catch block to obtain information about what went wrong and resolve the error condition.

This code example catches any ArgumentNullException objects.

A Simple Try/Catch Block


try
{
Product product = FindProductFromComment(comment);
}
catch (ArgumentNullException ex)
{
Console.WriteLine("The comment object was null. Error message: " + ex.Message);
}

In MVC controllers, there are two methods to catch exceptions, the OnException method and the
[HandleError] annotation.

Catching Controller Exceptions in OnException


The OnException method is defined on the base Controller class, so you can override it in any MVC
controller. When you override the OnException method in a custom controller, MVC runs the method
whenever an exception arises and is not handled in a try/catch block. By using this approach you can
handle errors anywhere in your controller without adding many try/catch blocks throughout the code.
In the OnException method, you can catch specific types of exceptions by checking the
ExceptionContext.Exception property. Your code must set the context.Result property to display a
view to the user. If you do not set this property, the browser displays a blank webpage to the user.
In this code example, the OnException method is used to catch InvalidOperationException objects.

Using OnException to Catch Controller Errors


protected override void OnException(ExceptionContext context)
{
// Catch invalid operation exception
if (filterContext.Exception is InvalidOperationException)
{
var model = new HandleErrorInfo(context.Exception, controllerName, actionName);
var result = new ViewResult
{
ViewName = "Error",
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
//Save pass the current Temp Data to the Error view, because it often
contains
//diagnostic information
TempData = context.Controller.TempData
};
context.Result = result;
}
}
MCT USE ONLY. STUDENT USE PROHIBITED
06-16 Testing and Debugging ASP.NET MVC 4 Web Applications

Catching Controller Exceptions with HandleError


Using the [HandleError] annotation, you can use an attribute on individual action methods or on the
controller class itself, in which case the the [HandleError] annotation catches errors in any action. When
you use the [HandleError] annotation without properties, any exceptions are caught and sent to an MVC
view called Error.cshtml. This default error view should be created in the /View/Shared folder.

You can use two properties to control the behavior of [HandleError]:

o ExceptionType: By default, the [HandleError] annotation catches all exceptions. If you specify a
more specific type with the ExceptionType property, only exceptions of that specific type will be
caught.

o View: By default, the [HandleError] annotation forward users to the Error.cshtml view. You can
specify another view file by using the View property. By specifying both the ExceptionType and
View properties, you can divert exceptions of specific types to views designed specifically to
display them.

In this code example, the first [HandleError] annotation forwards NotImplementedException errors to
the NotImplemented view. All other errors are forwarded to the default error view.

Using the HandleError Annotation


[HandleError(ExceptionType=typeof(NotImplementedException), View="NotImplemented")]
[HandleError]
public ActionResult Index()
{
//Place action code here
}

Question: You are using the [HandleError] annotation to catch exceptions in your web application.
However, you realize that [HandleError] catches all exceptions, making it difficult for you to isolate a
specific issue. What can you do to narrow down the exceptions that the [HandleError] annotation is
catching?

Configuring Exception Handling


ASP.NET applications display a default error page
to users when an unhandled exception occurs in
your MVC web application. There are two reasons
why you may want to change the default error
page:
o You want to display a branded and styled
error page that has the same layout as
other pages in the application and
includes the company logo.

o You want to avoid drawing attention to


the technology that powers the site. By
letting malicious users know that your
site runs ASP.NET MVC, you may encourage them to attack it using methods that have worked in
the past.

To configure a custom error page for your application, use the top-level web.config file. The custom error
page will be displayed when an exception in not handled by any try catch block or [HanldeError]
attribute.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-17

Custom Error Modes


To configure custom errors, add the <customErrors> element to the web.config file. This element must
be a child of the <system.web> element. Use the following attributes:

 mode: The mode attribute can be set to one of three values:

o Off: Unhandled errors are displayed on the default ASP.NET error page.

o On: Unhandled errors are displayed in the page you specify in the defaultRedirect attribute.

o RemoteOnly: In this mode, unhandled errors are displayed differently for browsers on remote
computers and browsers on the web server. In a production site, all browsers are remote and
errors are displayed in the page that you specify in the defaultRedirect attribute. In
development situations, the developer often browses to the site on the local computer. When an
error occurs, the developer is directed to the default ASP.NET error page, which includes
debugging information such as the stack trace. Use the RemoteOnly mode on development
computers.

 defaultRedirect: This attribute sets the name of the view that will be used to display unhandled
exceptions. When you create this view, place it in the Views/Shared folder.

The code in this example enables custom errors and specifies that unhandled exceptions are forwarded to
the customError view.

Configuring Custom Errors


<system.web>
<customErrors mode="On" defaultRedirect="CustomError" />
</system.web>

HTTP error pages


Some Hypertext Transfer Protocol-level (HTTP) errors cannot be caught by custom MVC error views as
specified in web.config or the [HandleError] annotation. For such errors, you can use the <error>
element in web.config, as a child element of the <customErrors> element.

In this example, the <error> element is used to specific HTML files to respond to HTTP 500 errors.

Catching HTTP Error Codes


<customErrors mode="On" defaultRedirect="CustomError" >
<error statusCode="500" redirect="~/Error/Error500.html" />
</customErrors>

Question: You have switched off custom errors in the web.config file. When you run your application
without debugging, an exception is thrown in a try/catch block. What page displays the exception to the
user?
MCT USE ONLY. STUDENT USE PROHIBITED
06-18 Testing and Debugging ASP.NET MVC 4 Web Applications

Using Visual Studio IntelliTrace in MVC


When something goes wrong in your web
application, whether it happens during
development or in production, you need
information about the state of the application in
order to diagnose the problem. Sometimes, a
small amount of information is sufficient. For
example, you may be able to see from the
Uniform Resource Locator (URL) query string that
a link was formulated wrongly. Often, however,
problems arise in more obscure places and you
need as much information as possible to perform
a diagnosis. IntelliTrace is a feature of Visual
Studio that assists debugging by presenting as much information as possible. IntelliTrace was introduced
with Microsoft Visual Studio 2010 Ultimate.

The Traditional Debugging Workflow


When developers debug malfunctioning code, they run the application in Visual Studio debugging mode.
If an unhandled exception occurs, Visual Studio breaks execution and displays details such as the
exception class and the exception message. You can use the Locals, Call Stack, and Error List windows to
determine the values of variables and properties that may have given rise to the exception. All these
pieces of information are presented as they were when the error occurred.

If a problem originated before the error occurred, you must halt debugging, set a break point, and then
restart debugging. Again, Visual Studio halts execution, but this time it is at the break point. You can use
the same windows to gain information and step through code line by line to see how values change and
diagnose the error.

Two problems can occur in this workflow:

o If you set the breakpoint too late, you will miss the origin of the error. You must set another
break point and restart debugging.

o If you are stepping through many lines of code, you can miss the error by selecting
Debug|StepInto or pressing the F11 shortcut too many times. This is easy to do when there are
hundreds of lines of code to step through.

Debugging With IntelliTrace


IntelliTrace improves this workflow and addresses the two problems that arise during the traditional
debugging workflow by recording a timeline of code execution. When you run an application in the
debug mode, IntelliTrace records all the events that occur in the background. This can include exceptions,
debugging events such as break points, and other events that you can configure.

As before, when the debugger encounters an unhandled exception or a break point, Visual Studio halts
execution and displays the details of any exception. However, the developer can now see all the recorded
events in the IntelliTrace pane. The current event appears at the bottom of the list and is named Live
Event. You can see all the previous events in the list and can click on them for more details. In this way,
you can investigate what happened before the exception or the break point.

When you click on an earlier event in the IntelliTrace pane, the Locals, Call Stack, and Error List windows
display the state of the application at the moment of the earlier event. The line of code that corresponds
to the selected event is highlighted in orange. Because of this highlighting, two problems that arise during
the traditional debugging workflow are eased, namely, missing the origin of error and stepping too far
through the code. Because the relevant code is highlighted, if you miss the origin of the error or step too
far through code, you can get back to earlier events and investigate values.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-19

To help you locate the correct event in the IntelliTrace window, you can filter by event category or
execution thread or use the search option.

IntelliTrace Configuration Options


To configure IntelliTrace, click DEBUG, click IntelliTrace, and then click Open IntelliTrace Settings. You
can enable or disable IntelliTrace event recording and specify a location and maximum size for the
IntelliTrace files.

Most of the configuration options concern the amount of data to record in IntelliTrace files. The default
settings have a mimimal effect on performance but, if those are insufficient to diagnose a problem, you
can choose to log more information to target the problem. This may compromise performance but
remember that IntelliTrace information is only recorded during debugging. Your settings will not degrade
performance after the application is deployed to a web server. Under General, you can set IntelliTrace to
record IntelliTrace event only or both IntelliTrace events and call information, which includes all calls to
methods and classes. You can configure what is included in IntelliTrace events by using the IntelliTrace
Events list. For example, by default, ASP.NET events such as error pages, page post backs, redirects are
included for MVC applications.

Saving and Opening IntelliTrace Files


IntelliTrace automatically saves events in an IntelliTrace file when you enter debug mode. The file is
automatically deleted when you close Visual Studio to ensure that IntelliTrace files do not over-use disk
space. Note that this means that you the IntelliTrace pane includes events from the current debugging
session and any previous debugging sessions run since you started Visual Studio. However, these files are
temporary.

You can avoid losing an IntelliTrace file when you close Visual Studio by saving it for later analysis. To do
this, click DEBUG, click IntelliTrace, and then click Save IntelliTrace Session. The file is saved with an
.iTrace extension. If you double-click on an .iTrace file, Visual Studio displays the threads, exceptions,
modules, and other information that the file contains.

Additional Reading: For more information on IntelliTrace and how to use it, see
http://msdn.microsoft.com/en-us/magazine/ee336126.aspx

Question: You are looking at IntelliTrace events and suspect that the error originated in a class written by
a colleague who works in another country. How can you help the colleague to see the events that you can
see in IntelliTrace after you close Visual Studio?

Logging Exceptions
Exceptions that you face during development can
be investigated and debugged by using the Visual
Studio debugging tools including IntelliTrace. In
an ideal situation, no exceptions would arise when
your web application is complete and deployed
for users over the Internet. However, in the real
world, unforeseen circumstances arise resulting in
exceptions. For example, database failures,
network issues, and configuration errors in any
part of the system can cause exceptions.

You have already learned how to present a


branded and informative custom error page to
MCT USE ONLY. STUDENT USE PROHIBITED
06-20 Testing and Debugging ASP.NET MVC 4 Web Applications

users when exceptions occur. It is also appropriate to log the exceptions in a production web application,
so the administrators and developers can assess the extent of the problem, remove the cause, and
improve the robustness of the code.

Writing Error Logging Code


Before you write code to log exceptions in your web application, consider where exceptions should be
recorded. Generally, web applications log exceptions to an Extensible Markup Language (XML) file. This
approach can be efficient, especially if the text file is kept on a separate drive from the database of the
web application and other critical, commonly accessed files. However, the file size of the XML files can
grow large, making them difficult to analyze.

A common approach is to log errors to tables in a database. These tables can be a part of a single web
application database or you can place them in a dedicated database away from product catalogs, user
profiles, and other information. By logging exceptions to a database, you create a list of exceptions that
can be easily searched and analyzed. You can also build a webpage to present these logged exceptions to
administrators so that they can access the details from a browser.
You should also consider sending emails or other types of messages to administrators and developers
when exceptions occur. This approach ensures that administrators and developers hear of an error very
quickly. It does not require them to review the exceptions logged in the database. Frequently, error
handling code in live applications both logs to a database and send emails to administrators and
developers.

Where to Write Error Logging Code


When you decide where to write code that logs errors, you should consider that errors might arise in
almost any part of your application. You should choose an approach that enables you to write error
logging code once that will run for any exception anywhere in your application.
For example, it is not appropriate to write error logging code in individual try/catch blocks. If you do this,
you will have to create a try/catch block for every procedure in your application and write duplicate
logging code in the catch block.

A more effective approach is as follows:

o Create a custom base controller class for your web application. This class inherits from the
System.Web.Mvc.Controller base class.

o In your custom base controller class, override the OnException() method. Place you error
logging code in this method.

o When you create controllers, inherit from your custom base controller class instead of
System.Web.Mvc.Controller.

In this way, you can write error logging code only once in the overridden OnExcpetion() method. This
catches exceptions from any of the inheriting controllers.

Using Third Party Logging Tools


Because exception logging is a very common functional requirement for web applications, there are many
third party solutions that you can choose if you do not want to write your own logging code. Many of
these are available within Visual Studio from the NuGet package manager.

Perhaps, the most commonly used and widely supported error logging package is Error Logging Modules
and Handlers (ELMAH). ELMAH can be used with any ASP.NET web application and can log exceptions to
XML files or a wide variety of databases including Microsoft SQL Server, Oracle, Microsoft SQL Server
Compact, MySQL, and others. In addition ELMAH includes webpages that you can use to remotely view
exception lists and details and ELMAH can send alert emails to address that you configure.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-21

Question: You want to ensure that developers can review exception details that arise during debugging.
How should you approach error logging code?

Health Monitoring
Since ASP.NET version 2.0, you can use health
monitoring to record what happens in a web
application. Health monitoring can record
exceptions through error logging techniques.
However, health monitoring can also record a
wide range of other events such as application
starts and stops, failed logon attempts,
membership events, and user input validation
errors. Some pieces of these information are
difficult to access from any other source.

Health Monitoring Features


You can customize the category of events that
health monitoring records. Each event is within one of the five following categories:

Event Category Description

Application Lifetime Events Application lifetime events include application starts and stops.

Audit Events Audit events include logons, logoffs, and logon failures.

Error Events Error events incude .NET Framework exceptions and HTTP errors such
as 404 errors.

Request Processing Events Request processing events include events that occur as ASP.NET
controllers, models, and views receive and respond to web page
requests.

Heartbeats Heartbeats are periodic events that health monitoring can raise and
record to confirm that the application is running.

You can also customize the location where health monitoring stores event details by configuring health
monitoring providers. You can choose from the following providers:

Health Monitoring Provider Description

EventLogWebEventProvider EventLogWebEventProvider sends events to the Windows


Event Log. These events appear in the Windows log.

SqlWebEventProvider SqlWebEventProvider sends events to a SQL Server


Database.

WmiWebEventProvider WmiWebEventProvider sends events to Windows


Management Infrastructure client.

SimpleMailWebEventProvider SimpleMailWebEventProvider sends events in an email


sent to an address you specify.

TemplatedMailWebEventProvider TemplatedMailWebEventProvider also sends events in an


email but you can use a template email to format the
MCT USE ONLY. STUDENT USE PROHIBITED
06-22 Testing and Debugging ASP.NET MVC 4 Web Applications

Health Monitoring Provider Description


information.

TraceWebEventProvider TraceWebEventProvider sends events to the ASP.NET


tracing system.

Configuring Health Monitoring


You configure health monitoring by using the <healthMonitoring> element within the <system.web>
element in web.config. You can use elements within <healthMonitoring> to add and configure
providers and define rules that govern how events of different types are recorded.

This code example shows how to configure health monitoring to record application life cycle events to a
SQL Server database

Configuring Health Monitoring


<system.web>
<connectionStrings>
<add name="HealthDB" connectionString="your connection string here" />
</connectionStrings>
<healthMonitoring enabled="true">
<providers>
<add name="sqlProvider"
type="System.Web.Management.SqlWebEventProvider"
connectionStringName="HealthDB"
buffer="false"
bufferMode="Notification" />
</providers>
<rules>
<add name="LifeCycle"
provider="sqlProvider"
eventName="Application Lifetime Events" />
</rules>
</healthMonitoring>
<system.web>

Additional Reading: For full details of all the options you can configure in web.config for health
monitoring, see: http://forums.asp.net/t/1027461.aspx

If you configure health monitoring to use the SqlWebEventProvider, ASP.NET will by default create a
database file called ASPNETDB.MDF in the App_Data folder of the web application. You can alter this
location by using the connection string in web.config. However, health monitoring will only work if certain
data tables are present in the database that you connect to. You can prepare the database with the
correct tables by using the aspnet_regsql.exe tool. This tool is found in the following directory:
%WINDOWS%\Microsoft.NET\Framework\version

The following command prepares a SQL Server database for health monitoring, authenticating as the
current user:

aspnet_regsql.exe –E –S HealthDatabaseServer –d HealthDatabase –A w

Question: After using your recently-developed web application, several users are facing failed logon
attempts. As a result, to track these exceptions, you have decided to set up a monitoring system that will
send an email to a specified email address. Which health monitoring provider will you use to fix this?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-23

Lab: Testing and Debugging ASP.NET MVC 4 Web


Applications
Scenario
The photo sharing application is in the early stages of development. However, frequent errors are
hindering the productivity of the development team. The senior developer advises that you intercept
exceptions and other flaws as early as possible. You have been asked to unit test the PhotoController to
ensure that all scenarios work as expected and to avoid problems later in the web application
development life cycle. You have also been asked to ensure that when critical errors occur, developers can
obtain helpful technical information.

Objectives
After completing this lab, you will be able to:

o Unit test any component of an MVC web application.


o Configure an exception handling strategy for an MVC web application.

o Use Visual Studio debugging tools against a web application.

Lab Setup
Estimated Time: 60 minutes

Virtual Machine: 20486A-SEA-DEV11

User name: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:

 Enable the Allow NuGet to download missing packages during build option in Package Manager.

Exercise 1: Performing Unit Tests


Scenario
In this exercise, you will:

o Create a test project and write the following tests.


 Test_Index_Return_View: This test checks that the Index action returns a view named Index.
 Test_PhotoGallery_Model_Type: This test checks that the _PhotoGallery action passes an
enumerable list of Photo objects to the _PhotoGallery partial view.
 Test_GetImage_Return_Type: This test checks that the GetImage action returns a file and
not a view.
 Test_PhotoGallery_No_Parameter: This test checks that when you call the _PhotoGallery
action without any parameters, the action passes all the photos in the context to the
_PhotoGallery partial view.
 Test_PhotoGallery_Int_Parameter: This test checks that when you call the _PhotoGallery
action with an integer parameter, the action passes the corresponding number of photos to
the _PhotoGallery action.
o Implement a repository.

o Refactor the PhotoController to use a repository.


MCT USE ONLY. STUDENT USE PROHIBITED
06-24 Testing and Debugging ASP.NET MVC 4 Web Applications

o Refactor tests to use a mock repository.

Note: The tests you add to the solution in this exercise will improve the quality of code and prevent
bugs as development proceeds. However, this exercise does not conform to the principles of TDD because
the PhotoController class already exists. In TDD, you would create these and other tests first, and then
create a PhotoController class that passes the tests.

The main tasks for this exercise are as follows:

1. Create a test project.

2. Write the tests.


3. Implement a repository.

4. Refactor the photo controller to use the repository.

5. Refactor the tests to use a mock repository.

6. Add further tests.

 Task 1: Create a test project.


1. Start the virtual machine and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11


o User name: Admin

o Password: Pa$$w0rd

2. Open the PhotoSharingApplication solution from the following location:


o File location: Allfiles (D):\Labfiles\Mod06\Starter\PhotoSharingApplication

3. Add a new project to the solution for unit tests by using the following information:

o Template: Unit Test Project


o Name: PhotoSharingTests

4. Add a reference to the PhotoSharingApplication project in the PhotoSharingTests project.

5. Add a reference to the System.Web.Mvc assembly in the PhotoSharingTests project by using the
following information:

o MVC version: 4.0.0.0

 Task 2: Write the tests.


1. Rename the UnitTest1 class as PhotoControllerTests.

2. Rename the TestMethod1 method as Test_Index_Return_View.

3. Add using statements for the following namespaces:

o System.Collections.Generic

o System.Web.Mvc

o PhotoSharingApplication.Models

o PhotoSharingApplication.Controllers

4. In the Test_Index_Return_View test, create a new PhotoController, call the Index action, and assert
that the name of the result view is Index.

5. Add a new test method by using the following information:


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-25

o Annotation: [TestMethod]

o Scope: public

o Return type: void

o Name: Test_PhotoGallery_Model_Type

o Parameters: None

6. In the Test_PhotoGallery_Model_Type test, create a new PhotoController, call the _PhotoGallery


action, and assert that the type of the result model is List<Photo>.

7. Add a new test method by using the following information:

o Annotation: [TestMethod]

o Scope: public

o Return type: void


o Name: Test_GetImage_Return_Type

o Parameters: None

8. In the Test_GetImage_Return_Type test, create a new PhotoController, call the GetImage action,
and assert that the result type is FileResult.

9. Run all the tests in the PhotoSharingTests project and examine the results.

 Task 3: Implement a repository.


1. Add a new interface called IPhotoSharingContext to the Models folder in the
PhotoSharingApplication project.

2. Set public scope to the new interface.

3. Add the Photos property to the IPhotoSharingContext interface by using the following information:
o Type: IQueryable<Photo>

o Name: Photos

o Access: Read only

4. Add the Comments property to the IPhotoSharingContext interface by using the following
information:

o Type: IQueryable<Comment>

o Name: Comments

o Access: Read only

5. Add the SaveChanges method to the IPhotoSharingContext interface by using the following
information:

o Return type: Integer

o Name: SaveChanges

6. Add the Add method to the IPhotoSharingContext interface by using the following information:

o Return type: T, where T is any class

o Parameter: an instance of T named entity

7. Add the FindPhotoById method to the IPhotoSharingContext interface by using the following
information:
MCT USE ONLY. STUDENT USE PROHIBITED
06-26 Testing and Debugging ASP.NET MVC 4 Web Applications

o Return type: Photo.

o Parameter: an integer named ID

o Add the FindCommentById

8. Add the FindCommentById method to the IPhotoSharingContext interface by using the following
information:
o Return type: Comment

o Parameter: an integer named ID

9. Add the Delete method to the IPhotoSharingContext interface by using the following information:

o Return type: T, where T is any class.

o Parameter: An instance of T named entity.

10. Ensure that the PhotoSharingContext class implements the IPhotoSharingContent interface.
11. In the PhotoSharingContext class, implement the Photos property from the IPhotoSharingContext
interface and return the Photos collection for the get method.

12. In the PhotoSharingContext class, implement the Comments property from the
IPhotoSharingContext interface and return the Comments collection for the get method.

13. In the PhotoSharingContext class, implement the SaveChanges method from the
IPhotoSharingContext interface and return the results of the SaveChanges method.
14. In the PhotoSharingContext class, implement the Add method from the IPhotoSharingContext
interface and return a Set<T> collection with entity added.

15. In the PhotoSharingContext class, implement the FindPhotoById method from the
IPhotoSharingContext interface and return the Photo object with requested ID.

16. In the PhotoSharingContext class, implement the FindCommentById method from the
IPhotoSharingContext interface and return the Comment object with requested ID.
17. In the PhotoSharingContext class, implement the Delete method from the IPhotoSharingContext
interface and return a Set<T> collection with entity removed.

18. Save all the changes and build the project.

 Task 4: Refactor the photo controller to use the repository.


1. In the PhotoController class, change the declaration of the context object so it is an instance of the
IPhotoSharingContext. Do not instantiate the context object.

2. Add a new constructor to the PhotoController class. In the controller, instantiate context to be a
new PhotoSharingContext object.

3. Add a second constructor to the PhotoController class that accepts an IPhotoSharingContext


implementation named Context as a parameter. In the controller, instantiate context to be the
Context object.

4. In the PhotoController Display action, replace the call to context.Photos.Find() with a similar call to
context.FindById<Photo>.

5. In the PhotoController Create action for the POST verb, replace the call to context.Photos.Add()
with a similar call to context.Add<Photo>.

6. In the PhotoController Delete action, replace the call to context.Photos.Find() with a similar call to
context.FindById<Photo>.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-27

7. In the PhotoController DeleteConfirmed action, replace the call to context.Photos.Find() with a


similar call to context.FindById<Photo>

8. In the PhotoController DeleteConfirmed action, replace the call to context.Photos.Remove() with a


similar call to context.Delete<Photo>.

9. In the PhotoController GetImage action, replace the call to context.Photos.Find() with a similar call to
context.FindById<Photo>

10. Run the application with debugging to ensure that the changes are consistent.

 Task 5: Refactor the tests to use a mock repository.


1. Add a new folder called Doubles to the PhotoSharingTests project.

2. Add the FakePhotoSharingContext.cs existing file to the Doubles folder from the following location:

o Allfiles (D):\Labfiles\Mod06\Fake Repository\ FakePhotoSharingContext.cs

Note: This class will be used as a test double for the Entity Framework context.

3. In the PhotoControllerTests.cs file, add using statements for the following namespaces:

o System.Linq

o PhotoSharingTests.Doubles

4. In the Test_Index_Return_View method, create a new instance of the FakePhotoSharingContext


class and pass it to the PhotoController constructor.

5. In the Test_PhotoGallery_Model_Type method, create a new instance of the


FakePhotoSharingContext class, add four new Photo objects to the class, and then pass them to the
PhotoController constructor.

6. In the Test_GetImage_Return_Type method, create a new instance of the


FakePhotoSharingContext class.

7. Add four new Photo objects to the context.Photos collection. Use the following information to add
each new Photo object:

o PhotoID: a unique integer value

o PhotoFile: a new byte array of length 1

o ImageMimeType: image/jpeg

8. Ensure that the new FakePhotoSharingContext object is passed to the PhotoController


constructor.

9. Run all the tests in the PhotoSharingTests project and verify the status of all the tests.

 Task 6: Add further tests.


1. In PhotoControllerTests.cs, add a new test method by using the following information:

o Annotation: [TestMethod]

o Scope: public
o Return type: void

o Name: Test_PhotoGallery_No_Parameter

o Parameters: None
MCT USE ONLY. STUDENT USE PROHIBITED
06-28 Testing and Debugging ASP.NET MVC 4 Web Applications

2. In the Test_PhotoGallery_No_Parameter method, create a new instance of the


FakePhotoSharingContext class, add four new Photo objects to the class, and then pass them to the
PhotoController constructor.

3. Call the _PhotoGallery action and store the PartialViewResult in a variable named result.

4. Cast the result.Model property as an IEnumerable<Photo> collection and then check that the
number of Photos in the collection is 4, which is the same as the number of photos you added to the
fake context.

5. In the PhotoControllerTests.cs code window, copy and paste the entire


Test_PhotoGallery_No_Parameter method. Rename the pasted test method as
Test_PhotoGallery_Int_Parameter.

6. In the Test_Photo_Gallery_Int_Parameter method, ensure that the call to the _PhotoGallery action
passes the integer 3.

7. Assert that the number of Photo objects in the modelPhotos collection is 3, which is the integer you
passed to the _PhotoGallery action.

8. Run all the tests in this PhotoSharingTests project and verify the status of all tests.

Results: After completing this exercise, you will be able to add a set of PhotoController tests defined in
the PhotoSharingTests project to the Photo Sharing application.

Exercise 2: Configuring Exception Handling


Scenario
Now that you have developed unit tests for the Photo Sharing application, you need to configure an
exception handling strategy for the MVC web application. This would ensure that when exceptions occur
in the development phase of the PhotoSharingApplication project, the controller, action, and exception
messages are displayed in a custom MVC error view. You also need to implement a placeholder action for
the SlideShow action in the PhotoController view. This action will be completed during a later iteration of
the project.
The main tasks for this exercise are as follows:

1. Edit Web.config for exception handling.

2. Create a custom error view.

3. Configure errors in the PhotoController class.

4. Raise errors.

 Task 1: Edit Web.config for exception handling.


1. Open the Web.config file in the root level folder of the PhotoSharingApplication project.

2. Add the <customErrors> element to the <system.web> element by using the following
information:

o Parent element: <system.web>

o Element: <customErrors>

o Mode: On

o defaultRedirect: ErrorPage

3. Add the <error> element to the <customErrors> element by using the following information:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-29

o Parent element: <customErrors>

o Element: <error>

o statusCode: 500

o redirect: Error.html

4. Add a new HTML page to the PhotoSharingApplication project by using the following information:

o Template: HTML Page

o Name: Error.html

5. In the Error.html file, set the contents of the TITLE element to Error.
6. Add content to the Error.html file to explain the error to users.

 Task 2: Create a custom error view.


1. Add a new view to the Shared folder by using the following information:

o Name of the view: Error


o View type: Not strongly typed

o Layout or master page: None

2. In the Error.cshtml file, set the content of the TITLE element to Custom Error.
3. Set the @model for the Error.cshtml to System.Web.Mvc.HandleErrorInfo.

4. In the DIV element, render an H1 element by using the following information:

5. Content: MVC Error


6. In the DIV element, render the ControllerName property of the Model object.

7. In the DIV element, render the ActionName property of the Model object.

8. In the DIV element, render the Exception.Message property of the Model object.

9. Save all the changes made to the Error.cshtml file.

 Task 3: Configure errors in the PhotoController class.


1. Modify the PhotoController class to send errors to the Error.cshtml view by using the following
information:

o Class: PhotoController

o Annotation: HandleError

o View: Error

2. Add a new action to the PhotoController class by using the following information:

o Scope: public

o Return type: ActionResult

o Name: SlideShow

o Parameters: None

3. In the new action, throw an exception by using the following information:


o Type: NotImplmentedException

o Message: The SlideShow action is not yet ready


MCT USE ONLY. STUDENT USE PROHIBITED
06-30 Testing and Debugging ASP.NET MVC 4 Web Applications

 Task 4: Raise errors.


1. Start debugging and display Sample Photo 5.

2. In the Internet Explorer window, request the relative URL and view the error details.

o URL: /Photo/Display/malformedID

3. In the Internet Explorer window, request the relative URL.

o URL: /Photo/SlideShow

4. Use the IntelliTrace pane to investigate the exception.

5. Stop debugging and close Visual Studio.

Results: After completing this exercise, you will be able to:

Configure a custom error handling strategy for an MVC application.

Question: When you ran the tests for the first time in Exercise 1, why did
Test_Index_Return_View pass, while Test_GetImage_Return_Type and
Test_PhotoGallery_Model_Type failed?

Question: In Exercise 1, why did all the tests pass during the second run?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 06-31

Module Review and Takeaways


In this module, you have learned various techniques that you can use to eliminate bugs from your
ASP.NET MVC 4 web application. This begins early in the development phase of the project, when you
define unit tests that ensure that each method and class in your application behaves precisely as
designed. Unit tests are automatically rerun as you build the application so you can be sure that methods
and classes that did work are not broken by later coding errors. Exceptions arise in even the most well-
tested applications due to unforeseen circumstances. You have learned how to ensure that exceptions are
handled smoothly, and how error logs are optionally stored for later analysis.

Best Practice: If you are using TDD or Extreme Programming, define each test before you write the
code that implements a requirement. Use the test as a full specification that your code must satisfy. This
requires a full understanding of the design.

     Best Practice: Investigate and choose a mocking framework to help you create test double objects
for use in unit tests. Though it may take time to select the best framework and to learn how to code
mock objects, your time investment will be worth it over the life of the project.

Best Practice: Do not be tempted to skip unit tests when under time pressure. Doing so can
introduce bugs and errors into your system and result in more time being spent debugging.

Common Issues and Troubleshooting Tips


Common Issue Troubleshooting Tip

No information appears in the IntelliTrace


window.

Review Question(s)
Question: You want to ensure that the PhotoController object passes a single Photo object
to the Display view, when a user calls the Search action for an existing photo title. What unit
tests should you create to check this functionality?

Tools
NinJect, StructureMap: These are Inversion of Control (IoC) containers, also known as Dependency
Injection (DI) frameworks. They create non-test implementations of interfaces in your web application.

Moq, RhinoMocks, NSubstitute: These are mocking frameworks. They automate the creation of test
doubles for unit tests.

IntelliTrace: This is a part of Visual Studio that displays application state at the point of an exception or
break and at earlier times.

Health Monitoring: This part of ASP.NET can store health events in a database, log, or other locations for
later analysis.

ELMAH: This exception logging tool can store exceptions in database tables, XML files, and elsewhere and
enable administrators to view exception details on a webpage.
MCT USE ONLY. STUDENT USE PROHIBITED
06-32 Testing and Debugging ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
07-1

Module07
Structuring ASP.NET MVC 4 Web Applications
Contents:
Module Overview 07-1 

Lesson 1: Analyzing Information Architecture 07-2 

Lesson 2: Configuring Routes 07-8 

Lesson 3: Creating a Navigation Structure 07-18 

Lab: Structuring ASP.NET MVC 4 Web Applications 07-24 

Module Review and Takeaways 07-30 

Module Overview
An MVC web application can grow large and complex and the application can contain many types of
information that relate to each other in complicated ways. A key challenge for such web applications is to
ensure that users can easily locate the information that interests them. Although a search tool is helpful,
you should design your web application in such a way that users can navigate to the page they need
through a short sequence of links. At each level, users must know what link to click next. The URLs that
users see in the address bar must be readable and understandable. You need to understand how to
present the model classes you have designed in a way that is logical to the user. You also need to ensure
that Internet search engines can crawl and analyze your web application structure, so that your
application content appears close to the top of the search engine results. You can achieve these goals by
learning about ASP.NET routing and navigation

Objectives
After completing this module, you will be able to:

o Plan suitable information architecture for a given set of functional requirements.

o Add routes to the ASP.NET routing engine and ensure that URLs are user-friendly throughout an
MVC web application.

o Add navigation controls to an application which enable users to locate data rapidly.
MCT USE ONLY. STUDENT USE PROHIBITED
07-2 Structuring ASP.NET MVC 4 Web Applications

Lesson 1
Analyzing Information Architecture
When you analyze use cases and user stories to plan a model for your web application, you must
determine the kinds of objects that your web application will manage. For example, if your web
application provides technical documentation for a product, you can plan model classes such as
installation guides, user guides, FAQs, FAQ answers, categories, comments, and other model classes. The
model imposes a logical structure of objects on the web application. However, you must also consider
how users expect information to be structured. Users may expect a hierarchical structure, in which FAQs,
FAQ answers, and comments are presented under the product they relate to. When you create a complex
MVC web application you need to know how to present model classes in a logical manner, so that your
web application is user-friendly.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe information architecture.

o Plan information architecture in an ASP.NET MVC 4 web application.


o Describe search engine optimization and its importance for web developers.

What Is Information Architecture?


When your web application manages information
that is complex and multi-faceted, there are
possibilities of presenting objects in a confusing
way. Unless you think carefully about the way
users expect information to be structured and
how they expect to navigate to useful content,
you may unintentionally create an unusable web
application. During development, when you work
with a limited amount of data, this confusion may
not become apparent. Then, when real-world data
is added to your database at production
deployment time, it becomes clear that the web
application is confusing. You can avoid this eventuality by planning the information architecture.

Information architecture is a logical structure for the objects your web application manages. You should
design such architecture in a way that users can find content quickly without understanding any technical
aspects of your web application. For example, users should be able to locate a technical answer about
their product without understanding the database structure or the classes that constitute the model.

Example Scenario: A Technical Documentation Website


To understand the need for information architecture, consider a web application that presents technical
information about a company’s products for customers and engineers. In this example, the company
manufactures domestic heating furnaces, which are installed in homes by qualified heating engineers. The
web application is intended to enable customers to locate instructions, hints, and tips. This web
application is also intended to enable engineers to obtain the technical documentation on installing and
servicing furnaces.

The development team has identified the following simple user stories:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-3

o Customers have a certain problem with their boilers. They want to find a specific FAQ answer that
solves the problem. They know the boiler product name and the fuel, but not the product
number. They visit the web application and navigate to the boiler name. They click the FAQ for
their boiler name and locate the answer that they need.

o Engineers need the latest installation manual for a boiler. They know the boiler product number,
product name, and fuel type. They visit the site and navigate to the boiler name. They click the
Manuals link and locate the installation manual.

o Engineers have web applications, which they want to link to a specific boiler name. You want to
ensure that the URL is simple and readable for customers and engineers.

You have already planned the following model classes:

o Furnace: Each furnace object is a product manufactured and sold by the company.

o Category: Categories organize the furnaces by type. For example, you can have categories such as
oil-fired, gas-fired, and solid fuel.

o FAQ Question: Each FAQ relates to a single furnace. Each furnace can have many questions. The
class includes both Question and Answer properties.

o Installation Manual: Each furnace has a single installation manual in the form of a PDF document.

o User Manual: Each furnace has a single user manual in the form of a PDF document.

Planning a Logical Hierarchy


You can see from the user stories that FAQ and manuals are both accessed by navigating to the relevant
product first. You can also see that the company has different products, and both customers and
engineers know the fuel type for a particular furnace. Therefore, you can organize furnaces in categories
by fuel type. The following shows a logical hierarchy of objects, which helps both the customers and
engineers find the information they need by clicking through each level:
 Category

o Furnace

 FAQ

 User Manual

 Installation Manual

Presenting a Hierarchy in Navigation Controls


The information architecture you design should be presented on webpages in the form of navigation
controls. Common approaches to such controls include:

o Site Menus: Most websites have a main menu that presents the main areas of content. For simple
web applications, the main menu may include a small number of static links. For larger web
applications, when users click on a site menu link, a submenu appears.

o Tree Views: A tree view is a menu that shows several levels of information hierarchy. Usually, users
can expand or collapse objects at each level, to locate the content they require. Tree views are
useful for presenting complex hierarchies in navigable structures.

o Breadcrumb Trails: A breadcrumb trail is a navigation control that shows the user where they are
in the web application. Usually a breadcrumb trail shows the current pages and all the parent
pages in the hierarchy, with the home page as the top level. Breadcrumb trails enable you to
understand how a page fits in with the information architecture shown in menus and tree views.
MCT USE ONLY. STUDENT USE PROHIBITED
07-4 Structuring ASP.NET MVC 4 Web Applications

The types of navigation controls you build in your web application depend on how you expect users to
find information.

Presenting a Hierarchy in URLs


You can increase the usability of your web application by reflecting the information architecture in the
URLs, which the users see in the address bar of the web browser. In many web applications, URLs often
include long and inscrutable information such as Globally Unique Identifiers (GUIDs) and long query
strings with many parameters. Such URLs prevent users from manually formulating the address to an item,
and these URLs are difficult to link to a page on your web application. Instead, URLs can be plain and
comprehensible, to help users browse through your content.

In MVC web applications, the default configuration is simple, but it is based on controllers, actions, and
parameters. Following are some example URLs that follow this default pattern:
o http://site/Furnace/Details/23: This URL links to the Furnace controller and the Details action, and
it displays the furnace with the ID 23.

o http://site/FAQQuestion/Details/234: This URL links to the FAQQuestion controller and the Details
action, and it displays the FAQ with the ID 234.

o http://site/InstallationManual/Details/3654: This URL links to the InstallationManual controller


and the Details action, and it displays the manual with the ID 3654.
As you can see, the web application user is required to understand how controllers, actions, and action
parameters work, to formulate URLs themselves. Instead, users can use URLs that are easier to understand,
such as the following, because they reflect the information hierarchy:
o http://site/OilFired/HotBurner2000: This URL links to a Furnace by specifying the fuel category
and the product name. Customers and engineers can understand these values.

o http://site/OilFired/HotBurner2000/HowToRestartMyFurnace: This URL links to an FAQ by


specifying the furnace name the question relates to.

o http://site/OilFired/HotBurner2000/: This URL links to the Installation Manual by specifying the


furnace name the manual relates to.
As you can see, these URLs are easy for customers and engineers to understand, because the URLs are
based on a logical hierarchy and the information that the users already have. You can control the URLs
that your ASP.NET web application uses, by configuring the ASP.NET routing engine.

Question: Why may it be difficult for users to understand URLs based on controllers, actions, and
parameters?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-5

Discussion: The Photo Sharing Information Architecture


Adventure Works is building an MVC web
application that enables users to upload and share
photos of their mountain biking equipment and
rides. You have implemented the following model
classes:

o Photo
o Comment

o User

You are intending to complete the following


functionality in the photo sharing application:

o Home Page: The home page for the application. The latest photos are displayed at thumbnail
size.

o All Photos: A list of all photos in the application at thumbnail size.

o Slideshow: This functionality enables the photos to be displayed at a large size, one-by-one. Each
photo is replaced by the next in the series after 20 seconds.

o Single Photo Display: This page displays one photo at full size. Full metadata including the photo
description is included, and all the comments that relate to the current photo are displayed. Users
can add new comments to the photo.
o Comments: This functionality enables users to view each comment that relates to a single photo.
Each photo can have many comments.

o Logon: This page collects a username and password from a user to authenticate with the web
application.

o About Adventure Works: This page displays information on Adventure Works and links to the
main Adventure Works web application, where users can purchase mountain biking products.
Discuss the following questions with the class:

o How will users locate photos and comments that interest them?

o How will this process change as more photos are added to the system?

o How will users expect information to be organized in the photo sharing application?

o What navigation controls will help users find the right page quickly?

o How will users expect URLs to work in the photo sharing application?
MCT USE ONLY. STUDENT USE PROHIBITED
07-6 Structuring ASP.NET MVC 4 Web Applications

What Is Search Engine Optimization?


Most users find web applications by using search
engines. Users tend to visit the links that appear at
the top of search engine results more frequently
than those lower down and those on the second
page of results. For this reason, website
administrators and developers try to ensure their
web application appears high in search engine
results, by using a process known as Search Engine
Optimization (SEO). SEO ensures that more people
visit your web application.

Search engines examine the content of your web


application, by crawling it with a program called a
web bot. If you understand the priorities that web bots and search engine indexes use to order search
results, you can create a web application that conforms to those priorities and thereby appears high in
search engine results.

SEO Best Practices


Various search engines have different web bots with different algorithms to prioritize results. The
complete details of these algorithms are not usually published. However if you adopt the following best
practices, your site has a good chance of appearing high in search results:

o Ensure that each webpage in your web application has a meaningful <title> element in the
<head> section of the HTML.
o Ensure that you include a <meta name="keywords"> tag in the <head> element of each page.
The content attribute of this element should include keywords that describe the content of the
page accurately.
o Ensure that you include a <meta name="description"> tag in the <head> element of each page.
The content attribute of this element should include a sentence that describes the content of the
page accurately.

o Ensure that the <title> element and the <meta> elements are different for each page in your
web application.

o Choose a domain name that includes one or more of your most important keywords.

o Ensure that keywords appear in the <h1>, <h2>, or <h3> elements of your webpage.

o Ensure that navigation controls enable web bots to crawl your entire web application. For
example, you may have content in your site that users can only find with the search tool, not by
clicking through links. As web bots cannot use search tools, this content will not be indexed.

o Ensure that URLs do not include GUIDs or long query text.

SEO and Web Application Structure


Information architecture is a subject that is closely related to SEO. This is because both information
architecture and SEO are relevant to the structure, hierarchy, and accessibility of the objects in your web
application. Users click links in menus to navigate to the pages that interest them. Web bots use the same
links to navigate the web application and crawl its content. Users prefer URLs without GUIDs and long
query text because they are meaningful. Web bots often ignore links with GUIDs and long query text in
them. In addition, when keywords appear in URLs, web bots prioritize a webpage in search results.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-7

As an ASP.NET MVC developer, you must understand SEO principles and use them whenever you write
code, to ensure that you do not damage the search engine positioning of your web application. Views are
critical to SEO because they render <meta> tags, and <title> elements. Routes and the configuration of
the routing engine are also critical, because, by using routes, you can control the URLs that your web
application generates.

Question: A developer creates a partial view that contains <meta> tags and <title> tags. The developer
uses this partial view on every page in the web application. Do these actions conform to SEO best
practices?
MCT USE ONLY. STUDENT USE PROHIBITED
07-8 Structuring ASP.NET MVC 4 Web Applications

Lesson 2
Configuring Routes
ASP.NET enables developers to control the URLs that a web application uses, to link the URLs and the
content by configuring routes. A route is an object that parses a requested URL, and it determines the
controller and action, to which the request must be forwarded. Such routes are called incoming routes.
HTML helpers also use routes when they formulate links to controllers and actions. Such routes are called
outgoing routes. You need to know how to write code that adds a new route to your application. You also
need to know how the routing engine interprets a route so that requests go to the appropriate controllers
and users see meaningful URLs.

Lesson Objectives
After completing this module, you will be able to:

o Describe the features of the ASP.NET routing engine.


o Describe the benefits of adding routes to an ASP.NET MVC web application.

o Explain how to add and configure routes.

o Explain how to use routes to pass parameters.


o Add routes to manage URL formulation.

The ASP.NET Routing Engine


Routing governs the way URLs are formulated and
how they map to controllers and actions. Routing
does not operate on the protocol, server, domain,
or port number of a URL but only on the
directories and file name in the relative URL. For
example, in the URL
http://www.adworks.com/photo/display/1, routing
operates on the relative path /photo/display/1.

In ASP.NET, routes are used for two purposes:


o To parse the URLs requested by browsers.
This analysis ensures that requests are
forwarded to the right controllers and
actions. These are called incoming URLs.

o To formulate URLs in webpage links and other elements. When you use helpers such as
Html.ActionLink() and Url.Action() in MVC views, the helpers construct URLs according to the
routes in the routing table. These are called outgoing URLs.

When you create an MVC web application in Visual Studio by using a project template, the application
has a default route. You must understand this default route before you consider adding extra routes to
manipulate URLs in the application.

Note: You can use routes and the routing engine to govern relative URLs in any ASP.NET
4.5 application and not just in those that use the MVC programming model.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-9

The Default route


The default route is simple but logical and works well in many web applications. The default route
examines the first three segments of the URL. Each segment is delimited by a forward slash:

o The first segment is interpreted as the name of the controller. The routing engine forwards the
request to this controller. If a first segment is not specified, the default route forwards the request
to a controller called Home.

o The second segment is interpreted as the name of the action. The routing engine forwards the
request to this action. If a second segment is not specified, the default route forwards the request
to a controller called Index.

o The third segment is interpreted as an ID value, which is passed to the action as a parameter. The
parameter is optional, so if a third segment is not specified, no default value is passed to the
action.

You can see that, when the user requests the URL http://www.adworks.com/photo/display/1, the default
route passes the ID parameter 1 to the Display action of the Photo controller.

The following code shows how the default route is implemented in new ASP.NET MVC 4 applications:

The Default Route


routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);

Custom Routes
Developers add their own custom routes to a web application for two main reasons:
o To make URLs easier for site visitors to understand. A URL such as
http://www.adworks.com/photo/display/1 is logical to the visitor, but it requires some knowledge
to formulate such URLs. In this case, to type the right URL in the address bar of the web browser,
the user must know some information. This information includes the controller name Photo, the
action name Display, and the ID of the photo of interest. If you use Globally Unique Identifiers
(GUIDs) in the database, the ID segment of the URL can be long and difficult to remember.
Ideally, you should consider what users know and create routes that accept that information. In
this example, users may know the title of a photo that they had seen earlier in your application.
You should create a route that can interpret a URL such as
http://www.adworks.com/photo/title/My%20Photo%20Title, and forward the URL to an
appropriate action to display the right photo. Although users usually click links to make requests,
friendly URLs like these make a site easier to use and link to from other sites.

o To improve search engine rankings. Search engines do not prioritize webpages that have GUIDs
or long query text in the URL. Some web bots do not even crawl such links. In addition, some
search engines boost a page’s ranking when one or more of its keywords appears in the URL.
User-friendly URLs are therefore a critical tool in SEO.

Controller Factories and Routes


MVC uses controller factories that help create an instance of a controller class to handle a request. MVC
uses action invokers to call the right action and pass parameters to that action method. Both controller
MCT USE ONLY. STUDENT USE PROHIBITED
07-10 Structuring ASP.NET MVC 4 Web Applications

factories and action invokers refer to the routing table to complete their tasks. Following are steps MVC
conducts when a request is received from a web browser:

1. An MvcHandler object creates a controller factory. The controller factory is the object that
instantiates a controller to respond to the request.

2. The controller factory consults the routing table to determine the right Controller class to use.

3. The controller factory creates a Controller object, and the MvcHandler calls the Execute method in
that controller.

4. The ControllerActionInvoker examines the request URL and consults the routing table to determine
the action in the Controller object to call.

5. The ControllerActionInvoker uses a model binder to determine the values that should be passed to
the action as parameters. The model binder consults the routing table to determine if any segments
of the URL should be passed as parameters. The model binder can also pass parameters from a
posted form, from the URL query text, or from uploaded files.

6. The ControllerActionInvoker runs the action. Often, the action creates a new instance of a model
class, perhaps by querying the database with the parameters that the invoker passed to it. This model
object is passed to a view, to display results to the user.

As you can see, you can use routes to manage the behavior of controller factories, action invokers, and
model binders, because all these objects refer to the routing table. MVC is highly extensible; therefore,
developers can create custom implementations of controller factories, action invokers, and model binders.
However, by using routes you can usually implement the URL functionality that you need with the default
implementations of these classes. You should ensure that routes cannot implement your required
functionality before you plan to customize controller factories, action invokers, or model binders.

Question: A user wants to edit a comment that she created in your MVC application. You have not
created any custom routes. What URL do you think she must request to see the edit view with the right
comment?

Adding and Configuring Routes


Every MVC web application has a RouteTable
object in which routes are stored, in the Routes
properties. You can add routes to the Routes
property, by calling the MapRoute() method.

In the Visual Studio project templates for MVC 4,


a dedicated RouteConfig.cs code file exists in the
App_Start folder. This file includes the
RouteConfig.RegisterRoutes() static method,
where the default route is added to the
RouteTable object. You can add custom routes in
this method. The Global.asax.cs file includes a
call to RouteConfig.RegisterRoutes() in the
Application_Start() method, which means that routes are added to the routing table whenever the MVC
application starts.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-11

Properties of a Route
Before you can add a route, you must understand the properties of a route. This is to ensure that these
properties match URL segments correctly and pass requests and parameters to the right location. The
properties of a route include the following:

o Name: This string property assigns a name to a route. It is not involved in matching or request
forwarding.

o URL: This string property is a URL pattern that is compared to a request, to determine if the route
should be used. You can use segment variables to match a part of the URL. You can specify a
segment variable by using braces. For example, if you specify the URL “photo/{title}” the route
matches any request where the relative URL starts with the string “photo/” and includes one
more segment. The segment variable is “title” and can be used elsewhere in the route.

o Constraints: Sometimes you must place extra constraints on the route to ensure that it matches
only with the appropriate requests. For example, if you want relative URLs in the form “photo/34”
to specify a photo with the ID “34”, you must use a URL property like “photo/{id}”. However,
observe that this URL pattern also matches the relative URL “photo/create”, because it has one
extra segment. For IDs, you must constrain the URL to match only segments comprising digits.
You can do this by adding a constraint to the route. The Constraints property enables you to
specify a regular expression for each segment variable. The route will match a request, only if all
the segment variables match the regular expressions you specify.

o Defaults: This string property can assign default values to the segment variables in the URL
pattern. Default values are used for segment variables, when the request does not specify them.

The Default Route Code


The default route specifies Name, URL, and Defaults properties to obtain controller and action names,
and ID values, from the requested relative URL. By examining these properties, you can understand how
to construct your own routes.

The following code shows the default route in the Visual Studio MVC project templates:

The Default Route in RouteConfig.cs


routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Observe that the URL pattern specifies three segments: {controller}, {action}, and {id}. This means that a
relative URL in the form “photo/display/23” will be sent to the Display action of the PhotoController
class. The third segment will be passed as a parameter to the action method. Therefore, if the Display
action accepts a parameter named id, the value 23 will be passed by MVC.

In the defaults property, the {id} segment is marked as optional. This means that the route still matches a
relative URL, even if no third segment is specified. For example, the route matches the relative URL
“photo/create” and passes the URL to the Create action of the PhotoController class.

The defaults property specifies a default value of “Index” for the {action} segment. This means that the
route still matches a relative URL, even if no second segment is specified. For example, the route matches
the relative URL “photo” and passes the URL to the Index action of the PhotoController class.

Finally the defaults property specifies a default value of “Home” for the {controller} segment. This means
that the route still matches a relative URL, even if no segments are specified, that is if there is no relative
MCT USE ONLY. STUDENT USE PROHIBITED
07-12 Structuring ASP.NET MVC 4 Web Applications

URL. For example, the absolute URL “http://www.adworks.com” matches this route, and the URL is passed
to the Index action of the HomeController class.
As you can see, with the default route in place, developers build the web application’s homepage by
creating a controller named HomeController with an action named Index. This action usually returns a
view called Index from the Views/Home folder.

Creating Custom Routes


You can add custom routes by calling the routes.MapHttpRoute() method, just like the default route.

In the following code example, a constraint is used to ensure that only digits match the {id} segment
variable.

A Custom Route
routes.MapRoute(
name: "PhotoRoute",
url: "photo/{id}",
defaults: new { controller = "Photo", action = "Details" },
constraints: new { id = "[0-9]+" }
);

Observe that the URL pattern in the route matches any relative URL that starts with “photo/” and has one
or more segment. However, in the constraints property, the route specifies a regular expression for the {id}
segment variable. This regular expression matches only if the segment is composed of digits that range
from 1 to 9. If the user requests the relative URL “photo/234”, then this route matches, and the routing
engine forwards the request to the Details action of the PhotoController class. If the user requests the
relative URL “photo/create”, then this route does not match and a subsequent route must be used to
forward the request.

Precedence of Routes
Routes are evaluated by the routing engine in the order with which they are added to the
RouteTable.Routes collection. If a route matches, the routing engine uses it and does not evaluate any
later route in the collection. If a route does not match, the routing engine ignores it and evaluates the
next route in the collection. For this reason, you must add routes to the RouteTable.Routes collection in
the appropriate order. You can add the most specific routes first, such as routes that have no segment
variables and no default values. Routes with constraints should be added before routes without
constraints.

The route named "Default" matches any request, including those with no relative URL. This default route
should be the last that you add to the RouteTable.Routes collection. If the routing engine evaluates this
route, the route is always used. Any routes added after the default route are never used.

Question: A developer has removed all code from the Application_Start() method in Global.asax.cs.
When he runs the application, he receives 404 errors for any request, regardless of the relative URL. Why
does this occur?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-13

Using Routes to Pass Parameters


The routing engine separates the relative URL in a
request into one or more segments. Each forward
slash delimits one segment from the next. If you
want one of the segments to specify the controller
name, you can use the {controller} segment
variable. The controller factory always interprets
this variable as the controller to instantiate.
Alternatively, to fix a route to a single controller,
you can specify a value for the controller variable
in the defaults property. In a similar manner, if
you want one of the segments to specify the
action method, you can use the {action} segment
variable. The action invoker always interprets this variable as the action to call. Alternatively, to fix a route
to a single action, you can specify a value for the action variable in the defaults property.

Segment variables or default values with other names have no special meaning to MVC and are passed to
actions. You can access the values of these variables by using one of two methods: the RouteData.Values
collection or by using model binding to pass values to action parameters.

The RouteData.Values Collection


In the action method, you can access the values of any segment variable by using the RouteData.Values
collection. Consider this example:

o In RouteConfig.cs, you have defined a route with the URL pattern “{controller}/{action}/{title}”.
o The user requests the relative URL “photo/edit/my%20photo”.

The RouteData.Values collection contains the following values, which you can use in the action method:

o RouteData.Value["controller"]: This value has the value “photo”.

o RouteData.Value["action"]: This value has the value “edit”.

o RouteData.Value["title"]: This value has the value “my%20photo”.

Using Model Binding to Obtain Parameters


Consider the following action method:

public void ActionMethod Display (int PhotoID)


{
return View(PhotoID);
}

The default MVC action invoker is a class named ControllerActionInvoker. This invoker uses a class
named DefaultModelBinder to pass the appropriate parameters to actions. This model binder uses the
following logic to pass parameters:

1. The binder examines the definition of the action, to which it must pass parameters. In the example,
the binder determines that the action requires an integer parameter called PhotoID.

2. The binder searches for values in the request, which can be passed as parameters by name. In the
example, the binder searches for integers because the action requires integers. The binder searches
for values in the following locations in sequential order:

a. Form Values: If the user fills out a form and clicks the Submit button, the binder can find
parameters in the Request.Form collection.
MCT USE ONLY. STUDENT USE PROHIBITED
07-14 Structuring ASP.NET MVC 4 Web Applications

b. Route Values: If the matched route includes the segment variable {photoid}, the binder can find
the value for the PhotoID parameter in the RouteData.Values collection. This match is case-
insensitive.

c. Query Strings: If the user request includes named parameters after a question mark, the binder
can find these parameters in the Request.QueryString collection.
d. Files: If the user request includes uploaded files, these files can be used as parameters in an
action. File parameters in actions must use the HttpPostedFileBase type.

Therefore, we can use the default model binder to pass parameters to action strings. If the name of an
action method parameter matches the name of a route segment variable, the model binder passes the
parameter automatically.

Optional Parameters
Sometimes you want requests to match a route, irrespective of whether parameter values are supplied.
For example, you might want to enable users to specify a color for a product.

In the following example, the route added includes an optional color parameter.

A Route with an Optional Parameter


routes.MapRoute(
name: "ProductRoute",
url: "product/{id}/{color}",
defaults: new { color = UrlParameter.Optional }
);

The default values specify that {color} is optional. If the relative URL has three segments, the route
matches, and the third segment is passed to the RouteData.Values["color"] value. If the relative URL has
only two segments, the route matches, because the {color} is optional. In this case, the
RouteData.Values["color"] value does not exist. If the relative URL has only one segment, the route does
not match.

Question: A developer has replaced the default model binder with a custom model binder. Now, several
action methods are throwing exceptions on lines that use action parameters. How can you fix this bug?

Demonstration: How to Add Routes


In this demonstration, you will see how to create URLs by adding routes to the MVC web application. You
will also see how to enable visitors to the Operas web application to browse the operas by title.

Demonstration Steps
1. Start the OperasWebSite project in debug mode.

2. Browse to the opera Cosi Fan Tutte and show that the URL includes the controller, the action, and
the ID.

3. Close the Internet Explorer window.

4. Open the OperaController.cs file.

5. Add new action method to the OperaController by using the following information:

o Scope: public

o Return Type: ActionResult


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-15

o Name: DetailsByTitle

o Parameter: a string named title

6. In the DetailsByTitle action, query the contextDB.Operas collection for the first Opera with
requested title.

7. If your query returns null, return an HTTPNotFound error. Otherwise, pass the Opera to the Details
view.

8. Open the RouteConfig.cs file.

9. Add a new route to the OperasWebSite project by using the following information:

o Name: OperaTitleRoute

o URL: opera/title/{title}

o Default Controller: Opera


o Default Action: DetailsByTitle

10. Save all the changes.

11. Start the web application in debug mode.

12. View the list of operas by using the following relative URL:

o /title/rigoletto

13. Close all open applications.

Unit Tests and Routes


Unit tests are small, isolated methods that check a
specific aspect of your code. As your project
grows in complexity, you can rerun all unit tests to
ensure that the verified functionality remains in a
working state. If your team uses Test Driven
Development (TDD), you can define tests to
specify a functional requirement and then write
code that passes your test. Such approaches
significantly increase the quality of your code, by
highlighting bugs as soon as they arise.

You can use unit tests to ensure that routes in


your MVC web application behave as designed. If
you do such tests, you can prevent many 404 HTTP errors and internal server errors from reaching web
application users. Users generally have a low tolerance for such errors, and if errors arise too often, they
will browse to the websites of your competitors.

Creating a HTTP Context Test Double


In a webpage request, ASP.NET stores details of the request in an HTTP Context object. These details
include the URL that was requested, details of the browser, and other information. To write a unit test for
routes, you must simulate the HTTP Context object by creating a test double class.

The following code shows how to create a fake HTTP context object to use as a test double.
MCT USE ONLY. STUDENT USE PROHIBITED
07-16 Structuring ASP.NET MVC 4 Web Applications

An HTTP Context Test Double


public class FakeHttpContextForRouting : HttpContextBase
{
FakeHttpRequestForRouting _request;
FakeHttpResponseForRouting _response;
//The constructor enables you to specify a request URL and an Application path in
the test
public FakeHttpContextForRouting(string appPath = "/", string requestUrl = "~/")
{
//Create new fake request and response objects
_request = new FakeHttpRequestForRouting(appPath, requestUrl);
_response = new FakeHttpResponseForRouting();
}
//This property returns the fake request
public override HttpRequestBase Request
{
get { return _request; }
}
//This property returns the fake response
public override HttpResponseBase Response
{
get { return _response; }
}
}
//This is the fake request class
public class FakeHttpRequestForRouting : HttpRequestBase
{
string _appPath;
string _requestUrl;
public FakeHttpRequestForRouting(string appPath, string requestUrl)
{
_appPath = appPath;
_requestUrl = requestUrl;
}
public override string ApplicationPath
{
get { return _appPath; }
}
public override string AppRelativeCurrentExecutionFilePath
{
get { return _requestUrl; }
}
public override string PathInfo
{
get { return ""; }
}
public override NameValueCollection ServerVariables
{
get { return new NameValueCollection(); }
}
}
//This is the fake response class
public class FakeHttpResponseForRouting : HttpResponseBase
{
public override string ApplyAppPathModifier(string virtualPath)
{
return virtualPath;
}
}

Writing Routing Tests


After creating a HTTP context test double, you can write unit tests for each route in the routing table.
These unit tests adopt the following general phases:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-17

o Arrange: In the arrange phase of the test, you can create a new HTTP context from your test
double class. You can set the request URL for this object to be the URL you want to test. You can
then create a new route collection and call the RouteConfig.RegisterRoutes() method in your
web application.

o Act: In the act phase, you can test the routes by calling the GetRouteData() method of the route
collection. You can then pass the fake HTTP context to this method.

o Assert: In the assert phase, you can use the RouteData.Values collection to check that the
controller, action, and other values are assigned correctly.

In the following example unit test, the default route is checked. The request URL has only a single
segment and the test asserts that this segment is passed as the controller name.

A Routing Unit Test


[TestMethod]
public void Test_Default_Route_ControllerOnly()
{
//This test checks the default route when only the controller is specified
//Arrange
var context = new FakeHttpContextForRouting(requestUrl: "~/ControllerName");
var routes = new RouteCollection();
MyMVCApplication.RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.AreEqual("ControllerName", routeData.Values["controller"]);
Assert.AreEqual("Index", routeData.Values["action"]);
Assert.AreEqual(UrlParameter.Optional, routeData.Values["id"]);
}

Question: You are writing a unit test to check your web application’s routing table. You want to make
sure that the request is passed to an action method called “Edit”. What line of code would you use in the
Assert phase of the test to check this fact?
MCT USE ONLY. STUDENT USE PROHIBITED
07-18 Structuring ASP.NET MVC 4 Web Applications

Lesson 3
Creating a Navigation Structure
A navigation structure is a logical hierarchy of webpages, which enables users to browse to the
information that interests them. In simple web applications, the design of the navigation structure is also
usually simple. As your web application grows, it becomes difficult to present menus and navigation
controls that show the users clearly what their next click should be, at every step. You need to understand
how to build menus, which help users understand where they are in your site and where to go next. You
can do this by building menus and breadcrumb controls.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the importance of well-designed navigation.


o Build clear menus in a web application.

o Add breadcrumb controls to a web application.

o Build site navigation in a web application.

The Importance of Well-Designed Navigation


The information architecture you design for your
web application is a logical hierarchy of objects.
This hierarchy should be clear and understandable
to the audience of your web application. If your
web application is designed for people with
specialist knowledge, you can implement
information architecture that requires specialist
knowledge. For example, in an engineering web
application, you could base your information
architecture on specialist technologies and
solutions that engineers understand. However,
such information architecture would not be
helpful to the general public because of the engineering technology used.

After designing the information architecture, you must present it in your web application. You can add
routes to ensure that URLs reflect your information architecture. Similarly, you must add navigation
controls to present the information architecture in the body of each webpage. The navigation controls
you build are the primary means by which users browse your web application and locate content. You
should ensure two things with these navigation controls:

o An obvious next step. You should ensure that users can easily decide what link to click, on each
page. To achieve this aim, you can consider use cases. For each use case, you can list what the
user knows when they arrive at your web application. The user should be able to choose the right
link without any specialist or technical knowledge. For example, avoid basing your links on any
kind of technical jargon unless you know that your audience understands it.

o A clear context. In a simple web application with only one or two levels in the information
architecture, users can easily keep track of where they are. In larger web applications with many
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-19

levels, you must provide navigation controls, which show users where they are and how to
navigate back up the hierarchy. Tree views and breadcrumb controls perform this function well.

Best Practice: Base your navigation controls on the information hierarchy, rather than on
the model and model classes, database tables, or other technical structures in your web
application. Users will probably not understand the model classes, controllers, views, and data
storage mechanisms that underlie your web application.

Common Types of Navigation Controls


Remember that most web application visitors have browsed thousands of Internet applications before
they visit your homepage. Visitors are usually familiar with common types of menus used on many web
applications, and you can make use of this knowledge by using the same types of menus in your web
application. Common menu types include the following:
o Top menus. Often placed at the top of the page or immediately beneath the application
branding, top menus link to the main areas of your web application. In some applications, each
item in the top menu has a submenu with links to lower level items and pages.
o Tree Views. A tree view is a hierarchical menu that can display many items in multiple levels in
your web application. By default, a tree view often displays only one or two levels of the
information architecture. Users can expand lower levels to find the content they need. Tree views
enable users to explore multiple levels on a single page. Tree views can also show clearly the
position of the current page, in the hierarchy.

o Breadcrumb Trails. Often presented as a horizontal sequence of links across the top of a
webpage, a breadcrumb trail shows the current page and its parents in all the higher levels of the
hierarchy. Breadcrumb trails show the current position of the user, in the context of the web
application and enable users to navigate to higher, less specific pages.
o Footer menus. The page footer is often not visible when a user arrives on a page. The user must
scroll to the bottom of the page to see the footer. The footer is often used as a location for
important, but not business-critical, links. For example, links to copyright and other legal
information, links to parent companies, and links to site maps are often placed in the footer
menu.

The MVC Site Map Provider


In ASP.NET, one way to rapidly build information architecture and navigation controls to present that
architecture is to use a site map provider. A site map provider is an ASP.NET component that stores the
logical hierarchy of your web application. Navigation controls such as top menus, tree views, and
breadcrumb trails can take their links from this single hierarchy.

The MVC Site Map Provider is a provider designed to work with MVC controllers and actions. It presents
the information architecture that you can configure in a simple XML file. This hierarchy is completely
independent of the model, controllers, and actions in your applications although you can build links to
them. The provider also includes HTML helpers, which you can use in views to render menus, tree views,
and breadcrumb trails.

Note: The MVC Site Map Provider is a component by Maarten Balliauw that is not included
in MVC web applications, by default. You can find it in the NuGet package manager by searching
for “MvcSiteMapProvider”.
MCT USE ONLY. STUDENT USE PROHIBITED
07-20 Structuring ASP.NET MVC 4 Web Applications

Question: Analysis of web logs has shown that visitors to your web application can navigate to low level
pages in your information architecture, quickly and easily. However, they subsequently find other pages
by returning to the home page and navigating the entire hierarchy again from the top, or else they use
the search tool. How can you enable users to navigate to higher levels without starting from the home
page again?

Configuring the MVC Site Map Provider


After installing the MVC Site Map Provider from
the NuGet package manager, you must configure
the provider and set up the logical hierarchy it
should present, before you can render any menus.

Configuring the MVC Site Map Provider in


Web.Config

You should configure the MVC Site Map


Provider, by adding elements to the
web.config file in the root folder of
your MVC web application. When you
install the site map provider, the
NuGet package adds a <siteMap>
element to the <system.web> section
of this file.

The following code example shows how to configure the site map provider:

The <siteMap> Element in Web.config


<siteMap defaultProvider="MvcSiteMapProvider" enabled="true">
<providers>
<clear />
<add name="MvcSiteMapProvider"
type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider"
siteMapFile="~/Mvc.Sitemap"
securityTrimmingEnabled="false"
cacheDuration="5"
enableLocalization="false"
scanAssembliesForSiteMapNodes="false"
includeAssembliesForScan=""
excludeAssembliesForScan=""
attributesToIgnore=""
nodeKeyGenerator="MvcSiteMapProvider.DefaultNodeKeyGenerator,
MvcSiteMapProvider"
controllerTypeResolver="MvcSiteMapProvider.DefaultControllerTypeResolver,
MvcSiteMapProvider"

actionMethodParameterResolver="MvcSiteMapProvider.DefaultActionMethodParameterResolver,
MvcSiteMapProvider"
aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider"
siteMapNodeUrlResolver="MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver,
MvcSiteMapProvider"

siteMapNodeVisibilityProvider="MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider,
MvcSiteMapProvider"

siteMapProviderEventHandler="MvcSiteMapProvider.DefaultSiteMapProviderEventHandler,
MvcSiteMapProvider"
/>
</providers>
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-21

</siteMap>

Note that you can configure the name of the XML file where the site map is stored, enable security
trimming, and configure caching.

Additional Reading: For complete details of the configuration values you can use with the
MVC Site Map Provider, together with other documentation, you can refer to
https://github.com/maartenba/MvcSiteMapProvider/wiki.

Creating the Site Map File

The MVC Site Map Provider is the only site map provider that allows you to specify a controller and action
for every item in the site map. You do this by completing a site map file. By default, the site map file is
named Mvc.sitemap and is stored in the highest level of your web application. The hierarchy consists of
nested <mvcSiteMapNode>elements. This is the hierarchy that the helpers will display in navigation
controls.

The following example shows simple information architecture as described in a MVC site map provider
site map file.

An MVC Site Map File


<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0"
xsi:schemaLocation=
"http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0 MvcSiteMapSchema.xsd"
enableLocalization="true">
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="Products" controller="Product" action="Index"
key="AllProducts">
<mvcSiteMapNode title="Bikes" controller="Category" action="Display">
</mvcSiteMapNode>
</mvcSiteMapNode>
<mvcSiteMapNode title="Latest News" controller="Article" action="DisplayLatest" >
<mvcSiteMapNode title="About Us" controller="Home" action="About" />
</mvcSiteMapNode>
</mvcSiteMap>

Question: You use the MVC Site Map Provider. Your boss notices that an incorrect link appears in the
highest menu and the tree view on every page in your web application. He asks you to fix every page in
the web application. What steps should you take?
MCT USE ONLY. STUDENT USE PROHIBITED
07-22 Structuring ASP.NET MVC 4 Web Applications

Adding Menu Controls


After configuring the site map provider and
creating a site map file, you can add menus, tree
views, and other navigation controls to your views.
The MVC site map provider includes some HTML
helpers to make this easy.

The following example code shows how to render


a main menu in an MVC view, by using the
HTML.MvcSiteMap() helper.

Rendering a Menu Control


@Html.MvcSiteMap().Menu(false, false,
true)

In this example, the first parameter specifies that the menu does not start from the current node. The
menu starts from the top-level node, which is usually the home page of the web application. The second
parameter specifies that the starting node does not display as a child node at the same level as second-
level nodes. The third parameter specifies that the starting node displays in the menu.

You can also use the Html.MvcSiteMap() helper to render a breadcrumb control, as displayed in the
following code:

Rendering a Breadcrumb Trail


@Html.MvcSiteMap().SiteMapPath()

The Html.MvcSiteMap() helper also includes functions that you can use to render node titles and
complete site maps.

Additional Reading: For complete reference documentation that detail the


Html.MvcSiteMap() helper and its functions, you can refer to
https://github.com/maartenba/MvcSiteMapProvider/wiki/HtmlHelper-functions.

Question: What are some of the benefits of using the Html.MvcSiteMap() helper?

Demonstration: How to Build Site Navigation


In this demonstration, you will see how to create menus and breadcrumb trails by using the MVC site map
provider and its HTML helpers.

Demonstration Steps
1. Use the NuGet Packages manager to add the MvcSiteMapProvider package to the
OperasWebSite project.

2. Open the Mvc.sitemap file.

3. Add a new <mvcSiteMapNode> tag to the Mvc.sitemap file by using the following information:

o Parent Node: Home

o Title: All Operas


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-23

o Controller: Opera

o Action: Index

o Key: AllOperas

4. Build the solution.

5. Open the Home Index view.

6. Add a site map menu to the Home Index view by using the following information:

o Helper: Html.MvcSiteMap()

o Method: Menu()

o Start from current node: false

o Starting node in child level: false

o Show starting node: true


7. Add a breadcrumb trail to the Home Index view by using the following information:

o Helper: Html.MvcSiteMap()

o Method: SiteMapPath()
8. Open the Opera Index view.

9. Add a site map menu to the Opera Index view by using the following information:

o Helper: Html.MvcSiteMap()
o Method: Menu()

o Start from current node: false

o Starting node in child level: false


o Show starting node: true

10. Add a breadcrumb trail to the Opera Index view by using the following information:

o Helper: Html.MvcSiteMap()

o Method: SiteMapPath()

11. Start the web site in debugging mode.

12. Use the menu option to browse to All Operas.

13. Use the breadcrumb trail to browse to the home page, and then use the menu to browse to the
About page.

14. Stop debugging and close the Visual Studio application.


MCT USE ONLY. STUDENT USE PROHIBITED
07-24 Structuring ASP.NET MVC 4 Web Applications

Lab: Structuring ASP.NET MVC 4 Web Applications


Scenario
An important design priority for the Photo Sharing application is that the visitors should be able to easily
and logically locate photographs. Additionally, photo galleries and photos need to appear high in search
engine results. To implement these priorities, you have been asked to configure routes that enable the
entry of user-friendly URLs to access photos.

You have been asked to ensure that the URLs of the following forms work to display a photo:

o ~/photo/display/PhotoId: In this form of URL, PhotoId is the database ID of the photo object. This
form of URL already works because it matches the default route.

o ~/photo/PhotoId: In this form of URL, PhotoId is the database ID of the photo object. This is the
logical URL to enter when you know the ID of the photo that you want to access.

o ~/photo/title/PhotoTitle: In this form of URL, PhotoTitle is the title of the photo object. This is the
logical URL to enter when you know the title of the photo that you want to access.

You have also been asked to implement the following navigation controls in the Photo Sharing
application:

o A menu with links to the main site areas.

o A breadcrumb control.

These navigation controls will be added to the menu after the completion of the main site areas.

Objectives
After completing this lab, you will be able to:

o Add routes to the ASP.NET Routing Engine in an ASP.NET MVC application.

o Build navigation controls within ASP.NET views.

Lab Setup
Estimated Time: 40 minutes

Virtual Machine: 20486A-SEA-DEV11

Username: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.
Before starting the lab, you need to perform the following step:

o Enable the Allow NuGet to download missing packages during build option in Package
Manager.

Exercise 1: Using the Routing Engine


Scenario
In this exercise, you will:

o Create unit tests for the routes you wish to create.

o Add routes to the application that satisfy your tests.


o Try out routes by typing URLs in the Internet Explorer Address bar.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-25

This approach conforms to the principles of Test Driven Development (TDD).

The main tasks for this exercise are as follows:

1. Test the routing configuration.

2. Add and test the Photo ID route.

3. Add and test the Photo Title route.

4. Try out the new routes.

 Task 1: Test the routing configuration.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Open the PhotoSharingApplication solution from the following location:


o File location: Allfiles (D):\Labfiles\Mod07\Starter\PhotoSharingApplication

3. Add an existing code file to the Photo Sharing Tests project, which contains test doubles for HTTP
objects by using the following information:

o Destination folder: Doubles

o Source folder: Allfiles (D):\Labfiles\Mod07\Fake Http Classes

o Code file: FakeHttpClasses.cs


4. Add a reference from the Photo Sharing Tests project to the System.Web assembly.

5. Add a new Unit Test item to the PhotoSharingTests project. Name the file as RoutingTests.cs.

6. Add using statements to the RoutingTests.cs file for the following namespaces:
o System.Web.Routing

o System.Web.Mvc

o PhotoSharingTests.Doubles
o PhotoSharingApplication

7. Rename the TestMethod1 test to Test_Default_Route_ControllerOnly.

8. In the Test_Default_Route_ControllerOnly test, create a new var by using the following information:
o Name: context

o Type: FakeHttpContextForRouting

o Request URL: ~/ControllerName

9. Create a new RouteCollection object named routes and pass it to the


RouteConfig.RegisterRoutes() method.

10. Call the routes.GetRouteData() method to run the test by using the following information:

o Return type: RouteData

o Return object name: routeData

o Method: routes.GetRouteData
MCT USE ONLY. STUDENT USE PROHIBITED
07-26 Structuring ASP.NET MVC 4 Web Applications

o Http context object: context.

11. Assert the following facts:

o That routeData is not null.

o That the controller value in routeData is “ControllerName”

o That the action value in routeData is “Index”

12. Add a new test to the RoutingTests class named Test_Photo_Route_With_PhotoID.

13. In the Test_Photo_Route_With_PhotoID() test method, create a new var by using the following
information:

o Name: context

o Type: FakeHttpContextForRouting

o Request URL: ~/photo/2


14. Create a new RouteCollection object named routes and pass it to the
RouteConfig.RegisterRoutes() method.

15. Call the routes.GetRouteData() method to run the test by using the following information:

o Return type: RouteData

o Return object name: routeData

o Method: routes.GetRouteData

o Http context object: context

16. Assert the following facts:

o That routeData is not null.

o That the controller value in routeData is “Photo”

o That the action value in routeData is “Display”

o That the id value in routeData is “2”

17. Add a new test to the RoutingTests class named Test_Photo_Title_Route.

18. In the Test_Photo_Title_Route test method, create a new var by using the following information:

o Name: context

o Type: FakeHttpContextForRouting

o Request URL: ~/photo/title/my%20title

19. Create a new RouteCollection object named routes and pass it to the
RouteConfig.RegisterRoutes() method.

20. Call the routes.GetRouteData() method to run the test by using the following information:

o Return type: RouteData

o Return object name: routeData

o Method: routes.GetRouteData

o Http context object: context


21. Assert the following facts:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-27

o That routeData is not null.

o That the controller value in routeData is “Photo”

o That the action value in routeData is “DisplayByTitle”

o That the title value in routeData is “my%20title”

22. Run all the tests in the Photo Sharing Tests project to verify the test results.

 Task 2: Add and test the Photo ID route.


1. Open the RouteConfig.cs file in the PhotoSharingApplication project.

2. Add a new route to the Photo Sharing application by using the following information:

o Name: PhotoRoute
o Url: photo/{id}

o Default Controller: Photo

o Default Action: Display

o Constraints: id = "[0-9]+"

3. Run all the tests in the Photo Sharing Tests project to verify the test results.

 Task 3: Add and test the Photo Title route.


1. Add a new route to the Photo Sharing application by using the following information:
o Name: PhotoTitleRoute

o Url: photo/title/{title}

o Default controller: Photo


o Default action: DisplayByTitle

2. Add a new action method to PhotoController.cs by using the following information:

o Scope: public

o Return type: ActionResult

o Name: DisplayByTitle

o Parameter: a string named title


3. In the DisplayByTitle action method, use the context.FindPhotoByTitle() method to locate a
photo. If the context.FindPhotoByTitle() method returns null, return HttpNotFound(). Otherwise,
pass the photo to the Display view.

4. Run all the tests in the Photo Sharing Tests project to verify the test results.

 Task 4: Try out the new routes.


1. Start the PhotoSharingApplication project with debugging.

2. View properties of the Display link of any image on the home page, and note the route that has
been used to formulate the link.

3. Display any image to verify the URL.

4. Access the following relative URL:

o /photo/title/sample photo 3.
MCT USE ONLY. STUDENT USE PROHIBITED
07-28 Structuring ASP.NET MVC 4 Web Applications

5. Stop debugging.

Results: After completing this exercise, you will be able to create a Photo Sharing application with three
configured routes that enable visitors to easily locate photos using logical URLs.

Exercise 2: Building Navigation Controls


Scenario
In this exercise, you will:

 Add the MVC site map provider to your Photo Sharing application.

 Use the MVC site map provider to create a menu and a breadcrumb control.

At this stage of development, most of the main areas in the Photo Sharing Application are not yet built;
therefore, the menu will show only the home page and the All Photos gallery. Your team will add new
nodes to the site map after the areas of the site are

The main tasks for this exercise are as follows:

1. Install the MVC site map provider.


2. Configure the MVC site map provider.

3. Render menus and breadcrumb trails.

4. Try out the menus.

 Task 1: Install the MVC site map provider.


1. Start the NuGet Packages manager and locate the MvcSiteMapProvider package

2. Install the MvcSiteMapProvider package.

 Task 2: Configure the MVC site map provider.


1. Open the Web.config file in the PhotoSharingApplication project.
2. Configure the MvcSiteMapProvider to disable localization.

3. Save the changes made to the Web.config file.

4. Open the Mvc.sitemap file and remove the <mvcSiteMapNode> element with the title About.

5. Add an <mvcSiteMapNode> element within the Home node by using the following information:

o Title: All Photos

o Controller: Photo

o Action: Index

o Key: AllPhotos

6. Save the changes made to the Mvc.sitemap file.


7. Build the solution.

 Task 3: Render menus and breadcrumb trails.


1. Render a site menu on the Home view by using the following information:

o Helper: Html.MvcSiteMap()
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 07-29

o Method: Menu

o Start From Current Note: False

o Starting Node in Child Level: False

o Show Starting Node: True

2. Render a breadcrumb trail on the Home view by using the following information:

o Helper: Html.MvcSiteMap()

o Method: SiteMapPath

3. Render a site menu on the Photo Index view by using the following information:

o Helper: Html.MvcSiteMap()

o Method: Menu

o Start From Current Note: False


o Starting Node in Child Level: False

o Show Starting Node: True

4. Render a breadcrumb trail on the Photo Index view by using the following information:
o Helper: Html.MvcSiteMap()

o Method: SiteMapPath

 Task 4: Try out the menus.


1. Start debugging the PhotoSharingApplication project.
2. Use the menu option to browse to All Photos.

3. Use the breadcrumb trail to browse to the Home page.

4. Stop debugging and close the Visual Studio application.

Results: After completing this exercise, you will be able to create a Photo Sharing application with a
simple site map, menu, and breadcrumb control.

Question: In Exercise 1, when you ran the tests for the first time, why did
Test_Default_Route_Controller_Only pass when Test_Photo_Route_With_PhotoID and
Test_Photo_Title_Route fail?

Question: Why is the constraint necessary in the PhotoRoute route?


MCT USE ONLY. STUDENT USE PROHIBITED
07-30 Structuring ASP.NET MVC 4 Web Applications

Module Review and Takeaways


To create a compelling web application that is easy to navigate, you must consider carefully the
architecture of the information you present. You should try to ensure that the hierarchy of objects you
present in URLs and navigation controls matches user expectations. You should also ensure that users can
understand this hierarchy of objects without specialist technical knowledge. By using routes and site map
providers you can present logical information architecture to application visitors.

Best Practice: The default route is logical. However, it requires knowledge of controllers and actions
for users to understand URLs. You can consider creating custom routes that can be understood with
information that users already have.

Best Practice: You can use breadcrumb trails and tree view navigation controls to present the
current location of a user, in the context of the logical hierarchy of the web application.

Best Practice: You can use unit tests to check routes in the routing table, similar to the manner with
which you use tests to check controllers, actions, and model classes. It is easy for a small mistake to
completely change the way URLs are handled in your web application. This is because new routes, if
poorly coded, can override other routes. Unit tests highlight such bugs as soon as they arise.

Common Issues and Troubleshooting Tips


Common Issue Troubleshooting Tip

A route never takes effect.

Review Question(s)
Verify the correctness of the statement by placing a mark in the column to the right.

Statement Answer

You have implemented the MVC Site Map


Provider in your web application and used
it to build menus and breadcrumb trails
with which users can navigate the logical
hierarchy. MVC automatically takes routes
from the MVC Site Map Provider so the
same hierarchy is used in URLs.

Question:

You want to ensure that, when the user specifies a relative URL in the form “customer/3546”
the request is forwarded to the DisplayByID() action in the CustomerController. You also
want to ensure that, when the user specifies a relative URL in the form “customer/fullname”
the request is forwarded to the DisplayByName() action in the CustomerController. What
routes should you add?
MCT USE ONLY. STUDENT USE PROHIBITED
08-1

Module08
Applying Styles to ASP.NET MVC 4 Web Applications
Contents:
Module Overview 08-1 

Lesson 1: Using Layouts 08-2 

Lesson 2: Applying CSS Styles to an MVC Application 08-5 

Lesson 3: Creating an Adaptive User Interface 08-9 

Lab: Applying Styles to MVC 4 Web Applications 08-15 

Module Review and Takeaways 08-22 

Module Overview
While building web applications you should apply a consistent look and feel to the application. You
should include consistent header and footer sections in all the views. ASP.NET MVC 4 includes features
such as CSS styles and layouts that enhance the appearance and usability of your web application.
ASP.NET MVC 4 also includes features such as mobile specific views that allow you to build applications
for different browsers or mobile devices.

Objectives
After completing this module, you will be able to:

o Apply consistent layout to ASP.NET MVC 4 applications by using CSS and layout.
o Develop device-specific views to support various browsers.

o Build adaptive user interface for mobile devices.


MCT USE ONLY. STUDENT USE PROHIBITED
08-2 Applying Styles to ASP.NET MVC 4 Web Applications

Lesson 1
Using Layouts
You need to build multiple views to support the operations of the application, such as creating an order
and querying order history. However, several maintenance issues arise while changing the common part
of the application layout, because of which you need to update each view. To resolve these maintenance
issues, you can build a common module or a shared view. A shared view that helps to store the
application logic is called a layout. ASP.NET MVC 4 includes features that help simplify the process of
creating and using layouts. You can further simplify the application management process by using the
_ViewStart file, to apply the layout to each view instead of individually editing each view.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe layouts.
o Describe how to use layouts.

o Describe the _ViewStart file.

What are Layouts?


The ASP.NET MVC 4 Razor engine includes a
feature called layouts. Layouts are also called
template views. Layouts allow you to define a
common style template, and then apply it to all
the views in a web application. The functionality
of layouts is similar to that of the master page in a
traditional ASP.NET web application. You can use
layouts to define the content layout or logic that
is shared across views.

You can define multiple layouts in an ASP.NET


MVC 4 application, and each layout can have
multiple sections. You can define these sections
anywhere in the layout file, even in the <head> section of the HTML in the layout file.. Sections allow you
to output dynamic content as part of the final response.

Question: What are some common scenarios when you would use layouts?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-3

Creating a Layout
The \Views\Shared folder is the default location,
where you can store common view files or
templates. The following example illustrates a
layout.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<div>
@RenderBody()
</div>
</body>
</html>

In the preceding example, the @RenderBody() method indicates to the rendering engine where the
content of the view is placed.

You can use the ViewBag object to pass information between a view and a layout. To pass information,
you need to add a property to the ViewBag object, in the ViewPage of the ViewController or View file,
and use the same property in the layout file. Properties help you control the content in the layout, to
dynamically render webpages from the code in the view file. For example, consider that the template uses
the ViewBag.Title property to render the <title> content in the view. This property helps define the Title
property of the ViewBag object in the view and retrieve the property in the layout. This retrieval is
possible because the code in the view file runs before the layout runs.

Layouts can contain sections. The following example illustrates a layout that contains a section.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<div id="menu">
@RenderSection("MenuBar",required:false)
</div>
<div>
@RenderBody()
</div>
</body>
</html>

The MenuBar parameter in the RenderSection() helper method specifies the name of the section that
you want to render in the layout. The required parameter is optional; it allows you to determine if the
section you render is required. Consider that a section is required, and you do not implement the section
in the layout file. In this case, ASP.NET MVC 4.0 displays the Section not defined exception at runtime.
Implementing sections in a layout file makes it easier to track content errors. If a section is not required,
you can choose to not include it in the layout.
MCT USE ONLY. STUDENT USE PROHIBITED
08-4 Applying Styles to ASP.NET MVC 4 Web Applications

Additional Reading: For more information on layouts and sections, you can see:
http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-
razor.aspx

Question: Why do you have multiple sections in a layout?

Linking Views and Layouts


After defining the layout, you should link the
layout to the view files to apply the layout
template to the view...

To link a layout to a view, you need to add the


Layout directive at the top of the view file. The
following code shows how to add a layout in a
view file.

@{
ViewBag.Title = "Details";
Layout =
"~/Views/Shared/SiteLayout.cshtml";
}
<h2>Details</h2>

You can use the ViewBag.Title property to pass page title information from the view to the layout. You
can define other properties along with the ViewBag object, such as <meta> elements in the <head>
section, and enable them to pass information to the layout.
If you have multiple sections in the layout file, you define the content of the sections by using the
@section directive. The following code illustrates how to use the @section directive.

@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/SiteLayout.cshtml";
}
<h2>Details</h2>
@section MenuBar {
<p> this is menu</p>
}

In the preceding example, you set the layout file to display sections at the top of each view file. Usually,
you have the same layout across the entire web application or section. You can define the layout for an
application or area, by using the _ViewStart file. During runtime, the code in the _ViewStart file runs
before all the other views in the web application. Therefore, you can place all common application logics
in the _ViewStart file.

To use the _ViewStart file, you need to add the _ViewStart.cshtml file in the \Views folder of your project.
The following code illustrates the contents of the _ViewStart file.

@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
}

Question: When should you use the _Viewstart file?


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-5

Lesson 2
Applying CSS Styles to an MVC Application
Cascading Style Sheet (CSS) is an industry standard for applying styles to HTML pages. Different methods
of applying CSS styles to a webpage are available. These methods include external CSS file, inline CSS
style, and CSS style in HTML. An external CSS file is shared across multiple pages to apply a consistent
style across the application. You need to know how to import styles into a web application, to ensure
consistency in the appearance of the application. You also need to know how to use Expression Blend to
help create rich and interactive user interfaces.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the user experience that can be accomplished by using Expression Blend.Import styles
into an ASP.NET MVC 4 web application.

o Apply a consistent look and feel to an MVC 4 web application.

Overview of User Interface Design with Expression Blend


Expression Blend is a visual authoring tool that
helps developers and designers to create user
interfaces for Windows 8 HTML5 applications and
other web applications. Expression Blend for
HTML includes a visual designer, which serves as a
good complement to Visual Studio. Visual Studio
is developer-centric, whereas Expression Blend for
HTML is designer-centric. The functions in
Expression Blend facilitate developing HTML styles
and CSS.

Expression Blend for HTML includes the interactive


mode that allows developers to visualize the result
of the design, HTML, CSS, and JavaScript, by displaying results on the screen while they edit the code.
Expression Blend for HTML also includes CSS tools that allow you to generate and edit CSS styles on the
graphical user interface.
Question: What are the key benefits of using Expression Blend for HTML to edit CSS?
MCT USE ONLY. STUDENT USE PROHIBITED
08-6 Applying Styles to ASP.NET MVC 4 Web Applications

Importing Styles into an MVC Web Application


After creating CSS styles, you should import these
styles into the web application. After importing
the CSS file into the web application, you need to
modify the layout of the web application, so that
you can use the CSS styles that you imported. You
can modify the layout of a web application, by
using the <link> element. The following example
shows how to use the <link> element.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/Views/Shared/StyleSheet1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="menu">
@RenderSection("MenuBar",required:false)
</div>
<div>
@RenderBody()
</div>
</body>
</html>

CSS selectors help browsers to determine how the CSS styles should be applied. You can use various
selectors, such as class and id selectors, to apply styles to HTML elements.

CSS class Selector


You can define a CSS class selector to specify a style for a group of elements. To apply the class selector to
an HTML element, you need to add the class attribute to the HTML element. You can use the .<class>
syntax to add the style in the CSS file. The following example shows how to add the class selector in a
view.

@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/SiteLayout.cshtml";
}
<h2>Details</h2>
@section MenuBar {
<p class=”menu”> this is menu</p>
}

The following CSS snippet shows how to create the class selector.

.menu
{
font-weight:bold;
}

CSS id Selector
You can use the CSS id selector to specify a style for any unique element in your HTML code. To apply the
id selector to an HTML element, you need to add the id attribute and a unique name to the HTML
element. You can use the #<id> syntax to add the style in the CSS file.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-7

The following example illustrates how to add the id selector in a view.

@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/SiteLayout.cshtml";
}
<h2>Details</h2>
@section MenuBar {
<p id="leftmenu"> this is menu</p>
}

The following CSS snippet shows how to create the CSS id selector.

#leftmenu
{
font-size:16px;
}

Question: What are some common scenarios when you would use the class selector? What are some
common scenarios when you would use the id selector?

Demonstration: How to Apply a Consistent Look and Feel


In this demonstration, you will see how to:
o Create a layout.

o Apply the layout to an MVC view.

Demonstration Steps
1. Demonstrate that the site header, menu, and breadcrumb control appear only on the homepage.

2. Add a view to the OperasWebSite web application by using the following information:

o Folder: Views/Shared

o View name: _SiteTemplate

o View engine: Razor(CSHTML)

o View Type: Not strongly typed

o Layout or master page: None

3. Remove the Razor code from the new layout.

4. Set the TITLE element to @ViewBag.Title.

5. Cut the code for site heading, main menu, and breadcrumb control from the
Views/Home/Index.cshtml file and paste the code in the _SiteTemplate.cshtml file.

6. Add a new <div> element to the _SiteTemplate.cshtml file and set the content of the new <div>
tag to @RenderBody().

7. Link the _SiteTemplate.cshtml view to the ~/Content/OperasStyles.css style sheet.

8. Open the Index.cshtml view.


o File path: Views/Home/Index.cshtml.

9. In the Razor code, remove the existing code and set the ViewBag.Title property to Operas I Have
Seen.
MCT USE ONLY. STUDENT USE PROHIBITED
08-8 Applying Styles to ASP.NET MVC 4 Web Applications

10. Remove the following tags along with the corresponding closing tags:

o <!DOCTYPE>

o <html>

o <head>

o <meta>

o <title>

o <body>

o <div>

11. Add a new view by using the following information:

o Folder: Views

o View name: _ViewStart


o View engine: Razor(CSHTML)

o View type: Not strongly typed

o Layout or master page: None


12. In the _ViewStart.cshtml file, set the Layout to ~/Views/Shared/_SiteTemplate.cshtml and
remove all HTML markup.

13. Start the web application in debugging mode.


14. Demonstrate that the site header, menu, breadcrumb control, and style sheet now apply to all pages.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-9

Lesson 3
Creating an Adaptive User Interface
ASP.NET MVC 4 applications facilitate adaptive user interface to render content on different devices.
Adaptive user interface is a type of user interface that renders content based on the capability of the
target web browser or device. You need to ensure that your application supports mobile devices, so that it
reaches all types of audience. You also need to know how to use media queries, mobile specific views, and
jQuery Mobile to ensure that your application is effective on mobile devices.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the HTML5 viewport attribute.

o Explain how CSS media queries apply specific CSS styles based on the capabilities of the browser.

o Explain how you can use MVC 4 templates to render views based on mobile device screen size.
o Describe the use of jQuery Mobile for building user interfaces that work on a wide variety of
mobile devices.

o Use NuGet to add jQuery Mobile to your projects.

The HTML5 viewport Attribute


Adaptive rendering allows you to customize your
web application to display differently based on
the capabilities of the web browser or device.

Additional Reading: For more information


on adaptive rendering techniques, you can visit
http://msdn.microsoft.com/en-
us/magazine/hh975347.aspx

Mobile browsers such as Internet Explorer use the


viewport attribute to render webpages in a
virtual window. This virtual window is usually wider than the application screen. The viewport attribute
helps eliminate the need to reduce the size of layout of each page. Reducing the size of the layout can
break or distort the display of non mobile-optimized web applications. Creating the application interface
by using the viewport attribute enables users to zoom into the different areas of a webpage.

Additional Reading: For more information on the viewport attribute, you can visit
tps://developer.mozilla.org/en-US/docs/Mobile/Viewport_meta_tag

The viewport tag is a meta tag that helps to control the width and height of webpages, while it renders
to web browsers. The following example illustrates how to use the viewport tag with three common
properties used by the developers nowadays.

.
MCT USE ONLY. STUDENT USE PROHIBITED
08-10 Applying Styles to ASP.NET MVC 4 Web Applications

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

The width and height properties help to specify the width and height of the virtual viewport window.
You can specify the width in pixels. You can use the keyword device-width to enable the content to fit
the native screen size of the browser. This helps developer to create content that you can adjust according
to the device specifications.

The initial-scale property controls the initial scale or zoom level of the webpage. The maximum-scale,
minimum-scale, and user-scalable properties control the other scalability features of the webpage.

Question: How can you control the size of the virtual viewport window?

CSS Media Queries


You may sometimes need to apply different CSS
styles in your application, to support different
browsers. HTML5 includes CSS media queries,
which are special selectors that begin with
@media. Media queries allow conditional
application of CSS styles, based on the device
conditions or browser capabilities. You can apply
media queries in CSS and HTML.

The following example illustrates a media query in


CSS, which applies the style when the screen is
smaller than the maximum width of 500 pixels.

@media only screen and (max-width:


500px) {
header{
float: none;
}
}

You can also apply a media query in the <link> element. The following example illustrates how to include
a media query in a <link> element to apply the style when the screen is smaller than the maximum width
500 pixels.

<link rel="stylesheet" type="text/css" href="smallscreen.css" media="only screen and


(max-width: 500px)" />

YHowever, you can use CSS media queries only for the screen layout, but not for the print layout.

Additional Reading: For more information on media queries you can visit
http://www.w3.org/TR/css3-mediaqueries/

The following table describes all properties that you can include in a media query.

Property Description

width The width of the targeted display area, which includes the browser window in
desktop and mobile devices. In desktop computers, when you resize the
browser window, the width of the browser changes. However, on most mobile
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-11

Property Description
browsers, you cannot resize the browser window..

height The height of the targeted display area, which includes the browser window in
desktop and mobile devices.

device-width The width of the entire screen of a device. For a desktop with a screen
resolution of 1024x768, the device-width is usually 1024 pixels.

device-height The height of the entire screen of a device. For a desktop with a screen
resolution of 1024x768, the device-height is usually 768 pixels.

orientation The orientation of the device. If the device-width is larger than the device-
height, the orientation value is set to landscape; otherwise, it is set to
portrait.

aspect-ratio The ratio of the width and height properties.

device-aspect-ratio The ratio of the device-width and device-height properties. The following
example illustrates the device-aspect-ratio for a device with a screen
resolution of 1280x720.
@media screen and (device-aspect-ratio: 16/9) { }
@media screen and (device-aspect-ratio: 1280/720) { }
@media screen and (device-aspect-ratio: 2560/1440) { }

color The number of bits per color component of the device. If the device is not a
color device, the value is zero.

color-index The number of entries in the color lookup table, of the output device.

monochrome The number of bits per pixel in a monochrome frame buffer. For non-
monochrome devices, this value is zero.

resolution The resolution of the output device, or the density of the pixels. The common
units for this property include dpi and dpcm.

scan The scanning process of tv output devices.

grid The property that detects whether the output is in the grid or bitmap format.
Grid based devices return a value of one; all other devices return a value of
zero.

Question: Why would you choose to use CSS media queries instead of using C# code, to define styles for
specific browsers?
MCT USE ONLY. STUDENT USE PROHIBITED
08-12 Applying Styles to ASP.NET MVC 4 Web Applications

MVC 4 Templates and Mobile Specific Views


ASP.NET MVC 4 includes two new features, mobile
display mode and custom display mode, that
helps you to create webpages for mobile devices
and different browsers.

Additional Reading: For more information


on creating webpages for mobile device browsers,
you can visit http://msdn.microsoft.com/en-
us/magazine/hh975347.aspx

ASP.NET MVC 4 allows you to override views for


mobile devices, by using a different set of view files rather than using a configuration. When ASP.NET
MVC 4 receives a request from a mobile browser, it analyses the request for views with the naming
convention [view].mobile.cshtml. If ASP.NET MVC 4 detects a view with the mentioned naming
convention, ASP.NET MVC 4 will serve the request by using the mobile version of the view; otherwise, it
returns the request to the standard view.

Consider that your web application includes a layout that is specific to a browser. In this case, you can
create browser specific views for that browser, by checking the UserAgent string of that browser. The
UserAgent string helps identify a browser. . The following example illustrates how to create an iPhone
display mode in an ASP.NET MVC application, so that you can have different set of view file that is specific
to iPhone.

DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("iPhone")


{
ContextCondition = (context =>
context.GetOverriddenUserAgent().IndexOf
("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
});

Additional Reading: For more information on creating browser specific view, you can visit
http://www.asp.net/mvc/tutorials/mvc-4/aspnet-mvc-4-mobile-features

Question: Why would you choose device specific display modes over CSS media queries?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-13

jQuery Mobile
The jQuery Mobile library is a mobile version of
the jQuery library, which helps to develop
applications that run on mobile devices. jQuery
helps to add responsive UI elements to a web
application. jQuery also allows to add interactive
content to the mobile version of a web
application. Similar to jQuery, jQuery Mobile
library includes a set of JavaScript and CSS files
that allow you to build mobile-specific
applications, without adjusting the HTML
elements.

jQuery mobile includes the following JavaScript


and CSS files:

o jquery.mobile.structure-<version>.css

o jquery.mobile.structure-<version>.min.css

o jquery.mobile.theme-<version>.css

o jquery.mobile.theme-<version>.min.css

o jquery.mobile-<version>.css

o jquery.mobile-<version>.js

o jquery.mobile-<version>.min.css

o jquery.mobile-<version>.min.js
o images/ajax-loader.gif

o images/ajax-loader.png

o images/icons-18-black.png
o images/icons-18-white.png

o images/icons-36-black.png

o images/icons-36-white.png
The jQuery.Mobile.MVC NuGet package simplifies the process of adding the jQuery Mobile library to a
web application, by eliminating the need to manually install the library files. NuGet is a Visual Studio
extension that allows users to download packaged content from the Internet and directly install the
content into the project. The NuGet package contains the following items:

o The App_Start\BundleMobileConfig.cs file. This file helps to reference the jQuery JavaScript and
CSS files

o jQuery Mobile CSS files

o A ViewSwitcher controller widget

o jQuery Mobile JavaScript files


o A jQuery Mobile-styled layout file

o A view-switcher partial view. This provides a link at the upper end of each page; this link helps
switch from desktop view to mobile view and vice versa
MCT USE ONLY. STUDENT USE PROHIBITED
08-14 Applying Styles to ASP.NET MVC 4 Web Applications

o .png and .gif image files

Additional Reading: For more information on the NuGet package, you can visit
http://www.asp.net/mvc/tutorials/mvc-4/aspnet-mvc-4-mobile-features
Note: You can use the Microsoft Ajax Content Delivery Network (CDN) to improve the
performance of the site by using the servers that are located geographically closer to them. CDN
helps to improve the performance of the application. After adding the NuGet package to your
ASP.NET MVC application, you can change the code in HTML or the view, to reference the jQuery
Mobile library that is hosted on Microsoft Ajax CDN.
Additional Reading: For more information on jQuery mobile that is hosted on Microsoft
CDN, you can visit
http://www.asp.net/ajaxLibrary/CDN.ashx#jQuery_Mobile_Releases_on_the_CDN_3

Question: What are the benefits of using Microsoft Ajax CDN?


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-15

Lab: Applying Styles to MVC 4 Web Applications


Scenario
You have created a good amount of the photo-handling functionality for the Photo Sharing web
application. However, stakeholders are concerned about the basic black-and-white appearance of the
application. In addition, titles and menus do not appear on every page.

To resolve these issues, your manager has asked you to implement the following user interface features:

o A layout for all webpages. The layout should include common elements, such as the main menu
and breadcrumb controls, which should appear on every page of the application.

o A style sheet and images for all webpages. The web design team has provided an HTML mock-up
application to show how the final product should look. This mock-up includes a style sheet and
image files. You need to import these files and apply them to every page of the application.

o A mobile-specific view. The web application should be accessible from mobile devices such as
mobile phones and tablets. In particular, you need to ensure that devices with narrow screens can
access photos easily.

Objectives
After completing this lab, you will be able to:
o Apply a consistent look and feel to the web application.

o Use layouts to ensure common interface features such as the header are consistent across the
entire web application.
o Ensure that the web application renders smoothly on screens of different sizes and aspect ratios.

Lab Setup
Estimated Time: 45 minutes
Virtual Machine: 20486A-SEA-DEV11

Username: Admin

Password: Pa$$w0rd
Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:

 Enable the Allow NuGet to download missing packages during build option in Package
Manager.

Exercise 1: Creating and Applying Layouts


Scenario
In this exercise, you will:

o Browse through the Photo Sharing web application without a layout applied.
o Create a new layout and link the application to the view by using a _ViewStart.cshtml file.

o Modify the home index and photo display views to use the new layout.

o Browse through the resulting web application.

The main tasks for this exercise are as follows:


MCT USE ONLY. STUDENT USE PROHIBITED
08-16 Applying Styles to ASP.NET MVC 4 Web Applications

1. Open and browse through the Photo Sharing application.

2. Create a new layout.

3. Set the default layout for the application.

4. Update the views to use the layout.

5. Browse through the web application.

 Task 1: Open and browse through the Photo Sharing application.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin


o Password: Pa$$w0rd

2. Open the PhotoSharingApplication solution from the following location:

o File location: Allfiles (D):\Labfiles\Mod08\Starter\PhotoSharingApplication

3. Start the web application in debugging mode and verify that the menu and the breadcrumb trail are
available on the home page.

4. Browse to the All Photos webpage and verify that the menu and the breadcrumb trail are not
available on this page.

5. Browse to the Sample Photo 1 webpage and verify that the menu and the breadcrumb trail are not
available on this page.

6. Stop debugging.

 Task 2: Create a new layout.


1. Add a new layout to the PhotoSharingApplication project by using the following information:

o File location: /Views/Shared


o View name: _MainLayout

o View type: None

o Partial view: None


o Layout or mater page: None

2. Change the content of the TITLE element so that the page takes its title from the ViewBag.Title
property.

3. Add an H1 heading to the page body by using the following information:

o Class attribute: site-page-title

o Content: Adventure Works Photo Sharing


4. Add a DIV element to the page with the class clear-floats.

5. Add a DIV element to the page with the class topmenu. Within this element, render the main menu
for the page by using the following information:

o Helper: Html.MvcSiteMap()

o Method: Menu()

o Start from current node: False


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-17

o Starting node in child level: True

o Show starting node: True

6. Add a DIV element to the page with the class breadcrumb. Within this element, render the
breadcrumb trail for the page by using the following information:

o Helper: Html.MvcSiteMap()

o Method: SiteMapPath()

7. Add a DIV element to the page. Within this element, render the view body by using the following
information:
o Helper: RenderBody()

8. Save the layout.

 Task 3: Set the default layout for the application.


1. Add a new view to the web application by using the following information:
o File path: /Views

o View name: _ViewStart

o View type: None


o Partial view: None

o Layout or mater page: None

2. In the _ViewStart.cshtml file, set the Layout to ~/Views/Shared/_MainLayout.cshtml

3. Remove all the HTML code from the _ViewStart.cshtml file, except the layout element.

4. Save the file.

 Task 4: Update the views to use the layout.


1. Open the Views/Home/Index.cshtml view file.
2. In the first Razor code block, remove the existing line of code, and set the ViewBag.Title property to
Welcome to Adventure Works Photo Sharing.

3. Remove the following:

o Tags along with the corresponding closing tags:


 <!DOCTYPE>
 <html>
 <head>
 <meta>
 <title>
 <body>
 <div>
o Content
 Menu:
 Current Location:
MCT USE ONLY. STUDENT USE PROHIBITED
08-18 Applying Styles to ASP.NET MVC 4 Web Applications

4. Save the changes made to the Index.cshtml file.

5. Open the Views/Photo/Display.cshtml view file.

6. In the first Razor code block, remove the existing line of code and set the ViewBag.Title property to
the Title property of the Model object.

7. Remove the following tags along with the corresponding closing tags:

o <!DOCTYPE>

o <html>

o <head>
o <meta>

o <title>

o <body>

o <div>

8. Save the changes made to the Display.cshtml file.

9. Open the Views/Shared/DisplayTemplates/Error.cshtml view file.

10. In the Razor code block, remove the existing line of code and set the ViewBag.Title property to
Custom Error.

11. Remove the following tags along with the corresponding closing tags:

o <!DOCTYPE>

o <html>

o <head>

o <meta>

o <title>

o <body>
o <div>

12. Save the changes made to the Error.cshtml file.

 Task 5: Browse through the web application.


1. Start the web application in debugging mode and verify the menu and the breadcrumb trail on the
home page.

2. Browse to the All Photos webpage and verify that the site title, menu, and breadcrumb trail are
available on this page.

3. Browse to Sample Photo 1 webpage and verify that the site title, menu, and breadcrumb trail are
available on this page.

4. Stop debugging.

Results: After completing this exercise, you will be able to create an ASP.NET MVC 4 web application that
uses a single layout to display every page of the application.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-19

Exercise 2: Applying Styles to an MVC Web Application


Scenario
In this exercise, you will

o Examine a mockup web application that shows the look-and-feel the web designers have created
for the Photo Sharing application.
o Import a style sheet, with the associated graphic files from the mockup application, to your web
application, and then update the HTML element classes to apply those styles to the elements in
views.
Examine the changes to the user interface after the styles have been applied.

The main tasks for this exercise are as follows:

1. Examine the HTML mockup web application.

2. Import the styles and graphics.

3. Update the element classes to use the styles.

4. Browse the styled web application.

 Task 1: Examine the HTML mockup web application.


1. Open the mockup web application and verify the layout of the home page by using the following
information:

o File path: Allfiles (D):\Labfiles\Mod08 \Expression Web Mock Up\default.html

2. Browse to the All Photos webpage and verify the layout of the page.

3. Browse to the details of any photo and verify the layout of the page.

4. Close Internet Explorer.

 Task 2: Import the styles and graphics.


1. Add a new top-level folder to the PhotoSharingApplication project with the following information:

o Name of the folder: Content

2. Navigate to Allfiles (D):\Labfiles\Mod08\Expression Web Mock Up\Content, and add the


following existing files to the new folder:

o PhotoSharingStyles.css

o BackgroundGradient.jpg
3. Add a <link> element to the _MainLayout.cshtml file to link the new style sheet by using the
following information:

o Type: text/css

o Relation: stylesheet

o Href: ~/content/PhotoSharingStyles.css

4. Save the _MainLayout.cshtml file.

 Task 3: Update the element classes to use the styles.


1. Open the _PhotoGallery.cshtml file.
MCT USE ONLY. STUDENT USE PROHIBITED
08-20 Applying Styles to ASP.NET MVC 4 Web Applications

2. Locate the first DIV element in the file and set the class attribute to photo-index-card.

3. For the <img> tag, remove the width attribute and set the class attribute to photo-index-card-
img.

4. For the next DIV element, set the class to photo-metadata.

5. For the <span>Created By:</span> element, set the class attribute to display-label.

6. For the <span>@Html.DisplayFor(model => item.UserName)</span> element, set the class attribute
to display-field.

7. For the <span>Created On:</span> element, set the class attribute to display-label.
8. For the <span>@Html.DisplayFor(model => item.CreatedDate)</span> element, set the class
attribute to display-field.

 Task 4: Browse the styled web application.


1. Start the web application in debugging mode to examine the home page with the new style applied.
2. Browse to All Photos to examine the page with the new style applied.

3. Display a photo of your choice to examine the new style applied.

4. Stop debugging.

Results: After completing this exercise, you will be able to create a Photo Sharing application with a
consistent look and feel.

Exercise 3: Adapting Webpages for Mobile Browsers


Scenario
In this exercise, you will:
o Create a new layout for mobile devices.

o Add a media query to the web application style sheet to ensure that the photo index is displayed
on small screens.

o Test the settings applied to the application by using a small browser and changing the user agent
string.

The main tasks for this exercise are as follows:

1. Test the application as a mobile device.

2. Add a new mobile layout.

3. Add a media query to the style sheet.

4. Retest the application as a mobile device.

 Task 1: Test the application as a mobile device.


5. Start the web application in debugging mode.

1. Resize the browser window to the following dimensions:

o Width: 480 pixels

o Height: 700 pixels


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 08-21

2. Set the user agent string to IE9 for Windows Phone 7.

3. Refresh the home page and examine the mobile view of the application.

4. Stop debugging.

 Task 2: Add a new mobile layout.


1. Create a copy of the _MainLayout.cshtml file in the Views/Shared folder and rename the file as
_MainLayout.Mobile.cshtml.

2. In the _MainLayout.Mobile.cshtml file, in the main page heading, place a <br /> tag after the
words Adventure Works.

3. After the H1 element, add an H2 element.

o Content: Mobile Site

4. Save the _MainLayout.Mobile.cshtml mobile view.

 Task 3: Add a media query to the style sheet.


5. Open the PhotoSharingStyles.css style sheet.
1. Add a media query to the style sheet that applies when the screen width is 500 pixels or less.

2. Examine the existing style of the topmenulink class.

3. Add the same style to the media query.


4. In the media query, set the width attribute for the topmenulink style to 100 pixels.

 Task 4: Retest the application as a mobile device.


1. Start the web application in a debugging mode.

2. Clear the browser cache to ensure that the style sheet is reloaded.
3. Set the user agent string to IE9 for Windows Phone 7.

4. Close the developer window and refresh the web application to examine whether the problem
persists in the mobile view of the application.
5. Stop debugging and close Microsoft Visual Studio.

Results: After completing this exercise, you will be able to create a Photo Sharing application that displays
well on mobile devices and devices with small screens.

Question: When you first browsed the web application in Exercise 1, why was the menu and
the breadcrumb trail visible on the home page, but not on the All Photos page or any other
page?
Question: When you first viewed the site as a mobile browser in Exercise 3, what are the
problems you came across with the display of the site heading and menu?
MCT USE ONLY. STUDENT USE PROHIBITED
08-22 Applying Styles to ASP.NET MVC 4 Web Applications

Module Review and Takeaways


In this module, you learned how to apply a consistent look and feel to a web application, and share other
common components, such as headers and footers, between all views. You also learned how to use the
CSS and display modes to adapt the web application for smaller screens and mobile devices. You also
familiarized yourself with HTML5 elements that allow you to develop web applications that work on
various browsers and devices.

Real-world Issues and Scenarios


When you develop web applications, you need to create applications that work on different devices and
browsers, such as iPhone, iPad, Windows Phone, Google Chrome, and Internet Explorer 10. In such cases,
you can leverage the HTML5 elements and features in MVC 4, such as mobile specific views, media
queries, and jQuery Mobile library, to create applications that work well in various browsers and devices.
MCT USE ONLY. STUDENT USE PROHIBITED
09-1

Module09
Building Responsive Pages in ASP.NET MVC 4 Web
Applications
Contents:
Module Overview 09-1 

Lesson 1: Using AJAX and Partial Page Updates 09-2 

Lesson 2: Implementing a Caching Strategy 09-6 

Lab: Building Responsive Pages in ASP.NET MVC 4 Web Applications 09-12 

Module Review and Takeaways 09-19 

Module Overview
Many web applications need to display large amount of information and graphics. Large volumes of data
make web applications take longer to load. Instead of having all the elements on the page load multiple
times, you can implement caching in your web applications to reduce the need to repeatedly load the
same elements. You can also use partial page updates to reduce the need to load the entire webpage, by
enabling the application to refresh only specific sections of the webpage.

Objectives
After completing this module, you will be able to:

o Implement partial page updates that help reduce the need to reload entire pages.
o Implement caching to reduce the loading time of the different elements of a webpage.
MCT USE ONLY. STUDENT USE PROHIBITED
09-2 Building Responsive Pages in ASP.NET MVC 4 Web Applications

Lesson 1
Using AJAX and Partial Page Updates
While developing web applications, to update individual sections in the page, you may need to reload the
entire webpage. Asynchronous JavaScript and XML (AJAX) in ASP.NET MVC 4 allows partial page updates,
to help update sections of a webpage, without reloading the entire page. The Ajax.ActionLink helper
helps implement partial page updates in your web application.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the benefits of using partial page updates.

o Use AJAX in an ASP.NET MVC 4 web application.

o Use the Ajax.ActionLink helper.

Why Use Partial Page Updates?


ASP.NET and MVC facilitate server-side
processing. Server-side processing allows HTML
rendering to occur at the server side and the
server usually generates the HTML. When you
update a webpage, any updates or actions
performed on the page require a round-trip
request to the server. Such constant server
requests affect the performance of the
application.

The AJAX development model helps reduce the


need for refreshing an entire webpage, each time
an update of page content is required. AJAX uses
JavaScript and XML to obtain information from the server and update webpages based on the XML
information downloaded without refreshing the whole page However, developing web applications by
using AJAX is not easy, because it requires using complex technologies, such as JavaScript and XML.
Microsoft includes a feature in ASP.NET called partial page updates that functions along with AJAX to
reduce the need for refreshing an entire webpage, each time an update occurs.
Partial page updates use AJAX technologies to help update individual sections of a webpage, during
postback. Partial page updates:

o Require fewer lines of code when compared to traditional way of creating web pages.

o Help reduce the data sent to users, each time a webpage update occurs.

o Increase the responsiveness of the web application.

Question: How do partial page updates help in improving the responsiveness of a web application?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-3

Using AJAX in an MVC 4 Web Application


To implement AJAX in your MVC 4 application
with partial page, you need to create views that
render only the updated content, and not the
entire webpage. You can initially develop your
web application without using AJAX, and then
check the application for any functionality errors.
This practice helps reduce the time required to
troubleshoot the application. This practice also
helps separate any application functionality errors
from errors that occur while implementing AJAX.

To implement partial page updates, you need to


create a view, called a partial view, that includes
only the section that you need to update.

For example, consider a view as shown in the following code.

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<div id="divMessage">@ViewBag.Message</div>
@Html.ActionLink("Refresh","HelloWorld")
</div>
</body>
</html>

In the preceding code example, ActionLink helps direct users to another view called HelloWorld.
The following code shows the HelloWorld view.

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>HelloWorld</title>
</head>
<body>
<div>
@ViewBag.Message
</div>
</body>
</html>

To implement AJAX in the preceding code example, you need to update the HelloWorld view to render
only the content that is updated.

Consider another example wherein you want to render only an updated message, on a webpage. The
following code shows a view that is updated to render only the changed message.
MCT USE ONLY. STUDENT USE PROHIBITED
09-4 Building Responsive Pages in ASP.NET MVC 4 Web Applications

@ViewBag.Message

With AJAX, the Javascript retrieves only a specific portion of a webpage, which you want to update, from
the server. In the ViewController class, you need to update the View function to return the PartialView
class, instead of the entire View class.

The following code shows how to update the View function to return the PartialView class

public class Default1Controller : Controller


{
//
// GET: /Default1/
public ActionResult Index()
{
ViewBag.Message = "Hello";
return View();
}
[HttpGet]
public PartialViewResult HelloWorld()
{
ViewBag.Message = "Hello World";
return PartialView();
}
}

Optionally, you can add the HttpGet or HttpPost attributes before the View function. These attributes
help indicate if the partial page update should be performed over the HTTP POST or HTTP GET method.

Question: What is the mandatory action that you should perform to implement partial page updates in
your web application?

This Ajax.ActionLink Helper


You can use the Ajax.ActionLink helper to trigger
partial page updates. The Ajax.ActionLink helper
helps initiate the Javascript, to obtain the updated
HTML information from the view and replace or
insert the updated HTML information at a specific
location.

The following code shows how to use the


Ajax.ActionLink helper.

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.js"
type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-
ajax.min.js" type="text/javascript"></script>
</head>
<body>
<div>
<div id="divMessage">@ViewBag.Message</div>
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-5

@Ajax.ActionLink("Refresh","HelloWorld", new AjaxOptions{ HttpMethod =


"POST", UpdateTargetId = "divMessage", InsertionMode = InsertionMode.Replace })
</div>
</body>
</html>

In the preceding example, parameters such as HttpMethod and UpdateTargetId are included along with
the Ajax.ActionLink helper, to:

o Obtain the HTML information from the HelloWorld view, by using the HTTP POST method.

o Replace the content in the divMessage HTML element.

While using the Ajax.ActionLink helper, you need to include the jQuery and jQuery unobtrusive libraries
in the same webpage, because the Ajax.ActionLink helper uses scripts from these two libraries.

Question: What is the primary function of the Ajax.ActionLink helper?


MCT USE ONLY. STUDENT USE PROHIBITED
09-6 Building Responsive Pages in ASP.NET MVC 4 Web Applications

Lesson 2
Implementing a Caching Strategy
Web applications display information on a webpage by retrieving the information from a database. If the
information that should be retrieved from the database is large, the application may take longer to
display the information on a webpage. ASP.NET MVC 4 supports some caching techniques to help reduce
the time required to process a user request.

Before implementing caching, you should first analyze if caching is relevant to your application, because
caching is irrelevant to webpages whose content change frequently. To successfully implement caching in
your web application, you need to familiarize yourself with the various types of caches, such as output
cache, data cache, and HTTP cache.

Lesson Objectives
After completing this lesson, you will be able to:
o Describe the benefits of using caching.

o Describe the output cache.

o Describe the data cache.


o Describe the HTTP cache.

o Describe how to prevent caching for webpage content that changes frequently.

o Configure caching.

Why Use Caching?


Caching involves storing the information that is
obtained from a database in the memory of a web
server. If the content rendered by a webpage is
static in nature, the content can be stored in
caches or proxy servers. When a user requests
content from a web application, caching ensures
that the user receives content from the cache,
thereby eliminating the need for repeated real-
time processing.

Caching:

o Reduces the need to repeatedly retrieve


the same information from the database.

o Reduces the need to reprocess data, if a user places a request multiple times.
o Helps improve the performance of a web application, by reducing the load on servers.

o Helps increase the number of users that can access the server farm.

However, caching does not help web applications that include frequent content changes. This is because,
the content rendered from a cache may be outdated, when compared to the current information.
Therefore, you should evaluate the content of your web application and analyze the impact of rendering
outdated content, before implementing caching.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-7

Question: How does caching help increase the scalability of a web application?

The Output Cache


Output cache allows ASP.NET engines to store the
rendered content of a webpage in the memory of
the web server. Therefore, when a user requests a
specific page multiple times, the content is
retrieved from the cache, thereby avoiding the
execution of programming code in the server.

Output cache is a good complement to AJAX


partial page updates. Output cache and partial
page updates reduce the workload on the server
and increase the number of user requests that a
server can handle.

In ASP.NET MVC 4, you can implement output


caching, by adding the OutputCache attribute to the controller. The following code shows how to
implement output caching.

[OutputCache(Duration = 60)]
public PartialViewResult HelloWorld()
{
ViewBag.Message = "Hello World";
return PartialView();
}

The OutputCache attribute helps direct the rendering engine to the cache that contains results from the
previous rendering process. The Duration parameter of the OutputCache attribute helps control the
period of time in seconds for which data should be stored in the cache.

By default, the output cache stores only one copy of the rendered content, for each view. Consider a view
with the QueryString input parameter that enables content to change based on the variable gathered
from the database or a prior request. In this case, you can add the VaryByParam property to the
OutputCache attribute, to store a copy of each unique combination of parameters in the cache. The
following code shows how to add the VaryByParam property to the OutputCache attribute.

[OutputCache(Duration = 60, VaryByParam="ID")]


public PartialViewResult HelloWorld()
{
ViewBag.Message = "Hello World";
return PartialView();
}

In the preceding example, observe that the VaryByParam property refers to QueryString instead of
other MVC parameters. You can also use the VaryByCustom property. The following code shows how to
add the VaryByCustom property to the OutputCache attribute.

[OutputCache(Duration = 60, VaryByCustom="browser")]


public PartialViewResult HelloWorld()
{
ViewBag.Message = "Hello World";
return PartialView();
}
MCT USE ONLY. STUDENT USE PROHIBITED
09-8 Building Responsive Pages in ASP.NET MVC 4 Web Applications

You can add browser as the input parameter to the VaryByCustom property. The browser parameter
helps store a copy of the rendered content corresponding to each browser that the application is run on.
For example, you can have different version of cache stored for IE and non-IE users because the code in
the page is different. If you want to control and implement your own logic to determine when a new copy
should be stored, you need to override the GetVaryByCustomString function in the Global.asax file of
your project.

Question: How does the functioning of a web application that implements the output cache differ from
an application that does not implement output cache?

The Data Cache


Web applications usually depend on the content
in a database, to render content on a webpage.
Databases sometimes encounter performance
issues caused by poorly written queries, which can
slow down the performance of database requests
resulting in poor web page performance. You can
implement the data cache in your web application
to avoid loading data from a database every time
a user places a request. The MemoryCache class
allows you to implement data cache in your web
application. Implementing the data cache involves
the following actions:

1. Loading information from the database

2. Storing content in the MemoryCache object

3. Retrieving data from the MemoryCache object

4. Ensuring that content is available in the MemoryCache object; otherwise, reloading the content
The following code shows how to add the MemoryCache object.

System.Data.DataTable dtCustomer = System.Runtime.Caching.MemoryCache.Default.


AddOrGetExisting("CustomerData", this.GetCustomerData(),
System.DateTime.Now.AddHours(1));

In the preceding example, the following parameters are specified:

o Key. The unique identifier of the object that should be stored in the memory cache.

o Value. The object that should be stored in the memory cache.


o AbsoluteExpiration. The time when the cache should expire.

You can use the AddOrGetExisting function, instead of the Add function, to enable the application to
refresh and retrieve data in one line of code. If the cache contains the relevant data, the
AddOrGetExisting function retrieves the data from the cache. If the cache does not contain the relevant
data, the AddOrGetExisting function allows adding the data to the cache, and then rendering the same
data on the webpage.
One of the potential issues for using data cache is that the users may be viewing outdated content, which
is stored in memory, instead of taking the latest from the database. To resolve the issue, you can reduce
the amount of time for storing data in cache, and this helps to provide updated content to the users.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-9

Additional Reading: For more information on data cache, go to


http://msdn.microsoft.com/en-us/library/system.web.caching.cachedependency.aspx

Question: What are the benefits of implementing data caching in MVC applications?

The HTTP Cache


You can implement HTTP caching in the Browser
Cache and the Proxy Cache.

The Browser Cache

Most web browsers store the content downloaded


from web servers in their local cache. Storing data
in the local cache helps remove the need to
repeatedly download content from the server.
Web browsers frequently check content for
updates. If the content is updated in the server,
web browsers download the content from the
server, to attend to user requests. Otherwise, web
browsers render content from the local cache.

The Proxy Cache

The functionality of the proxy cache is similar to the functionality of the browser cache. However, the
cache is stored on a centralized server. Users can connect to the Internet or web servers by using this
proxy server. Proxy servers store a copy of a web application in a manner similar to a web browser storing
a copy of an application in the local drives. Many users can access the cache in a proxy server, while only
one user can access the browser cache at a time.

Question: What is the difference between HTTP cache and output cache?

Preventing Caching
Caching can sometimes create issues in web
applications, because if an application involves
frequent content updates, caching prevents users
from viewing these content updates. To resolve
caching issues, you can implement an HTTP
header called Cache-Control. The Cache-Control
header indicates to the web browser how to
handle the local cache or proxy server. All HTTP
clients, such as browsers and proxy servers,
respond to the instructions provided in the Cache-
Control header to determine how to handle the
local cache of a web application.

Additional Reading: For more information about setting this header, go to:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
MCT USE ONLY. STUDENT USE PROHIBITED
09-10 Building Responsive Pages in ASP.NET MVC 4 Web Applications

You can use the HttpCachePolicy.SetCacheability method to specify the value of the Cache-Control
header. The HttpCachePolicy.SetCacheability method helps control caching performance. The following
code shows how to use the HttpCachePolicy.SetCacheability method.

Response.Cache.SetCacheability(HttpCacheability.Private);

In the preceding example, the HttpCachePolicy.SetCacheability method takes the Private enumeration
value, which sets the header value to private indicating that the content should not be shared with other
users

To prevent caching in your web application, you should set the Cache-Control header value to NoCache.
The code shows how to exclude the HTTP cache in your web application.

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Question: What scenarios would require you to prevent caching for a web application?

Demonstration: How to Configure Caching


In this demonstration, you will see:

o How to configure the output cache for an MVC controller action.

o Measure the difference that the configured cache makes to the delivery of the page.

Demonstration Steps
1. Start the application in debugging mode and configure the browser to always refresh the page from
the server by using the Internet Explorer developer tools.

2. Capture traffic between the browser and the server when the All Operas page is loaded, by using the
Network tools.

3. Record the time taken by the server to render the /Opera page and return the page to the browser.
This value is the Request duration, which you can find on the Timings tab.

4. Clear the first network capture, and then capture a second request to the All Operas page.

5. Record the second instance of the time taken by the server to render the /Opera page and return the
page to the browser. Observe if the duration is more or less than the first instance.

6. Stop debugging.

7. Open the OperaController.cs file and add a using statement for the following namespace:

o System.Web.UI

8. Configure the Index action to use the output cache by using the following information:

o Duration: 10 minutes

o Location: Server

o Vary by parameters: None

9. Start the application in debugging mode, and configure the browser to always refresh the page from
the server, by using the Internet Explorer developer tools.

10. Capture the traffic between the browser and the server when the All Operas page is loaded, by using
the Network tools.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-11

11. Record the time taken by the server to render the /Opera page and return the page to the browser.
This value is the Request duration, which you can find on the Timings tab.
12. Clear the first network capture, and then capture a second request to the All Operas page.

13. Record the second instance of the time taken by the server to render the /Opera page and return the
page to the browser. Observe if the duration is more or less than the first instance.

14. Close the developer tools, stop debugging, and then close Visual Studio.
MCT USE ONLY. STUDENT USE PROHIBITED
09-12 Building Responsive Pages in ASP.NET MVC 4 Web Applications

Lab: Building Responsive Pages in ASP.NET MVC 4 Web


Applications
Scenario
Your manager has asked you to include comments for photos in the Photo Sharing application. Your
manager has also highlighted that the performance of some pages in the application is too slow for a
production site.

You want to ensure that comments for photos take minimal loading time, for which you decide to use
partial page updates. You also want to return pages in quick time, while updated information is displayed,
for which you decide to configure caching in your application.

Objectives
After completing this lab, you will be able to:

o Write controller actions that can be called asynchronously and return partial views.
o Use common AJAX helpers to call asynchronous controller actions, and insert the results into
Razor views.

o Configure ASP.NET caches to serve pages in quick time.

Lab Setup
Estimated Time:

60 minutes
Virtual Machine: 20486A-SEA-DEV11

Username: Admin

Password: Pa$$w0rd
Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:

o Enable the Allow NuGet to download missing packages during build option in Package Manager.

Exercise 1: Using Partial Page Updates


Scenario
You have been asked to include a comment functionality on the photo display view of the Photo Sharing
application. You want to ensure high performance by using AJAX partial page updates.

In this exercise, you will

o Import a partially complete controller to add comments, and a view to delete comments.

o Add code to the controller for partial page update.

The main tasks for this exercise are as follows:


1. Import the Comment controller and Delete view.

2. Add the _CommentsForPhoto action and view.

3. Add the _Create Action and the _CreateAComment views.


4. Add the _CommentsForPhoto POST action.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-13

5. Complete the _CommentsForPhoto view.

 Task 1: Import the Comment controller and Delete view.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Open the PhotoSharingApplication.sln file from the following location:

o File location: Allfiles (D):\Labfiles\Mod09\Starter


\PhotoSharingApplication

3. Create a new folder in the Views folder by using the following information:

o Name of the new folder: Comment

4. Add an existing item to the new Comment folder by using the following information:
o File location of the existing item: Allfiles (D):\Labfiles\Mod09\Comment
Components\Delete.cshtml

5. Add an existing item to the Controller folder by using the following information:
o File location of the existing item: Allfiles (D):\Labfiles\Mod09\Comment
Components\CommentController.cs

 Task 2: Add the _CommentsForPhoto action and view.


1. Add a new action to CommentController.cs. by using the following information:
o Annotation: ChildActionOnly

o Scope: public

o Return Type: PartialViewResult


o Name: _CommentsForPhoto

2. Parameter: an integer named PhotoId

3. In the _CommentsForPhoto action, select all the comments in the database that have a PhotoID
value equal to the PhotoId parameter, by using a LINQ query.

4. Save the PhotoId parameter value in the ViewBag collection to use it later in the view.

5. Return a partial view as the result of the _CommentsForPhoto action by using the following
information:

o View name: _CommentsForPhoto

o Model: comments.ToList()

6. Add a new partial view to display a list of comments by using the following information:

o Parent folder: Views/Shared

o View name: _CommentsForPhoto

o View type: Strong.

o Model class: Comment

o Create partial view: Yes.


MCT USE ONLY. STUDENT USE PROHIBITED
09-14 Building Responsive Pages in ASP.NET MVC 4 Web Applications

7. Bind the _CommentsForPhoto.cshtml view to an enumerable collection of comments.

8. Create an H3 element by using the following information:

o Heading: Comments

9. After the heading, create a DIV element with the ID comments-tool. Within this DIV element, create
a second DIV element with the ID all-comments.
10. For each item in the model, render a DIV element with the photo-comment class.

11. Within the <div class="photo-comment"> element, add a DIV element with the photo-comment-
from class. Within this DIV element, render the UserName value of the model item by using the
Html.DisplayFor() helper.

12. Add a DIV element with the photo-comment-subject class. Within this DIV element, render the
Subject value of the model item by using the Html.DisplayFor() helper.

13. Add a DIV element with the photo-comment-body class. Within this DIV element, render the Body
value of the model item by using the Html.DisplayFor() helper.

14. Render a link to the Delete action by using the Html.ActionLink() helper. Pass the
item.CommentID value as the id parameter.

15. In the Views/Photo/Display.cshtml view file, just before the Back To List link, render the
_CommentsForPhoto partial view by using the following information:

o Helper: Html.Action()

o View: _CommentsForPhoto

o Controller: Comment

16. PhotoId parameter: Model.PhotoID

17. Run the application in debugging mode and browse to Sample Photo 1. Observe the display of
comments on the page.
18. Close Internet Explorer.

 Task 3: Add the _Create Action and the _CreateAComment views.


1. Add a new action to the CommentController.cs file by using the following information:

o Scope: public

o Return type: PartialViewResult

o Name: _Create

2. Parameter: an integer named PhotoId.


3. In the _Create action, create a new Comment object and set its PhotoID property to equal the
PhotoId parameter.

4. Save the PhotoId parameter value in the ViewBag collection to use it later in the view.

5. Return a partial view named _CreateAComment.

6. Add a new partial view for creating new comments by using the following information:

o Parent folder: Views/Shared


o View name: _CreateAComment

o View type: Strong


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-15

o Model class: Comment

o Create partial view: Yes

7. In the _CreateAComment view, render validation messages by using the


Html.ValidationSummary() helper. For the excludePropertyErrors parameter, pass true.

8. After the validation messages, add a DIV element with the add-comment-tool class.

9. Within the <div class="add-comment-tool"> element, add a DIV element with no class or ID.

10. Within the DIV element you just created, add a SPAN element with the editor-label class and
content Subject:
11. After the SPAN element you just created, add a second SPAN element with the editor-field class.
Within this element, render the Subject property of the model by using the Html.EditorFor() helper.

12. Within the <div class="add-comment-tool"> element, add a second DIV element with no class or
ID.

13. Within the DIV element you just created, add a SPAN element with the editor-label class and
content Body:

14. After the SPAN element you just created, add a second SPAN element with the editor-field class.
Within this element, render the Body property of the model by using the Html.EditorFor() helper.

15. Within the <div class="add-comment-tool"> element, add an INPUT element by using the
following information:

o Element: <input>

o Type: submit
16. Value: Create

17. Save all your changes.

 Task 4: Add the _CommentsForPhoto POST action.


1. Add a new action to the CommentController.cs file by using the following information:
o Annotation: HttpPost

o Scope: public

o Return type: PartialViewResult

o Name: _CommentsForPhoto

o Parameter: a Comment object named comment.

2. Parameter: an integer named PhotoId.

3. In the _ComentForPhoto action, add the comment object to the context and save the changes to
the context.

4. Select all the comments in the database that have a PhotoID value equal to the PhotoId parameter
by using a LINQ query.

5. Save the PhotoId parameter value in the ViewBag collection to use it later in the view.

6. Return a partial view as the result of the _CommentsForPhoto action by using the following
information:

o View name: _CommentsForPhoto


MCT USE ONLY. STUDENT USE PROHIBITED
09-16 Building Responsive Pages in ASP.NET MVC 4 Web Applications

7. Model: comments.ToList()

8. Save all the changes.

 Task 5: Complete the _CommentsForPhoto view.


1. In the _CommentsForPhoto.cshtml view file, render an HTML form around all tags by using the
following information:

o Helper: Ajax.BeginForm()

o Action name: _CommentsForPhoto

o PhotoId parameter: ViewBag.PhotoId

2. Ajax options: UpdateTargetId = "comment-tool"


3. In the form code block, in the <div class="comments-tool"> element, add a new DIV element with
the add-comment-box class and the ID add-comment.

4. In the DIV element you just created, render the _Create action of the Comment controller by using
the Html.Action() helper. Pass the ViewBag.PhotoId value as the PhotoId parameter.

5. Add script tags to the _MainLayout.cshtml page that reference the following content delivery
network (CDN) locations:
o http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js

6. http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.js

7. Start the web application in debugging mode, browse to Sample Photo 1, and observe the
comments displayed.

8. Add a new comment to Sample Photo 1.

o Subject: Test Comment


9. Body content: This comment is to test AJAX-based partial page updates.

10. Stop debugging.

Results: At the end of this exercise, you will have ensured that new comments can be added and
displayed on the pages of the application without a complete page reload. You will create a Photo
Sharing application with a comments tool, implemented by using partial page updates.

Exercise 2: Configuring the ASP.NET Caches


Scenario
You have been asked to configure the ASP.NET caches in the Photo Sharing application to ensure optimal
performance. Senior developers are particularly concerned that the All Photos gallery might render slowly
because it will fetch and display many photos from the database at a time.

In this exercise, you will:

 Configure the output cache to store the photo index view.

 Use the developer tools in Internet Explorer to examine the speed at which image files and pages
render with and without caching.

 Configure the output cache to store the results of the GetImage action so that image files can be
returned from the cache.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-17

The main tasks for this exercise are as follows:

1. Test the All Photos page with no caching.

2. Configure caching for the Index action.

3. Retest the All Photos page with Index caching.

4. Configure caching for the GetImage action.

5. Retest the All Photo page with GetImage caching.

 Task 1: Test the All Photos page with no caching.


1. Start the application in debugging mode and configure the browser to always refresh the page from
the server by using the Internet Explorer developer tools.

2. Capture traffic between the browser and the server when the All Photos page is loaded, by using the
Network tools.

3. Record the time taken by the server to render the /Photo page and return the page to the browser.
This value is the Request duration, which you can find on the Timings tab.

4. Clear the first network capture, and capture a second request to the All Photos page.

5. Record the second instance of time taken by the server to render the /Photo page and return the
page to the browser. Observe if the duration is more or less than the first instance.

6. Stop debugging.

 Task 2: Configure caching for the Index action.


1. Open the PhotoController.cs code file, and add a using statement for the following namespace:
2. System.Web.UI

3. Configure the Index action to use the output cache by using the following information:

o Duration: 10 minutes
o Location: Server

o Vary by parameters: None

4. Save all your changes.

 Task 3: Retest the All Photos page with Index caching.


1. Start the application in debugging mode, and configure the browser to always refresh the page from
the server, by using the Internet Explorer developer tools.

2. Capture the traffic between the browser and the server when the All Photos page is loaded, by using
the Network tools.

3. Record the time taken by the server to render the /Photo page and return the page to the browser.
This value is the Request duration, which you can find on the Timings tab.

4. Clear the first network capture, and capture a second request to the All Photos page.

5. Record the second instance of the time taken by the server to render the /Photo page and return the
page to the browser. Observe if the duration is more or less than the first instance.
6. Record the time taken by the server to render the /Photo/GetImage/1 request.

7. Stop debugging.
MCT USE ONLY. STUDENT USE PROHIBITED
09-18 Building Responsive Pages in ASP.NET MVC 4 Web Applications

 Task 4: Configure caching for the GetImage action.


1. In the PhotoController, configure the GetImage action to use the output cache, by using the
following information:

o Duration: 10 minutes.

o Location: Server

2. Vary by parameters: id
3. Save all your changes.

 Task 5: Retest the All Photo page with GetImage caching.


4. Start the application in debugging mode and configure the browser to always refresh the page from
the server, by using the Internet Explorer developer tools.

5. Capture the traffic between the browser and the server when the All Photos page is loaded, by using
the Network tools.

6. Record the time taken by the server to render the /Photo/GetImage/1 request.

7. Clear the first network capture, and capture a second request to the All Photos page.

1. Record the second instance of the time taken by the server to render the /Photo/GetImage/1
request and return the page to the browser.
2. Close the developer tools, stop debugging, and close Visual Studio.

Results: At the end of this exercise, you will create a Photo Sharing application with the Output Cache
configured for caching photos.

Question: In Exercise 2, why was the Request timing for /Photo not reduced for the first
request when you configured the output cache for the index action?

Question: In Exercise 2, when you configured the output cache for the GetImage() action,
why was it necessary to set VaryByParam="id"?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 09-19

Module Review and Takeaways


In this module, you learned how to use AJAX and partial page updates in MVC applications. AJAX and
partial page updates help reduce the need for reloading the entire page, when a user places a request.
Partial page updates also reduce the need for writing multiple lines of code, to update specific portions of
a webpage. You also learned how to use caching to increase the performance of a web application.

Real-world Issues and Scenarios


Web applications usually run multiple queries to retrieve information from a database and render content
on the webpages. Users sometimes complain that webpages take longer to load. Therefore, developers
implement caching in the web application, to reduce the need to load data from a database, every time a
user places a request. Caching helps webpages load faster, thereby increasing the performance of the
application.
MCT USE ONLY. STUDENT USE PROHIBITED
09-20 Building Responsive Pages in ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
10-1

Module10
Using JavaScript and jQuery for Responsive MVC 4 Web
Applications
Contents:
Module Overview 10-1 

Lesson 1: Rendering and Executing JavaScript Code 10-2 

Lesson 2: Using jQuery and jQueryUI 10-8 

Lab: Using JavaScript and jQuery for Responsive MVC 4 Web Applications 10-15 

Module Review and Takeaways 10-20 

Module Overview
Responsive web design is a web design approach that helps create visually rich and interactive web
applications. JavaScript plays an important role in the development of responsive web applications. You
need to know how to use JavaScript to implement application logic and resize interface elements, without
triggering a full-page refresh. To simplify adding JavaScript to your web application, you need to know
how to use libraries such as jQuery, jQuery UI, and jQuery Mobile.

Objectives
After completing this module, you will be able to:

 Add JavaScript code to your web application.


 Use the jQuery and jQuery UI libraries, in your web application.
MCT USE ONLY. STUDENT USE PROHIBITED
10-2 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

Lesson 1
Rendering and Executing JavaScript Code
You can create interactive HTML elements in your web application, by using JavaScript. ASP.NET renders
these interactive elements on your webpages. You can add packaged JavaScript libraries to your project
by using the NuGet tool. You should know how to use AJAX to update the contents of webpages. By
using AJAX, you can optimize the performance of your web application. In addition, you should know
how the content delivery network (CDN) helps take content geographically closer to users.

Lesson Objectives
After completing this lesson, you will be able to:

 Describe how to add JavaScript files to an MVC application.

 Describe how to call JavaScript functions in JavaScript libraries.

 Describe how to use JavaScript Libraries in MVC 4 web applications.


 List the benefits of using CDN to improve the performance of JavaScript libraries.

 Describe how to use the NuGet tool to add packages.

 Use the NuGet tool to add a JavaScript Library.

Adding JavaScript Files


You can add JavaScript code to web applications
by:

o Adding the JavaScript code to HTML.

o Defining the JavaScript code in dedicated


JavaScript files.

JavaScript code helps add interactive


functionalities to the webpages of your
application. The following example shows how to
add JavaScript to HTML.

<body>
<script type="text/javascript">
function HelloWorld() {
alert('Hello World');
}
</script>
<div>
...
</div>
</body>

If you have multiple HTML pages in a web application, you need to add JavaScript code for each HTML
page. You cannot simultaneously add JavaScript code for multiple HTML pages. Therefore, you can define
the JavaScript code in a JavaScript file (.js file). Then, you can reference the JavaScript file in multiple
HTML pages. This enables you to maintain a single JavaScript file, to edit the JavaScript code for multiple
HTML pages. You can also have multiple JavaScript code files for a single HTML page.

The following example shows how to reference the new JavaScript file in HTML pages.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-3

<script src="~/Scripts/JavaScript1.js" type="text/javascript"></script>

You can create the Scripts folder in the root folder of your MVC project, and then save all JavaScript files
in the Scripts folder.

Question: What are the advantages of using a JavaScript file?

Calling JavaScript Procedures


You can call functions defined in JavaScript files
by using script blocks or event handlers. The
following code shows how to call the HelloWorld
function.

<body>
<script type="text/javascript">
HelloWorld();
</script>
<div>
Hello
</div>
</body>

Before calling a JavaScript function, you need to


define the function by using a script block. Then, you must reference the JavaScript file from the HTML
pages.

If you want to avoid calling the JavaScript function directly, you can use the onclick JavaScript event to
trigger JavaScript functions. The onclick event initiates the JavaScript function assigned to an HTML file,
when you click the corresponding HTML element. JavaScript functions that are attached to document
object model (DOM) events are called event handlers.
The following code shows how to add the HelloWorld event handler to the button’s onclick event.

<body>
<div>
Hello
<input type="button" value="Hello" onclick="HelloWorld();" />
</div>
</body>

Additional Reading: For more information about events that are available for HTML
elements, go to http://msdn.microsoft.com/en-us/library/ms533023(v=vs.85).aspx

Question: What is the advantage of initiating JavaScript functions by using JavaScript events?
MCT USE ONLY. STUDENT USE PROHIBITED
10-4 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

JavaScript Libraries
You can reduce the time taken to develop
applications by using JavaScript libraries.
JavaScript libraries help to:

o Reduce the amount of code you need to


write to add functions.

o Reduce the time the system takes to


debug the application.

Some commonly used JavaScript libraries include


the following:

o jQuery (www.jquery.com)

o jQuery UI (www.jqueryui.com)

o jQuery Mobile (www.jquerymobile.com)


o jQuery Validation (www.jquery.com)

o jQuery Cycle (www.malsup.com/jquery/cycle/)

o jQuery DataTables (http://datatables.net/)

You can use JavaScript libraries to make your application more interactive. The functioning of JavaScript
codes depends on the version of the library you use. Not all code may work with all versions of a library.

The jQuery library (and its related libraries) has the additional advantage of dealing with the differences in
the DOM across different browsers and different browser versions.

Question: What is the advantage of using JavaScript libraries?

Using Content Delivery Networks for JavaScript Libraries


A content delivery network (CDN) is a group of
geographically distributed servers that is used for
hosting contents for web applications. In many
cases,, you can bring web content geographically
closer to your applications users by using a CDN
to host libraries. This will also improve the
scalability and robustness of the delivery of that
content.

The amount of content stored in a CDN varies


among different web applications. Some
applications store all their content on a CDN,
while other applications store only some of their
content.

Microsoft has a dedicated CDN called Microsoft Ajax CDN that hosts some popular JavaScript libraries,
such as:

o jQuery

o jQuery UI
o jQuery Mobile
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-5

o jQuery Validation

o jQuery Cycle

o jQuery DataTables

o Ajax Control Toolkit

o ASP.NET Ajax

o ASP.NET MVC JavaScript Files

Note that Microsoft does not own the license of the JavaScript libraries mentioned in the preceding list.
Microsoft only hosts the libraries for developers.

Additional Reading: For more information about Microsoft Ajax CDN, go to


http://ajax.aspnetcdn.com/

You can often reduce the loading time of your web application, by using the JavaScript libraries hosted on
Microsoft Ajax CDN. Web browsers can cache these JavaScript libraries on a local system.

The following code shows how to use JavaScript libraries.

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"
type="text/javascript"></script>

Additional Reading: For more information about Microsoft AJAX CDN, go to


http://www.asp.net/ajaxlibrary/cdn.ashx

Question: How can CDN help improve the performance of a web application?

Using the NuGet Tool to Add Packages


You can use the NuGet package manager to
manage JavaScript libraries. You can avoid adding
JavaScript libraries manually to your web
application by installing the NuGet package
manager in your application. This practice helps
reduce the need for configuration tasks, while
adding JavaScript libraries to an application.

Microsoft Visual Studio 2012 supports installing


and using NuGet packages. You can search for
NuGet packages in the NuGet store of Microsoft
Visual Studio 2012. Then, you can directly install
them into your MVC application.

The following image shows the application page that you can use to manage NuGet packages.
MCT USE ONLY. STUDENT USE PROHIBITED
10-6 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

Figure 10.1: Managing NuGet Packages

After you select the NuGet package that you wish to install, click Install, to download and install the
package into your project.

Additional Reading: To search for NuGet packages and analyze the details of each package, go to
http://nuget.org/packages

Question: Why should you use NuGet packages to add JavaScript libraries to your web application?

Demonstration: How to Use NuGet to Add a JavaScript Library


In this demonstration, you will see how to:

o Add the jQueryUI library to an application by using NuGet Package Manager.


o Access the location where jQueryUI components are added in the MVC application.

o Link to a script file in the site template view.

Demonstration Steps
1. Open the OperasWebSite project to examine that there is no Scripts folder at the top level.

2. Ensure that the Content folder contains only the OperaStyles.css file.

3. Install the jQueryUI package by using NuGet Package Manager in the OperasWebSite project.
4. In the OperasWebSite project, note that a Scripts folder has been created at the top level, and
populated.

5. In the Content folder, note that several style sheets have been added as sub-folders.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-7

6. In the _SiteTemplate.cshtml view, add a script link to the HEAD element after the link to jQuery by
using the following information:

o Type: text/javascript

o Src: ~/Scripts/jquery-ui-1.9.2.js

7. In the _SiteTemplate.cshtml view, add a link to the Content/themes/base/jquery-ui.css style sheet


by using the following information:

o Type: text/css

o Relation: stylesheet

8. Save all the changes made in the OperasWebSite project and then close Visual Studio.
MCT USE ONLY. STUDENT USE PROHIBITED
10-8 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

Lesson 2
Using jQuery and jQueryUI
jQuery is a JavaScript library that simplifies the adding of JavaScript to HTML pages. jQuery is an open-
source software that you can use for free. It helps reduce the amount of code that you need to write, to
perform tasks such as accessing and modifying HTML elements on a webpage. You can use the ajax
function in jQuery to call a web service in your application. You can also use jQuery UI to add interactions,
animations, effects, and widgets to your web applications.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe jQuery.

o Describe how to link web applications to jQuery Libraries for client-side scripting.

o Describe how jQuery helps access the HTML elements of a webpage.


o Describe how to modify elements by using jQuery.

o Describe how to call a web service by using jQuery.

o Describe jQuery UI.

o Add a jQuery UI widget to an MVC 4 web application.

Introduction to jQuery
jQuery is a JavaScript library that you can use with
different browsers. jQuery was first released in
2006. jQuery helps query the HTML Document
Object Model (DOM) and obtain a set of HTML
DOM elements. This feature of jQuery helps:

o Reduce the amount of code that you


need to write, to perform a task

o Reduce the development time of HTML


applications

jQuery includes the following features:

o DOM element selections

o DOM traversal and modification

o DOM manipulation, based on CSS selectors

o Events

o Effects and animations

o AJAX

o Extensibility through plug-ins

o Utilities

o Compatibility methods

o Multi-browser support
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-9

Additional Reading: For more information about jQuery, go to:


http://en.wikipedia.org/wiki/JQuery

The jQuery family includes the following two companion modules:

o jQuery UI. This library adds functions and other supporting elements that help implement a rich
interface to HTML-based applications.

o jQuery mobile. This library adds functions that optimize the application interface for mobile
devices.

Question: Why should you use jQuery while developing web applications?

Using different jQuery versions


jQuery Version and jQuery Minified Version
provide same functionalities; however, they
optimize web applications for different purposes:
o jQuery Version (jQuery-<version>.js). This
is the uncompressed version of the
jQuery library, with files sizes larger than
200 kilobytes (KB).

o jQuery Minified Version (jQuery-


<version>.min.js). This includes the
compressed and gZip versions of jQuery,
with file sizes around 30 KB.

When you develop the production environment, you can use jQuery Minified Version, to reduce the
loading time of the web application. If you use the minified version while working on the development
environment, it will be very difficult to access the source code of the JavaScript libraries during the debug
operation. Therefore, you can use the jQuery version, while creating the development environment.
The following code shows how to reference the jQuery library in your web application. You can add the
line of code in the <head> element of your HTML.

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js"
type="text/javascript"></script>

Bundling and Minification

Bundling is a new feature in ASP.NET 4.5, which you can use in MVC 4 web applications. Bundling helps
combine multiple JavaScript libraries into a single HTTP request.

Minification compresses code files before incorporating them in the client application. Bundling and
minification help reduce the loading time of web applications by reducing both the number and size of
HTTP requests.

To use bundling and minification in your web application manually, you should perform the following
steps:

1. Reference the Microsoft.AspNet.Web.Optimization library in your application, by using NuGet


packages.

2. In the App_Start folder of your project, add the BundleConfig.cs file.

3. In the BundleConfig.cs file, add the following code.


MCT USE ONLY. STUDENT USE PROHIBITED
10-10 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

public static void RegisterBundles(System.Web.Optimization.BundleCollection


bundles)
{
bundles.Add(new
System.Web.Optimization.ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-
{version}.js","~/Scripts/JavaScript1.js"));
}

4. In the Global.asax Application_Start event, add the following code.

BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);

The ScriptBundle class allows you to define the bundle that helps combine multiple JavaScript libraries
into a single file. You can use a special place holder, such as {version} in the JavaScript path, to help
update the version of jQuery libraries. ASP.NET replaces {version} with the latest version number of the
JavaScript libraries present in the Scripts folder. If the minified version of jQuery is available in the Scripts
folder, the MVC 4 engine selects the minified version for bundling. Then, in your View file, you can include
the following lines of code to render the bundled JavaScript file.

<head>
@Scripts.Render("~/bundles/jquery")
</head>

You should add the following line of code in a page or namespace of the web.config file in the View
folder. This code adds support to the functioning of @Scripts.Render in layout file.

<add namespace="System.Web.Optimization"/>

Based on the compilation setting in the web.config file, the jQuery library renders minified versions of
Javascript. For example, the following code sets the debug attribute of the compilation element to false
which allows jQuery to use the non-minified version of the libraries, making debugging of the libraries
much easier.

<compilation debug="false" />

Question: What are the benefits of using the minified version of jQuery in the production environment?

Accessing HTML Elements by Using jQuery


jQuery helps access HTML elements, to create
interactive web applications. You use the
following selector to select elements by element
name, id, or CSS class, while adding jQuery code
to access HTML elements.

$(element name|#id|.class)

The following jQuery selectoraccesses the HTML


element with the HelloButton ID.

$(“#HelloButton”)

You can use jQuery to access or modify all instances of a specific HTML element, in an HTML page. The
following jQuery selector identifies all instances of the A element in an HTML page.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-11

$(“a”)

After accessing the HTML elements, you can perform actions on the elements, such as:

o Modify the attributes on the HTML elements.

o Define event handlers to respond to events associated with the selected HTML elements.

The following example adds an event handler to the click event of the HelloButton HTML element.

$("#HelloButton").click(function (event) {
alert("Hello World");

If the jQuery scripts load before the webpage loads, you may encounter errors such as object not
defined. You can place the jQuery code in the document.ready event, to prevent the code from loading
until all HTML elements in the page load.

$(document).ready(function () {
});
});

The following complete example shows how to access HTML elements by using jQuery.

<body>
<div>
Hello
<input type="button" value="Hello" id="HelloButton" />
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#HelloButton").click(function (event) {
alert("Hello World");
});
});
</script>
</body>

Question: Why should you include the jQuery code in the document.ready event?

Modifying HTML Elements by Using jQuery


You can use jQuery to query HTML DOM and
obtain HTML elements. You can use jQuery
functions to modify the attributes associated with
the HTML elements. The following are some
commonly used jQuery functions, which allows to
modify HTML elements:
The val function

You can use the val function to get or set the


value of an HTML element. The following example
shows how to use the val function to set the value
of the HelloButton element to “Hello World”.

$('#HelloButton').val('Hello World');

The css function


MCT USE ONLY. STUDENT USE PROHIBITED
10-12 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

You can use the css function to get or set the inline CSS style associated with an HTML element. The
following example shows how to use the css function to set the background color of an HTML element to
blue.

$('#HelloButton').css('background-color','blue);

The addclass function

You can use the addclass function to assign a CSS class to an HTML element. The following example
shows how to use the addclass function to add the input_css_class to an HTML element.

$('#HelloButton').addClass('input_css_class');

Additional Reading: For more information about jQuery functions, go to


http://api.jquery.com/category/manipulation/

Question: If querying HTML DOM returns multiple HTML elements, how will jQuery functions handle
these elements?

Calling a Web Service by Using jQuery


jQuery includes the ajax function that helps:
1. Perform asynchronous calls to web services.

2. Retrieve the data returned from web services.

The following example illustrates how to use the


ajax function.

var req= $.ajax({


type: "POST",
dataType: "json",
url:
"Customer.asmx/GetCustomerInfo",
data: "{'ID': '123'}",
contentType:
"application/json; charset=utf-8",
success: function (msg) {
alert("Data Saved: " + msg);
},
failure: function (msg) {
alert(msg);
}
});

The ajax function uses the parameters such as type, datatype, url, data, contentType, success, and
failure to control how to call the web services. These parameters are describes as follows:

o type. This parameter controls the request type that you should use while querying the web
services.

o dataype. This parameter defines the data type to be sent for AJAX services.

o url. This parameter provides the URL of the web services.


o data. This parameter defines the data that you should provide as a parameter to the web services.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-13

o contentType. This parameter defines the HTTP content type that you should use, when you
submit HTTP requests to web services.

o success. This parameter defines the name of the function, which will be triggered when the call
completes successfully.

o failure. This parameter defines the name of the function, which will be triggered when the call
completes with errors.

When calls to the web services complete, jQuery triggers one of two callback functions based on the
success of the call.

Additional Reading: For more information about the AJAX function, go to


http://api.jquery.com/jQuery.ajax/

Question: Why should you call web services by using jQuery?

Introduction to jQueryUI
jQuery simplifies interacting with JavaScript
elements, by providing a simple query-based
syntax. jQuery UI is a library that includes
widgets, animations, and themes that help you to
build a rich user interface.

jQuery Widgets
You can add different types of widgets to your
pages by using the following jQuery functions:

o Accordion. This function adds accordion


containers to your webpage.

o Autocomplete. This function adds auto-


complete boxes that are based on user input.

o Button. This function adds buttons to your webpage.

o Datepicker. This function adds date-pickers to your webpage.


o Dialog. This function adds dialog boxes to your webpage.

o Menu. This function adds a menu to your webpage.

o Progressbar. This function adds animated and static progress bars to your webpage.

o Slider. This function adds sliders to your webpage.

o Spinner. This function adds a number spinner to a data entry box.

o Tabs. This function adds tabs to your webpage.

o Tooltip. This function displays a tooltip for interface elements.

jQuery Effects
You can add various effects by using the following jQuery functions:

o Color Animation. This function animates colors.


MCT USE ONLY. STUDENT USE PROHIBITED
10-14 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

o Toggle Class, Add Class, Remove Class, and Switch Class. This function adds or removes CSS
classes.

o Effect. This function adds a variety of effects, such as appear, slide-down, explode, and fade-in.

o Toggle. This function toggles any jQuery effect on and off.

o Hide and Show. This function displays or hides any jQuery effect.

jQuery Utility
You can align your webpage content by using the Position jQuery utility. This utility helps align an
element in relation to the position of another element.

Applying jQuery UI
To use jQuery UI:

1. Download the script and supporting files from http://jqueryui.com/download/

2. Reference the JavaScript libraries in the HTML files.

The following line of code shows how to use jQuery UI.

<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js"
type="text/javascript"></script>

Question: What is the key difference between jQuery and jQuery UI?

Demonstration: How to Add a jQueryUI Widget


In this demonstration, you will see how to create a set of expandable sections on a webpage by using the
Accordion widget.

Demonstration Steps
1. Start the web application in debugging mode. View how the reviews for Cosi Fan Tutte are currently
displayed.

2. Stop debugging.

3. Open the _ReviewsForOpera.cshtml view from the Shared folder.

4. Add a script that selects the element with ID reviews-tool and runs the jQueryUI accordion()
function on that element.

5. Start the application in debugging mode. Navigate to Cosi Fan Tutte and demonstrate the
Accordion widget.

6. Stop debugging and close Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-15

Lab: Using JavaScript and jQuery for Responsive MVC 4


Web Applications
Scenario
You have been asked to add a slideshow page to the web application that will show all the photos in the
database. Unlike the All Photos gallery, which shows thumbnail images, the slideshow will display each
photo in a large size. However, the slideshow will display only one photo at a time, and cycle through all
the photos in the order of ID.

You want to use jQuery to create this slideshow because you want to cycle through the photos in the
browser, without reloading the page each time. You also want to animate slide transitions and show a
progress bar that illustrates the position of the current photo in the complete list. You will use jQueryUI to
generate the progress bar.

Begin by importing a partially complete view that will display all photos simultaneously in the correct
format. Then, change styles and add jQuery code to the application to create your slideshow.

Objectives
After completing this lab, you will be able to:

o Render and execute JavaScript code in the browser.

o Use the jQuery script library to update and animate page components.

o Use jQueryUI widgets in an MVC application.

Lab Setup
Estimated Time:

45 minutes

Virtual Machine: 20486A-SEA-DEV11

Username: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:

o Enable the Allow NuGet to download missing packages during build option in Package Manager.

Exercise 1: Creating and Animating the Slideshow View


Scenario
Your team has created a view that displays photos of the right size and format. However, the view displays
all photos simultaneously, one below the other.

In this exercise, you will:

o Import the view and modify the style sheet so that the photos are displayed on top of each other.

o Using jQuery, set the order for each photo so that each photo is displayed sequentially.

The main tasks for this exercise are as follows:

1. Import and test the slideshow view.


MCT USE ONLY. STUDENT USE PROHIBITED
10-16 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

2. Modify the style sheet.

3. Animate the photo cards in the slideshow.

4. Link to the script and test the animation.

 Task 1: Import and test the slideshow view.


1. Start the virtual machine, and log on with the following credentials:

o Virtual machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Open the PhotoSharingApplication.sln file from the following location:

o File location: Allfiles (D):\Labfiles\Mod10\Starter


\PhotoSharingApplication

3. Add the SlideShow.cshtml view file to the Photo folder, from the following location:

o File location: Allfiles (D):\Labfiles\Mod10\Slide Show View

4. In the PhotoController.cs file, edit the SlideShow action method. Instead of throwing an exception,
return the SlideShow view you just added. Pass a list of all the photos in the context object as the
model.

5. Add a new site map node to the Mvc.sitemap file to link to the SlideShow action by using the
following information:

o Tag: <mvcSiteMapNode>

o Title: Slideshow

o Visibility: *

o Controller: Photo

o Action: SlideShow

6. Start the web application in debugging mode, clear the browser cache, and then browse to the
Slideshow view to examine the results.

7. Stop debugging.

 Task 2: Modify the style sheet.


1. In the Content folder, open the PhotoSharingStyles.css style sheet. Add the following properties to
the style that selects <div> tags with the slide-show-card class:

o position: absolute

o top: 0

o left: 0

o z-index: 8

2. Add a new style to the PhotoSharingStyles.css style sheet by using the following information:

o Selector: #slide-show DIV.active-card

o z-index: 10

3. Add a new style to the PhotoSharingStyles.css style sheet by using the following information:

o Selector: #slide-show DIV.last-active-card


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-17

o z-index: 9

4. Start debugging, and then clear the Internet Explorer browser cache to ensure that the style sheet is
reloaded.

5. Navigate to the Slideshow view and examine the results.

6. Stop debugging.

 Task 3: Animate the photo cards in the slideshow.


1. Add a new top-level folder, named Scripts, to the Photo Sharing application.

2. Add a new JavaScript file, SlideShow.js, to the Scripts folder.

3. In the SlideShow.js file, create the following global variables:


o percentIncrement

o percentCurrent

Set the percentCurrent value to 100.

4. Create a new function named slideSwitch with no parameters.

5. Within the slideSwitch function, add a line of code that selects the first <div> element with active-
card class that is a child of the element with an ID of slide-show. Store this element in a new variable
named $activeCard.

6. Add an if statement stating that if the $activeCard contains no elements, use the last DIV element
with slide-show-card class that is a child of the element with an ID of slide-show.

7. Add a line of code that selects the next element after $activeCard. Store this element in a new
variable named $nextCard.

8. Add an if statement stating that if $nextCard contains no elements, use the first DIV element with
slide-show-card class and ID slide-show.

9. Add the last-active-card class to the $activeCard element.

10. Set the opacity of the $nextCard element to 0.0.


11. Add the active-card class to the $nextCard element. This applies the z-order value 10, from the
style sheet.

12. Use the animate() function to fade the $nextCard element to opacity 1.0 over a time period of 1
second. When the animate() function is complete, remove the following classes from the
$activeCard element:

o active-card

o last-active-card

13. Create a new anonymous function that runs when the document is fully loaded.

14. In the new anonymous function, use the setInterval() function to run the slideSwitch() function
every 5 seconds.

15. Save all the changes.

 Task 4: Link to the script and test the animation.


1. Open the SlideShow.cshtml view file.

2. Add a SCRIPT element that links to the SlideShow.js script file.


MCT USE ONLY. STUDENT USE PROHIBITED
10-18 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

3. Start the application in debugging mode and navigate to the Slideshow view. Observe the fade
effects.

4. Stop debugging.

Results: At the end of this exercise, you will have created a Photo Sharing application with a slideshow
page that displays all the photos in the application, sequentially.

Exercise 2: Adding a jQueryUI ProgressBar Widget


Scenario
The slideshow pages you added works well. Now you have been asked to add some indication of progress
through the slideshow. You want to use a progress bar to show the position of the current photo in the
list of photos in the application. In this exercise, you will:

o Create a display by using the JQueryUI progress bar

o Test the script that you created.


The main tasks for this exercise are as follows:

1. Complete the slideshow view and template view.

2. Modify the slideshow script.


3. Test the slideshow view.

 Task 1: Complete the slideshow view and template view.


1. Open the SlideShow.cshtml view file from the Photo folder.

2. Within the <div id="slideshow-progress-bar-container">element, add a new <div> element with the
ID slide-show-progress-bar.

3. Add a <script>tag to the Views/Shared/_MainLayout.cshtml view to link the view to jQuery UI.
Ensure the <script> tag appears after the other <script>tags in the HEAD element. Link the view to
the following location: http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.0/jquery-ui.min.js

4. Add a <link> tag to link to the jQuery UI style sheet by using the following information:

o Type: text/css

o Rel: stylesheet

o Href: http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css

5. Save all the changes.

 Task 2: Modify the slideshow script.


1. Open the SlideShow.js JavaScript file.

2. Create a new function named calculateIncrement that takes no parameters.

3. In the new function, create a new variable named cardCount. Use this variable to store the number of
<div class="slide-show-card"> elements within the <div id="slide-show"> element.

4. Divide 100 by the cardCount variable, and store the result in percentIncrement.

5. Run the jQueryUI progressbar() function on the <div id="slidehow-progress-bar">element. Set


the value to 100.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 10-19

6. Before the call to setInterval(), insert a call to the new calculateIncrement() function.

7. At the beginning of the slideSwitch() function, add the value of percentIncrement to the value of
percentCurrent.

8. Add an if statement stating that if percentCurrent is more than 100, set percentCurrent is to equal
percentIncrement.

9. Run the jQueryUI progressbar() function on the <div id="slidehow-progress-bar"> element. Set
the value to percentCurrent.

10. Save all the changes.

 Task 3: Test the slideshow view.


1. Start the web application in debugging mode and clear the browser cache. Navigate to the
Slideshow view and examine the results.

2. Stop debugging and close Visual Studio.

Results: At the end of this exercise, you will have created a slideshow page with a progress bar that
displays the position of the current photo in the list of photos.

Question: What is the use of adding the two links to the _MainLayout.cshtml file in Task 1
of Exercise 2?

Question: You added <script> tags to the _MainTemplate.cshtml file to enable jQueryUI.
Is this the optimal location for this link?
MCT USE ONLY. STUDENT USE PROHIBITED
10-20 Using JavaScript and jQuery for Responsive MVC 4 Web Applications

Module Review and Takeaways


You have seen how to use JavaScript in a web application. JavaScript helps the application interact with
the actions of users and provide response to users, without reloading an entire webpage. You also saw
how to use the jQuery library to access the HTML DOM structure and modify HTML elements. jQueryUI is
a complement to the jQuery library, which contains functions to build rich user interfaces.
MCT USE ONLY. STUDENT USE PROHIBITED
11-1

Module11
Controlling Access to ASP.NET MVC 4 Web Applications
Contents:
Module Overview 11-1 

Lesson 1: Implementing Authentication and Authorization 11-2 

Lesson 2: Assigning Roles and Membership 11-7 

Lab: Controlling Access to ASP.NET MVC 4 Web Applications 11-13 

Module Review and Takeaways 11-26 

Module Overview
Authentication is a vital requirement in most web-based applications. Developers usually display only
restricted information to all users. Web applications require users to authenticate themselves to view
exclusive information. Web applications also display specific information that is relevant to specific user
roles. Microsoft ASP.NET 4.5 includes various authentication models including local authentication
providers, claim-based authentication systems, and federated authentication systems. You need to know
how to use these authentication models to implement authentication functionality in your web
application. You should also know how to authorize users and roles in your application to restrict
information access according to user and role membership.

Objectives
After completing this module, you will be able to:

o Implement authentication and authorization systems in your web application.

o Manage users and roles in your web application.

o Authorize users and roles in your web application.


MCT USE ONLY. STUDENT USE PROHIBITED
11-2 Controlling Access to ASP.NET MVC 4 Web Applications

Lesson 1
Implementing Authentication and Authorization
Membership providers, which were introduced in ASP.NET 2.0, help you to create secure authentication
and authorizations systems for web applications. You need to know how to use the membership provider
model in ASP.NET 4.5, to change authentication methods with minimum changes to the code. You can
use claim-based authentication and federated authentication to allow external users to authenticate
themselves, and access and use the web application. You should know how to configure restrictions in the
web application to ensure that users access only specific information that is permitted and relevant to
them.

Lesson Objectives
After completing this lesson, you will be able to:

o List the local authentication options available for an ASP.NET 4 web application.

o Describe how client-based authentication works.


o Describe how federated authentication works.

o Describe how to restrict access to resources.

o Authorize access to specific users to MVC 4 web application controller actions.

Local Authentication Providers


Authentication providers include code that runs
when ASP.NET needs to authorize a user. The
code authenticates users by using the information
stored in back-end databases, such as Active
Directory or Microsoft SQL Server. The
membership system in ASP.NET 4.5 includes the
following authentication providers:
ActiveDirectoryMembershipProvider,
SqlMembershipProvider,
SimpleMembershipProvider, and
UniversalProviders. The
SimpleMembershipProvider and
UniversalProviders are new authentication providers that support the OAuth authentication mechanism.

The ASP.NET 4.5 membership system allows application developers to switch amongst authentication
providers, without modifying the code.

The following is a description of the authentication providers—ActiveDirectoryMembershipProvider


and SqlMembershipProvider:

o ActiveDirectoryMembershipProvider. This provider class is defined in the


System.Web.Security namespace, and the provider enables you to use Active Directory as the
membership and role repository of your web application.

o SqlMembershipProvider. This provider class is defined in the System.Web.Security namespace.


The provider works with a specific table schema that you can generate using the
aspnet_regdb.exe command.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-3

ActiveDirectoryMembershipProvider and SqlMembershipProvider are configured to work only with a


specific table schema or directory. To overcome this restriction, Microsoft developed
SimpleMembershipProvider and UniversalProviders to replace ActiveDirectoryMembershipProvider
and SqlMembershipProvider.

o SimpleMembershipProvider. This is a new generation of membership providers that works with


the SQL Server, SQL Server Compact Edition, Windows Azure SQL Database, and other versions of
SQL Server. SimpleMembershipProvider requires only three key parameters—table name, user
ID, and user name. You use this provider to implement authentication that works with any SQL
Server database table schema.

o UniversalProviders. This is a set of membership providers that works with any database that
Entity Framework supports. However, these providers work only with the database schema
designed by Microsoft. While initializing a universal provider, if the schema does not exist in the
database, the provider generates the schema.

Question: What is the benefit of using SimpleMembershipProvider?

Claims-Based Authentication
Claims-based authentication is a model that
facilitates single sign-on. Single sign-on is a
feature that allows you to receive a claim when
you log on to a centralized authentication system.
The claim is a ticket that authentication systems
use to authenticate user logons. The claim
contains user identity information that helps
authentication systems identify users. With claims-
based authentication systems, you can focus
efforts on developing business functions, rather
than worrying about authenticating users.

Claims-based authentication facilitates:

o Authenticating users to access applications

o Storing user account information and passwords

o Checking enterprise directories for user information

o Integrating the application with the identity systems of other platforms or companies

To implement claims-based authentication in your web application, you need to use Windows Identity
Foundation (WIF) 4.5. WIF 4.5.is a set of .NET Framework classes that helps read information from the
claims in a web application.

The following steps describe the functioning of claims-based authentication systems:

1. When an unauthenticated user requests a webpage, the request is redirected to the Identity Provider
(IP) pages.

2. The IP requires you to enter credentials such as the user name and password.

3. After you enter the credentials, the IP issues a token. Then, the token is returned to the web browser.

4. The web browser is redirected to the webpage that you originally requested. The Windows Identity
Foundation (WIF) determines if the token satisfies the requirements to access the webpage. If the
MCT USE ONLY. STUDENT USE PROHIBITED
11-4 Controlling Access to ASP.NET MVC 4 Web Applications

token satisfies all requirements, a cookie is issued to establish a session. This cookie ensures that the
authentication process occurs only once. Then, the authentication control is passed on to the
application.

Question: What are the benefits of using claims-based authentication?

Federated Authentication
Federations rely on claims-based authentication
to allow external parties such as trusted
companies to access their applications.
Federations use the WS-Federations claim
standard to enable the exchange of claims
between two parties in a standardized manner.

WIF provides support for federations by using the


WSFederationAuthenticationModule HTTP
module. WSFederationAuthenticationModule
enables you to implement support for federations
in your ASP.NET 4.5 application, without
implementing individual logic.

Federated authentication enables Security Token Service (STS) to:

o Process the claims from business partners

o Extract user information from the claims

STS enables you to focus more on writing the business logic. It eliminates the need to manage the
authentication information of business partners, in your web application.
Configuring the WSFederationAuthenticationModule helps specify the STS to which non-authenticated
requests should be redirected. WIF provides two methods of federated authentication—
FederatedPassiveSignIn and Passive Redirect.
FederatedPassiveSignIn Control

Consider cases when unauthenticated users try to access protected resources and you want to redirect
these users to a logon page. For such cases, you can embed the FederatedPassiveSignIn control in the
logon page of your web application, to redirect unauthenticated users to the logon page. The
FederatedPassiveSignIn control is configured with issuer (STS) information. You can use the
FederatedPassiveSignIn control to:
o Exclude application-wide protection

o Include a logon page with clickable controls

Passive Redirect

Consider cases when unauthenticated users try to access a protected resource, and you want to redirect
these users to an STS without employing an application logon page. For such cases, you can use passive
redirect. Passive redirect enables STS to:

o Verify the identity of the unauthenticated users

o Issue security tokens that contain the appropriate claims for users
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-5

Passive redirect requires you to add WSFederationAuthenticationModule in the pipeline of the


Hypertext Transfer Protocol (HTTP) modules, to identify unauthenticated user requests and redirect users
to the STS you specify. Adding WSFederationAuthenticationModule in the HTTP pipeline processes the
claim information before passing the claim to the ASP.NET 4.5 engine.

You can instantiate WSFederationAuthenticationModule and use it to trigger the sign-on process. This
functionality is similar to that of the FederatedPassiveSignIn control; however, passive redirect
implements this sign-on functionality with minimum coding effort.

Question: What are the benefits of using federated authentication?

Restricting Access to Resources


You can restrict user access by implementing the
Authorize attribute in a controller instead of
using the Web.config file as you would use in a
ASP.NET WebForms application. The Web.config
file requires physical files to exist, for access
control to work. You cannot use the Web.config
file to restrict user access because MVC
applications route requests to actions, not pages.

The following code shows how to add the


Authorize attribute to your controller class.

[Authorize()]
public ActionResult GetEmployee()
{
return View();

Observe the Authorize attribute in the code sample. If you specify this attribute, ASP.NET 4.5 mandates
that users should be authorized to access the view returned by the code sample.

If you add the Authorize attribute at the class level, the attribute requires users to log on before they can
access anything in the controller class. To allow anonymous users to access a specific portion of your class,
you can use the AllowAnonymous attribute. The following code shows how to use the
AllowAnonymous attribute.

[AllowAnonymous()]
public ActionResult Register()
{
return View();
}

Question: Why should you use the Authorize attribute, instead of the Web.config file to control
authorization of pages in your ASP.NET MVC application?

Demonstration: How to Authorize Access to Controller Actions


In this demonstration, you will see how to:

o Generate authentication for access to a controller action.

o Handle unauthenticated requests for actions that require authentication by using ASP.NET.
MCT USE ONLY. STUDENT USE PROHIBITED
11-6 Controlling Access to ASP.NET MVC 4 Web Applications

Demonstration Steps
1. Start the web application in debugging mode and then browse to the Create an Opera page. The
link to this page can be found on the All Operas page.

2. Stop debugging.

3. Open the OperaController.cs file.

4. Add the [Authorize] annotation to the Create action for the GET requests.

5. Add the [Authorize] annotation to the Create action for the POST requests.

6. Save all the changes made.

7. Start the web application in debugging mode and then browse to the Create an Opera page.

8. Register as a user of the Operas web application by using the following credentials:

o User name: David Johnson

o Password: Pa$$w0rd

9. Browse to the Create an Opera page.

10. Stop debugging and then close Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-7

Lesson 2
Assigning Roles and Membership
Roles and memberships complement authentication features, to help you control all modes of access in
web applications. To define access levels for different types of users, you need to know how to implement
roles and users. ASP.NET 4.5 provides role providers and membership providers to help you assign roles
and users. ASP.NET 4.5 also enables you to create custom role providers and custom membership
providers. These custom providers allow you to store role and user information in data sources not
supported by ASP.NET 4.5, such as a FoxPro or Oracle database.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe role providers in ASP.NET 4.5.

o Describe how to add user accounts to roles.


o Describe how to build custom role providers.

o Describe membership services.

o Describe how to build custom membership providers.

o Add code to an MVC 4 web application to help users reset their password.

Role Providers in ASP.NET 4.5


Role providers help develop applications by using
a role-based security model. Similar to
membership providers, two types of role providers
are available in ASP.NET 4.5—
ActiveDirectoryRoleProvider and
SqlRoleProvider:
o ActiveDirectoryRoleProvider. This
provider class is defined in the
System.Web.Security namespace, and
the provider enables you to use Active
Directory as the management model for
roles.

o SqlRoleProvider. This provider class is defined in the System.Web.Security namespace. The


provider works with only a specific table schema in Microsoft SQL Server. You can generate this
schema by using the aspnet_regdb.exe command.

o WindowsTokenRoleProvider. This provider uses the Windows authentication token, to


determine the role of users. Then, the provider checks if users belong to any group stored in the
Windows authentication token that was generated when they first logged on. The format of the
group name is Domain\Group.

ActiveDirectoryRoleProvider and SqlRoleProvider have restrictions such as lack of support for non-SQL
databases or developer-defined schema. Therefore, Microsoft developed SimpleRoleProvider and
UniversalProviders to replace ActiveDirectoryRoleProvider and SqlRoleProvider. The following list
describes the SimpleRoleProvider and UniversalProviders:
MCT USE ONLY. STUDENT USE PROHIBITED
11-8 Controlling Access to ASP.NET MVC 4 Web Applications

o SimpleRoleProvider. This is a new generation of role provider that works with SQL Server, SQL
Compact Editions, and others versions of SQL Server. SimpleRoleProvider enables you to
implement authorization based on the table defined by application developers.

o UniversalProviders. This is a database agnostic version that works with any database that Entity
Framework supports. However, the database schema is determined by Microsoft. The provider
usually generates tables during initialization.

You can choose a role provider based on the membership provider that you select. You can also mix role
providers; for example, you can combine SimpleMembershipProvider and UniversalRoleProvider.
However, you should avoid mixing role providers because the manner in which each provider identifies
users is different.

Question: What is the difference between SimpleRoleProvider and SqlRoleProvider?

Adding User Accounts to Roles


When you use a database-based role provider,
such as SimpleRoleProvider or
UniversalRoleProvider, you can load the initial
data onto the database by directly editing the
table. You can also edit the table by using the
AddUsersToRoles function. To use the
AddUsersToRoles function, you need to
implement the custom management tool in your
application. The custom management tool helps
call the AddUsersToRoles function, to add roles
to a database. The following code shows how to
use the AddUsersToRoles function.

AddUserToRoles("Peter", new string[] {"Admin","Staff"})

You can also use the AddUsersToRoles function in business applications, to add users to a role. However,
you must ensure that the role to which you want to add users exists, before using the AddUsersToRoles
function. ASP.NET 4.0 and all previous versions of ASP.NET include the Website Administration Tool
(WSAT). However, WSAT does not work with the SimpleRoleProvider.

Question: Why should we not use WSAT while using SimpleRoleProvider?


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-9

Building a Custom Roles Provider


ASP.NET 4.5 allows you to build custom role
providers. Custom role providers enable you to
implement role management that uses your own
database schema and logic. To build a custom
role provider, you need to create a class that
inherits the RoleProvider class. The following
code shows how to build a custom role provider.

public class CustomRoleProvider :


RoleProvider
{
}

In the RoleProvider class, you need to implement


the GetRolesForUser function. The GetRolesForUser function takes the user name as the input and
returns a list of roles to which the user belongs. You can write your own code to obtain role information
from the database or other back-end stores. The following code shows how to use the GetRolesForUser
method.

public override string[] GetRolesForUser(string username)


{
//code to return a list of roles for users
}

After implementing the GetRolesForUser function, you need to apply the custom role provider to the
application by modifying the Web.config file. The following code shows how to apply the custom role
provider to the application.

<roleManager defaultProvider="CustomRoleProvider" enabled="true"


cacheRolesInCookie="false">
<providers>
<clear />
<add name="CustomRoleProvider" type="CustomRoleProvider” />
</providers>
</roleManager>

Question: Why should you create a custom role provider?

Providing Membership Services


You need to add the
SimpleMembershipProvider to the membership
section of the Web.config file to use it for
membership services. The following code shows
how to add SimpleMembershipProvider to the
Web.config file.

<membership
defaultProvider="SimpleMembershipProvi
der">
<providers>
<clear/>
<add
name="SimpleMembershipProvider"
MCT USE ONLY. STUDENT USE PROHIBITED
11-10 Controlling Access to ASP.NET MVC 4 Web Applications

type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />


</providers>
</membership>

Next, you need to create a User table. If you do not have a User table created already, you can modify the
AccountModel.cs file, to generate the table. To view the AccountModel.cs file, you should select the
Internet Application template, when you create the project. In the following code sample, the UserProfile
class from AccountModel.cs has been modified to add an addition property, Country.

[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Country { get; set; }
}

After modifying the AccountModel.cs file, you need to call the


WebSecurity.InitializeDatabaseConnection function in the App.Start.cs file. The
WebSecurity.InitializeDatabaseConnection function ensures that SimpleMembershipProvider is
configured with connection strings to the database. The following code shows how the membership
provider connects to the User table, by using the DBConn connection string.

WebSecurity.InitializeDatabaseConnection(“DBConn”,”User”,”id”,”Username”,false)

If a specific table does not exist in a database, the autoCreateTables parameter helps indicate to the
membership provider that it may create the table. To initialize SimpleMembershipProvider, you need to
add the InitializeSimpleMembership attribute to the AccountController class. The following code
shows how to add the InitializeSimpleMembership attribute to the AccountController class.

[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
}

Question: Why should we use the InitializeDatabaseConnection method?

Building a Custom Membership Provider


If you need to add a functionality to a
membership provider, such as custom logic for
authentication, you need to implement a custom
membership provider. The following code shows
how to implement a custom membership
provider.

public class CustomMembershipProvider


: SimpleMembershipProvider
{
}

After implementing the custom membership


provider, you override the ValidateUser method
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-11

defined in SimpleMembershipProvider. The following code shows how to override the ValidateUser
method defined in SimpleMembershipProvider.

public override bool ValidateUser(string username, string password)


{
}

In the preceding code sample, the ValidateUser method validates the username and password against
the membership store.

Perhaps, you want to add logic to get the decryption key for passwords. To add this logic, you can
override the Provider constructor, when the custom membership provider is initializing. Overriding the
Provider constructor initializes the encryption engine.

public CustomAdminMembershipProvider(SimpleSecurityContext simpleSecurityContext)


{
}

After adding logic to the custom membership provider, you need to modify the Web.config file to apply
the custom provider class to the application.

<membership defaultProvider="CustomMemberProvider">
<providers>
<clear/>
<add name="CustomMemberProvider" type=" CustomAdminMembershipProvider " />
</providers>
</membership>

Question: Why should you implement a custom membership provider?

Demonstration: How to Reset a Password


In this demonstration, you will see how to write code that uses a Membership Services Provider to reset
the user’s password. Using this code, you can enable users to control their own passwords and reset the
password without involving a site administrator.

Demonstration Steps
1. In the AccountController.cs file, in the ResetPassword action for the HTTP POST verb, locate the
following comment and replace it with a try… catch block:

o //Add Reset Password Code Here

2. In the try block, add a line of code that changes the user’s password, by using the following
information:

o Method: Membership.Provider.ChangePassword()

o User name: User.Identity.Name

o Old password: model.OldPassword

o New password: model.NewPassword

o Store the return value in the changePasswordSucceeded Boolean variable.

3. In the catch block, set the value of the changePasswordSucceeded variable to false.

4. Save all the changes.


MCT USE ONLY. STUDENT USE PROHIBITED
11-12 Controlling Access to ASP.NET MVC 4 Web Applications

5. Start the web application in debugging mode and log on with the following credentials:

o User name: David Johnson

o Password: Pa$$w0rd

6. Change your password to Pa$$w0rd2 by using the Reset Password option.

7. Stop debugging and close Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-13

Lab: Controlling Access to ASP.NET MVC 4 Web


Applications
Scenario
A large part of the functionality for your proposed Photo Sharing application is in place. However,
stakeholders are concerned about security because there are no restrictions on the tasks that users can
complete. The following restrictions are required:

o Only site members should be able to add or delete photos.

o Only site members should be able to add or delete comments.


You have been asked to resolve these concerns by creating a membership system for the Photo Sharing
application. Visitors should be able to register as users of the web application and create user accounts for
themselves. After registration, when the users log on to the application, they will have access to actions
such as adding and deleting photos and comments. Anonymous users will not have access to perform
these actions, but will be able to view photos and comments. Additionally, registered users should also be
able to reset their own password.

Objectives
After completing this lab, you will be able to:

o Configure a web application to use ASP.NET Form Authentication with accounts stored in
Windows Azure SQL Database.

o Write models, controllers, and views to authenticate users in a web application.


o Provide access to resources in a web application.

o Enable users to reset their own password.

Lab Setup
Estimated Time: 90 minutes

Virtual Machine: 20486A-SEA-DEV11

Username: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

o Before starting the lab, you need to perform the following step:
 Enable the Allow NuGet to download missing packages during build option in Package
Manager.
o After completing the lab, take a Snapshot of the 20486A-SEA-DEV11 virtual machine. Ensure
that this Snapshot is applied before initiating the lab in Module 13.

Exercise 1: Configuring Authentication and Membership Providers


Scenario
You want to use a Windows Azure SQL database to store user accounts and membership information.

In this exercise, you will:


MCT USE ONLY. STUDENT USE PROHIBITED
11-14 Controlling Access to ASP.NET MVC 4 Web Applications

o Create a Windows Azure SQL database.

o Configure a provider to connect to the database.

The main tasks for this exercise are as follows:

1. Configure a new Windows Azure SQL database.

2. Install Universal Providers.

3. Configure Providers in Web.config.

 Task 1: Configure a new Windows Azure SQL database.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Log on to the Windows Azure portal by using the following information:


o Portal address: http://manage.windowsazure.com

o Username: <username>

o Password: <password>
3. Create a new database server and a new custom database by using the following information:

o Database name: PhotoAppServices

o Database server: New SQL Database Server

o Logon name: <your Windows Azure account ID>

o Logon password: Pa$$w0rd

o Logon password confirmation: Pa$$w0rd


o Region: <a region close to you>

4. In the list of allowed IP addresses for the PhotoAppServices database, add the following IP address
ranges:

o Rule name: First Address Range

o Start IP Address: <first address in range>

o End IP Address: <last address in range>

 Task 2: Install Universal Providers.


1. Open the PhotoSharingApplication.sln file from the following location:

o File location: Allfiles (D):\Labfiles\Mod11\Starter


\PhotoSharingApplication

2. Install the Microsoft ASP.NET Universal Providers package in the PhotoSharingApplication


project by using the NuGet Package Manager.\

 Task 3: Configure Providers in Web.config.


1. Remove the connection string named DefaultConnection from the top-level Web.config file.

2. Obtain the connection string for the PhotoAppServices database and add it to the Web.config file.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-15

3. Configure the web application to use Forms authentication in Web.config, by using the following
information:
o Parent Element: <system.web>

o Element: <authentication>

o Mode: Forms

4. Configure the logon page for the web application by using the following information:

o Parent element: <authentication>

o Element: <forms>
o Logon URL: ~/Account/Login

o Timeout: 2880

5. Configure the default profile provider to use the connection string named PhotoAppServices.

6. Configure the Default Membership Provider to use the connection string named PhotoAppServices.

7. Configure the Default Role Provider to use the connection string named PhotoAppServices.

8. Configure the Default Session Provider to use the connection string named PhotoAppServices.

9. Save all the changes.

Results: At the end of this exercise, you will have created a Photo Sharing application that is configured to
use Windows Azure SQL database for user accounts and membership information. In subsequent
exercises, you will add model classes, actions, and views to implement authentication for this database.

Exercise 2: Building the Logon and Register Views


Scenario
You have configured the Photo Sharing application to connect to Windows Azure SQL database for
authentication and membership services. However, to use forms authentication in an MVC application,
you need to build model classes, controllers, and views that enable users to log on, log off, and register
for an account.

In this exercise, you will:

o Add model classes.


o Add controllers.

o Import logon and register views.

o Test the developed components.


The main tasks for this exercise are as follows:

1. Add account model classes.

2. Add an account controller.

3. Import Logon and Register views.

4. Add authentication controls to the Template view.


MCT USE ONLY. STUDENT USE PROHIBITED
11-16 Controlling Access to ASP.NET MVC 4 Web Applications

5. Test registration, log on, and log off.

 Task 1: Add account model classes.


1. Add a new Class file named AccountModelClasses.cs to the Models folder.

2. Add references to the following namespaces, to the new class file:

o System.ComponentModel.DataAnnotations

o System.Data.Entity

3. Remove the AccountModelClasses class and add a new class by using the following information:

o Scope: Public

o Name: UsersContext

o Inherit: DbContext

4. In the UsersContext class, create a constructor that passes the AzureAppServices connection string
to the base class constructor.
5. In the AccountModelClasses.cs code file, add a new public class named Login.

6. Add a new property to the Login class by using the following information:

o Scope: public

o Type: string

o Name: UserName

o Access: Read/Write
o Display name: User name

o Use the Required annotation.

7. Add a new property to the Login class by using the following information:
o Scope: public

o Type: string

o Name: Password

o Access: Read/Write

o Data type: Password

o Use the Required annotation.


8. Add a new property to the Login class by using the following information:

o Scope: public

o Type: bool
o Name: RememberMe

o Access: Read/Write

o Display name: Remember me?

9. In the AccountModelClasses.cs code file, add a new public class named Register.

10. Add a new property to the Register class by using the following information:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-17

o Scope: public

o Type: string

o Name: UserName

o Access: Read/Write

o Display name: User name

o Use the Required annotation.

11. Add a new property to the Register class by using the following information:

o Scope: public

o Type: string

o Name: Password

o Access: Read/Write
o Data type: Password

o Use the Required annotation.

12. Add a new property to the Register class by using the following information:
o Scope: public

o Type: string

o Name: ConfirmPassword
o Access: Read/Write

o Data type: Password

o Display name: Confirm password


o Ensure that this property matches the Password property by using the Compare annotation.

13. Save all the changes.

 Task 2: Add an account controller.


1. Add a new controller named AccountController to the MVC web application by using the Empty
MVC controller template.

2. Delete the default Index action from the AccountController file and add using statement references
for the following namespaces:

o System.Web.Security

o PhotoSharingApplicaition.Models

3. Create a new action method in the AccountController class by using the following information:

o Scope: public

o Return type: ActionResult

o Name: Login

o Parameter: a string named returnUrl.

4. In the Login action, store the returnUrl value in the ViewBag collection, and then return a view
named Login.
MCT USE ONLY. STUDENT USE PROHIBITED
11-18 Controlling Access to ASP.NET MVC 4 Web Applications

5. Create a new action method in the AccountController class by using the following information:

o HTTP verb: POST

o Scope: public

o Return type: ActionResult

o Name: Login

o Parameters: a Login object named model and a string named returnUrl.

6. Within the Login action method code block for the POST HTTP verb, check if the ModelState is
valid.

7. Add an if…else statement to check the user’s credentials by using the following information:

o Method: Membership.ValidateUser.

o User name: model.UserName


o Password: model.Password

8. If the user’s credentials are correct, authenticate the user by using the following information:

o Method: FormsAuthentication.SetAuthCookie
o User name: model.UserName

o Create persistent cookie: model.RememberMe

9. If returnUrl is a local URL, redirect the user to the returnUrl. Otherwise, redirect the user to the
Index action of the Home controller.

10. If the user’s credentials are incorrect, add a model error to the ModelState object by using the
following information:

o Key: An empty string

o Error message: The username or password is incorrect

11. If the ModelState is not valid, return the current view and pass the model object so that the user can
correct errors.

12. Create a new action method in the AccountController class by using the following information:

o Scope: public

o Return type: ActionResult

o Name: LogOff

o Parameters: None

13. In the LogOff action, log off the user, and then redirect to the Index action of the Home controller
by using the FormsAuthentication.SignOut() method.

14. Create a new action method in the AccountController class by using the following information:
o Scope: public

o Return type: ActionResult

o Name: Register

o Parameters: None

15. In the Register action, return the Register view.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-19

16. Create a new action method in the AccountController class by using the following information:

o HTTP verb: POST

o Scope: public

o Return type: ActionResult

o Name: Register

o Parameter: a Register object named model.

17. Within the Register action method code block for the POST HTTP verb, check if the ModelState is
valid.

18. If the ModelState is valid, create a try…catch block that catches exceptions of the type
MembershipCreateUserException.

19. In the try block, create a new user with the right user name and password by using the
Membership.CreateUser method. Store the result in a MembershipUser object named NewUser.

20. Authenticate the new user and redirect the browser to the Index action of the Home controller.

21. In the catch block, add a model error to the ModelState object by using the following information:
o Key: Registration Error.

o Error message: Report the error status code as a string.

22. If the ModelState is not valid, return the current view and pass the model object so that the user can
correct errors.

23. Save all the changes.

 Task 3: Import Logon and Register views.


1. Add a new folder named Account to the Views folder.
2. Add the Login.cshtml file to the Account folder from the following location:

o File location: Allfiles (D):\Labfiles\Mod11\Account Views

3. Add the Register.cshtml file to the Account folder from the following location:
o File location: Allfiles (D):\Labfiles\Mod11\Account Views

 Task 4: Add authentication controls to the Template view.


1. Open the _MainLayout.cshtml page for editing.

2. Immediately before the DIV element with clear-floats class, insert a DIV element with login-
controls class.

3. In the new DIV element, write a Razor if… else code block that checks whether the current request is
from an authenticated user.

4. If the request is from an authenticated user, render a greeting message that includes the
authenticated user’s name.

5. After the greeting message, render a link to the LogOff action by using the following information:

o Helper: Html.ActionLink()

o Link text: Log Off

o Action name: LogOff


MCT USE ONLY. STUDENT USE PROHIBITED
11-20 Controlling Access to ASP.NET MVC 4 Web Applications

o Controller name: Account

6. If the request is from an anonymous user, render a link to the Login action by using the following
Information:

o Helper: Html.ActionLink()

o Link text: Log In

o Action name: Login

o Controller name: Account

7. After the Log In link, render a link to the Register action by using the following Information:
o Helper: Html.ActionLink()

o Link text: Register

o Action name: Register

o Controller name: Account

8. Save all the changes.

 Task 5: Test registration, log on, and log off.


1. Start the web application in debugging mode and register a user account by using the following
information:

o User name: David Johnson

o Password: Pa$$w0rd
2. Log off and then log in with the credentials you just created.

3. Stop debugging.

Results: At the end of this exercise, you will create a Photo Sharing application in which users can register
for an account, log on, and log off.

Exercise 3: Authorizing Access to Resources


Scenario
Now that you have enabled and tested authentication, you can authorize access to resources for both
anonymous and authenticated users.

You should ensure that:

o Only site members can add or delete photos.

o Only site members can add or delete comments.

o The account controller actions are authorized properly.

o Only authenticated users see the_Create view for comments in the Display view.

The main tasks for this exercise are as follows:

1. Restrict access to Photo actions.

2. Restrict access to the Comment actions.

3. Restrict access to the Account actions.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-21

4. Check authentication status in a view.

5. Test authorization.

 Task 1: Restrict access to Photo actions.


1. In the PhotoController.cs file, add the [Authorize] annotation to ensure that only authenticated
users can access the Create action for the GET requests.

2. Add the [Authorize] annotation to ensure that only authenticated users can access the Create action
for the HTTP POST verb.

3. Add the [Authorize] annotation to ensure that only authenticated users can access the Delete
action.

4. Add the [Authorize] annotation to ensure that only authenticated users can access the
DeleteConfirmed action for the HTTP POST verb.

5. Save all the changes.

 Task 2: Restrict access to the Comment actions.


1. In the CommentController.cs file, add the [Authorize] annotation to ensure that only authenticated
users can access the _Create action.

2. Add the [Authorize] annotation to ensure that only authenticated users can access the Delete
action.

3. Add the [Authorize] annotation to ensure that only authenticated users can access the
DeleteConfirmed action for the HTTP POST verb.
4. Save all the changes.

 Task 3: Restrict access to the Account actions.


1. In the AccountController.cs file, add the [Authorize] annotation to ensure that only authenticated
users can access all actions by default.

2. Add the [AllowAnonymous] annotation to ensure that anonymous users can access the Login
action.

3. Add the [AllowAnonymous] annotation to ensure that anonymous users can access the Login action
for the HTTP POST verb.

4. Add the [AllowAnonymous] annotation to ensure that anonymous users can access the Register
action.

5. Add the [AllowAnonymous] annotation to ensure that anonymous users can access the Register
action for the HTTP POST verb.

6. Save all the changes.

 Task 4: Check authentication status in a view.


1. Open the _CommentsForPhoto.cshtml partial view.

2. In the _CommentsForPhoto.cshtml partial view, add an if statement to ensure that the _Create
partial view is only displayed if the request is authenticated.

3. If the request is not authenticated, render a link to the Login action of the Account controller to
display the text To comment you must log in.

4. Save all the changes.


MCT USE ONLY. STUDENT USE PROHIBITED
11-22 Controlling Access to ASP.NET MVC 4 Web Applications

 Task 5: Test authorization.


1. Start the web application in debugging mode and then attempt to add a new photo to the web
application, without logging on to the application.

2. Without logging on to the application, view any photo in the application and attempt to add a
comment.

3. Log on to the web application by using the following credentials:

o User name: David Johnson

o Password: Pa$$w0rd

4. Add a comment of your choice to the photo by using the following information:
o Subject: Authenticated Test Comment

5. Stop debugging.

Results: At the end of this exercise, you will have authorized anonymous and authenticated users to
access resources in your web application.

Exercise 4: Building a Password Reset View


Scenario
Site visitors can now register as users of the Photo Sharing application and log on to the site so that they
can add photos and comments. However, they do not have the facility to change their password. In this
exercise, you will create a password reset page by using the membership services provider.

The main tasks for this exercise are as follows:

1. Add a Local Password Model class.

2. Add reset password actions.

3. Import the reset password view.

4. Add a link to the reset password view.


5. Test password reset.

 Task 1: Add a Local Password Model class.


1. Add a new class to the AccountModelClasses.cs file by using the following information:

o Scope: public
o Name of the class: LocalPassword

2. Add a new property to the LocalPassword class by using the following information:

o Scope: public

o Type: string

o Name: OldPassword

o Access: Read/Write

o Data type: Password

o Annotation: [Required]
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-23

o Display name: Current password

3. Add a new property to the LocalPassword class by using the following information:

o Scope: public

o Type: string

o Name: NewPassword

o Access: Read/Write

o Data type: Password

o Annotation: [Required]

o Display name: New password

4. Add a new property to the LocalPassword class by using the following information:

o Scope: public
o Type: string

o Name: ConfirmPassword

o Access: Read/Write
o Data type: Password

o Display name: Confirm new password

o Use the Compare annotation to ensure this property matches the NewPassword property.
5. Save all the changes.

 Task 2: Add reset password actions.


1. In the AccountController class, add a new enumeration by using the following information:

o Scope: public
o Name: ManageMessageId

o Values: ChangePasswordSuccess, SetPasswordSuccess

2. Add a new action to the AccountController class by using the following information:
o Scope: public

o Return type: ActionResult

o Name: ResetPassword
o Parameters: an optional ManageMessageId object named message.

3. If the message parameter is not null, set the ViewBag.StatusMessage property to Your password
has been changed.

4. Set the ViewBag.ReturnUrl property to the URL of the ResetPassword action.

5. Return a view named ResetPassword.

6. Add a new action to the AccountController class by using the following information:

o Scope: public

o Return Type: ActionResult


MCT USE ONLY. STUDENT USE PROHIBITED
11-24 Controlling Access to ASP.NET MVC 4 Web Applications

o Name: ResetPassword

o Parameter: a LocalPassword object named model

7. In the new ResetPassword action for the HTTP POST verb, set the ViewBag.ReturnUrl property to
the URL of the ResetPassword action.

8. Include an if statement to check whether the ModelState is valid.

9. If the ModelState is valid, create a new Boolean variable named changePasswordSucceeded, and
then add a try… catch block that catches all exceptions.

10. In the try block, change the user’s password by using the following information:
o Method: Membership.Provider.ChangePassword

o User name: User.Identity.Name

o Old password: model.OldPassword


o New password: model.NewPassword

o Store the result in changePasswordSucceeded

11. In the catch block, set the changePasswordSucceeded variable to false.

12. After the try…catch code block, if changePasswordSucceeded is true, redirect to the
ResetPassword action and pass the ManageMessageId.ChangePasswordSucccess value to the
message parameter.
13. If changePasswordSucceeded is false, add a model error to the ModelState object by using the
following information:

o Key: An empty string


o Message: The current password is incorrect or the new password is invalid

14. If the ModelState is not valid, return the current view and pass the model object so that the errors
can be corrected.
15. Save all the changes.

 Task 3: Import the reset password view.


1. Add the ResetPassword.cshtml file to the Views/Account folder from the following location:

o Allfiles (D):\Labfiles\Mod11\Account Views

 Task 4: Add a link to the reset password view.


1. Add a new link to the _MainLayout.cshmtl template view by using the following information:

o Position: After the link to the LogOff action

o Text: Reset
o Action: ResetPassword

o Controller: Account

2. Save all the changes.

 Task 5: Test password reset.


1. Start the web application in debugging mode, and then log on with the following credentials:

o User name: David johnson


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 11-25

o Password: Pa$$w0rd

2. Change the password from Pa$$w0rd to Pa$$w0rd2.

3. Stop debugging and close the open windows.

Results: At the end of this exercise, you will build a Photo Sharing application in which registered users
can reset their own password.

Question: In Exercise 3, when you tried to add a photo before logging on to the application,
why did ASP.NET display the Login view?

Question: How can you ensure that only Adventure Works employees are granted access to
the Delete action of the Photo controller?
MCT USE ONLY. STUDENT USE PROHIBITED
11-26 Controlling Access to ASP.NET MVC 4 Web Applications

Module Review and Takeaways


In this module, you discussed the various membership and role providers in ASP.NET 4.5. You compared
the benefits of using SimpleProviders and UniversalProviders with the benefits of using other
providers, for database-centric applications. However, for applications that need to use Windows for
authentication and authorization, you can use the existing role and membership providers. You also
viewed how to implement custom providers, to add functionalities such as password encryption to your
web application.

Review Question(s)
Question: What is the key difference between implementing authentication and
authorization in ASP.NET 4.5 from implementing the same in the previous versions of
ASP.NET?

Real-world Issues and Scenarios


When you create web applications, you may need to create custom providers because you do not want to
use the schema provided by Microsoft. However, you can use SimpleProviders to remove the need to
develop custom providers and reduce the effort required for building applications.
MCT USE ONLY. STUDENT USE PROHIBITED
12-1

Module12
Building a Resilient ASP.NET MVC 4 Web Application
Contents:
Module Overview 12-1 

Lesson 1: Developing Secure Sites 12-2 

Lesson 2: State Management 12-8 

Lab: Building a Resilient ASP.NET MVC 4 Web Application 12-14 

Module Review and Takeaways 12-18 

Module Overview
Security is always the top priority for web applications, because publicly accessible web applications are
usually the targets of different types of web attacks. Hackers use web attacks to access sensitive
information. To help prevent these attacks, you need to know how to use AntiXSS and request validation
for your web application. Using various security mechanisms, you can build a secure web application.
Another problem that developers usually face is the need to retain information amongst multiple
postbacks. You need to know how to use state management techniques to retain information for multiple
HTTP requests. Using state management, you can help reduce the need for users to reenter information
every time they need to place a request.

Objectives
After completing this module, you will be able to:

 Develop secure sites.

 Implement state management.


MCT USE ONLY. STUDENT USE PROHIBITED
12-2 Building a Resilient ASP.NET MVC 4 Web Application

Lesson 1
Developing Secure Sites
Web applications are often subject to security attacks. These attacks prevent the applications from
functioning properly, and try to access sensitive information stored in the underlying data store. ASP.NET
provides built-in protection mechanisms that help prevent these attacks. However, such mechanisms also
tend to affect the operation of your applications. You should know when to enable or disable the
protection mechanisms to avoid any impact on the functionalities of your application. You also need to
know how to use Secure Sockets Layer (SSL) to prevent unauthorized access to information during
information transmission.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe how to avoid malicious attacks that use cross-site scripting.


o Describe the common attack techniques used by malicious users.

o Describe how to disable attack protection in your MVC application.

o Decide whether to disable attack protection in various scenarios.


o Describe the role of Secure Sockets Layer in helping secure web applications.

Cross-Site Scripting
Cross-site scripting involves the malicious insertion
of scripts in a user session. Cross-site scripting
poses information to other websites, by using the
authentication information of users, without their
knowledge. For example, consider the code
inserted into a web application to maliciously post
messages on social networking websites, without
the knowledge of the users. When a script is
inserted into a web application, the script has full
access to the Document Object Model (DOM) of
the HTML. This access allows the attacker to create
fake input boxes in the application, create fake
users, and post fake information on the web application.

The cross-site scripting attack usually takes input from improperly escaped output. These scripting attacks
usually impact the query strings. For example, consider the following URL:
http://localhost/Default1/?msg=Hello

The following code shows how to access the msg query string parameter from a controller action.

public ActionResult Index(string msg)


{
ViewBag.Msg = msg;
return View();
}

The following code shows how to display the value of the query string parameter in a view.

<div class="messages">@ViewBag.Msg<div>
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-3

After running the preceding code samples, the application should display the resultant word, Hello. Now,
consider a scenario where the query string parameter msg is changed to a less benign value resulting in
the following URL:

http://localhost/Default1/?msg=<script>alert('pwnd')</script>

As a result, the script block included in the query string is displayed to users. In such cases, attackers can
inject malicious code into your application by using the value of a query string parameter.

ASP.NET includes request validation, to help protect the input values that are subject to cross-site
scripting attacks. However, attackers can bypass this mechanism by using encoding to subvert common
cross-site scripting filters. For example, here is the same query string, which is encoded:

http://localhost/Default1/?msg=Jon\x3cscript\x3e%20alert(\x27pwnd\x27)%20\x3c/script\x3e

You can modify the view class to use the @Ajax.JavaScriptStringEncode function to help prevent cross-
site scripting attacks, instead of directly using the input from query strings. The following line of code
shows how to use the @Ajax.JavaScriptStringEncode function.

<div class="messages">@Ajax.JavaScriptStringEncode(ViewBag.Msg)<div>

You can also import the AntiXSS library to check the query string content for possible attacks. The AntiXSS
library is part of the Web Protection Library, which was developed by Microsoft to detect more complex
web attacks, when compared to ASP.NET’s request validation.

Additional Reading: For more information on AntiXSS Library, go to


http://wpl.codeplex.com/

After importing the AntiXSS library in your MVC application, you can use the library to encode any output
content in HTML. The following code shows how to use the AntiXSS library.

@using Microsoft.Security.Application
<div class="messages">@Encoder.JavaScriptEncode(ViewBag.Msg)<div>

The code in the preceding sample illustrates how to encode input values by using the JavaScriptEncode
method of the AntiXSS library, when displaying output in HTML. This practice ensures that the input
values are safe for display.

Question: What causes cross-site scripting attacks?

Other Attack Techniques


In addition to cross-site scripting attacks, hackers
can use other types of attacks, including cross-site
request forgery and SQL injection attacks to
subvert web applications.
Cross-Site Request Forgery

Cross-site request forgery (CSRF) is an attack that


occurs when you open a URL in a web browser, by
using your user context, without knowing that you
are allowing attackers to make changes to your
system. For example, consider that your
application uses query strings to pass information
MCT USE ONLY. STUDENT USE PROHIBITED
12-4 Building a Resilient ASP.NET MVC 4 Web Application

to other applications. You receive an email message with a link such as the following:
<a href="http://localhost/Default1/?id=100">Click Me</a>

When you click the link, the action associated with the view runs on your web browser. Because, you are
an authenticated user in the application, the attacker can now access your system.

You can prevent CSRF by using the following rules:

1. Ensure that a GET request does not provide result if the user repeating the request by opening the
same URL again.

2. Use the GET requests only for retrieval and not for state modifications.

3. Prevent any data modifications that use the GET request.

The @Html.AntiForgeryToken() function helps protect your system from CSRF by using unique tokens
that are passed to the application along with requests. The @Html.AntiForgeryToken() function uses
not only a hidden form field but also a cookie value, making it more difficult to forge a request. The
following code shows how to use the @Html.AntiForgeryToken() function in a view to add a
randomized parameter to ensure that the page request submitted is not repeatable.

@using (Html.BeginForm())
{
@Html.AntiForgeryToken();
@Html.EditorForModel();
<input type="submit" value="Submit" />
}

The following code shows how to force Anti-Forgery token checking in a controller by using the
ValidateAntiForgeryToken attribute.

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Index(User user)
{
Return View();
}

Note the ValidateAntiForgeryToken() attribute in the preceding code sample. This attribute enables the
controller to check if the user input from the HTML form includes the token generated by the
@Html.AntiForgeryToken(), before accepting a request.
SQL Injection Attack

SQL injection attacks are similar to cross-site scripting attacks. However, the difference is that the user
input is used to create dynamic SQL, instead of HTML.. Observe the following line of code that obtains
information from DB using dynamic SQL.

cmd.CommandText = "select userID from Users where userID = '" +


Request.QueryString["user"] + "'";

Consider a scenario where an attacker modifies the query string as displayed in the following line of code.

user=1' AND 1=1 GO SELECT * from Users WHERE NOT 0='

The following line of code denotes the resultant SQL.

select userID from Users where userID = '1' AND 1=1 GO SELECT * from Users WHERE NOT
0=''
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-5

In this case, SQL returns everything from the Users table.

To prevent SQL injection attack, you should:

1. Validate user input.

2. Avoid using string concatenations to create dynamic SQL.

3. Use parameterized commands with dynamic SQL.

4. Store all sensitive and confidential information in encrypted formats.

5. Ensure that the application does not use or access the database with administrator privileges.

Question: What is the best way to prevent SQL injection attacks if you cannot avoid dynamic SQL?

Disabling Attack Protection


Request validation is an ASP.NET feature that
examines an HTTP request and determines if it
contains potentially dangerous content. Potentially
dangerous content can include any HTML markup
or JavaScript code in the body, header, query
string, or cookies of the request.

However, request validation can also cause the


application to function improperly by preventing
some input from entering the application page for
processing. Consider a situation in which your
application uses an HTML editor to generate
HTML code for user input, before saving the input
on the database. In this case, you may want to disable the request validation to allow the HTML editor to
function properly.

You can choose to disable request validation at different levels, such as, in the web.config file, on a
webpage, or in a specific HTML element.
The following code shows how to change the requestvalidation mode back to the state for version 2.0 for
your application in the web.config file.

<system.web>
<httpRuntime requestValidationMode="2.0" />
</system.web>

You can set the following values to the requestValidationMode attribute:

o 4.0 (the default). The HttpRequest object internally sets a flag, which indicates that request
validation should be triggered whenever any HTTP request data is accessed. This guarantees that
the request validation is triggered before data such as cookies and URLs are accessed during the
request. The request validation settings of the pages element in the configuration file or of
the @ Page directive in an individual page are ignored.

o 2.0. Request validation is enabled only for pages, not for all HTTP requests. In addition, the
request validation settings of the pages element in the configuration file or of
the @ Page directive in an individual page are used to determine which page requests to
validate.
MCT USE ONLY. STUDENT USE PROHIBITED
12-6 Building a Resilient ASP.NET MVC 4 Web Application

The value that you assign to this property is not validated to match a specific version of ASP.NET. Any
numeric value smaller than 4.0 (for example, 3.7, 2.9, or 2.0) is interpreted as 2.0. Any number larger
than 4.0 is interpreted as 4.0.

The following code shows how to add the ValidateInput(false) attribute to the controller, to disable
request validation for an action in a controller.

[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(string comment)
{
return View(comment);
}

The following code shows how to add the AllowHtml attribute to the Prop1 property of the model, to
disable request validation for this property.

[AllowHtml]
public string Prop1 { get; set; }

You should consider using attack protection techniques that:


o Have a minimum impact on the application.

o Involve minimum fields to accept HTML elements in the request validations.

Question: Describe a scenario when you would want to disable request validation?

Discussion: Scenarios for Disabling Attack Protection


Consider the following scenarios. In each case,
discuss whether the scenario requires you to
disable attack protection.

1. You are creating a photo sharing application.


You want to enable each user to discuss
photos and trips with the other users whom
they have marked as their friends. However,
you want to prevent other users from seeing
these discussions. You want to implement the
HTML editing control on all the comments
posted by users.

2. When MVC calls the GetImage action


method, you want to ensure that the ID in the query string is passed as a parameter.

3. In your photo sharing application, you need to use dynamic SQL to help obtain a list of photos that
are posted by users.

4. In your photo sharing application, you want to use a SQL stored procedure to obtain a list of friends
present in a specific location.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-7

Secure Sockets Layer


Secure Sockets Layer (SSL) is an application layer
protocol that helps:

o Encrypt content by using the public key


infrastructure (PKI) keys.

o Protect the content that is transmitted


between the server and client.

o Prevent unauthorized access of content


during transmission.

You can use SSL on views that accept user input, if


the input includes sensitive information, such as
credit card information and passwords. Using SSL
on such crucial views ensures the confidentiality of the content and the authenticity of the sender.
However, you may not be able to capture and analyze the HTML generated by your application and easily
detect if a user accesses the web application by using SSL. If you use SSL, it may impact the performance
because of the additional workload introduced to perform the encryption and decryption of the
transportations.

ASP.NET MVC 4 includes the RequireHttps attribute that enables you to use SSL on the views that involve
sensitive information. The RequireHttps attribute redirects users to the SSL link, if they request a view
using normal HTTP. The following code shows how to add the RequireHttps attribute in your controller
class and action methods to require a user to use the SSL protocol.

[RequireHttps]
public class Controller1
{
[RequireHttps]
public ActionResult Edit()
{
}
}

You can use the RequireHttps attribute at the controller level or action level. This flexibility allows you to
choose SSL when required in your web application.

Note that web servers require you to configure the PKI certificate so that the server accepts SSL
connections. SSL certificates need to be purchased from a certificate authority (CA). During application
development, you can use the self-sign certificate to simplify the configuration process.

Question: What action is required to be performed on the web server before implementing SSL?
MCT USE ONLY. STUDENT USE PROHIBITED
12-8 Building a Resilient ASP.NET MVC 4 Web Application

Lesson 2
State Management
While developing applications, you may want to create functions that require information to be retained
across requests. For example, consider an application in which you need to first select a customer and
then work on the order relevant to the customer. HTTP is a stateless protocol. Therefore, ASP.NET includes
different state management techniques to help store information for multiple HTTP requests. You need to
know how to configure and scale state storage mechanisms to support web server farms.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the benefits of storing state information.

o List the options for storing state information.

o Configure state storage.


o Describe the scalability of state storage mechanisms.

o Store and retrieve state information in an MVC 4 web application.

Why Store State Information?


HTTP is as a stateless protocol that does not relate
submitted requests to each other. HTTP treats
each request as a new request. However, this
independence among the requests affects business
applications that have information dependencies
amongst functions. For example, consider that you
can access the Update Address function only after
you provide the user logon information. The page
that handles the User Login function is different
from the page that handles the Update Address
function. Therefore, these functions require two
separate HTTP requests. With the default HTTP
implementation, the server may lose the result of the logon request, when a user accesses the Update
Address page. ASP.NET implements the session management technology to retain dependent information
for multiple HTTP requests.

Session management allows web applications to store values for multiple HTTP requests, regardless of
whether the request is directed to the same page or multiple pages. The session management process
involves two techniques—server-side techniques and client-side techniques, to retain user information
pertinent to multiple HTTP requests.

Client-Side Session Management

Client-side session management technologies include:

o View state

o Control state
o Hidden fields

o Cookies
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-9

o Query strings

Client-side session management technologies help retain dependent information when a user triggers the
HTTP POST action by clicking a button on a webpage. Then, when a page is rendered, the server
generates the dependent information.

Server side Session Management

Server-side options provide higher security for storing page information than the client-side options,
because information retained by using client-side options is not visible to the client system. However,
server-side options require more web server resources such as memory for storing data. This additional
usage of resources can lead to scalability issues, if the size of the information to be stored is large.

ASP.NET provides the following server-side session management options:

o Application state
o Session state

o TempData

o Profile properties

o Database support

Additional Reading: For more information about the pros and cons of session
management techniques, go to http://msdn.microsoft.com/en-us/library/z1hkazw7.aspx

Question: Why do you need to implement session management?

State Storage Options


You can store values for requests, and the
subsequent requests related to the previous
requests, by using the TempData object. By
default, the TempData object uses the Session
variable to store information relevant to requests.
You can use the TempData object to access
session information, instead of using the Session
variable. The TempData object includes logic to
handle redirection of information to different
views.

You can use the sessionState element in the


web.config file, to define the repository that
should retain the session state information. You can also use the sessionState element to turn off session-
state support. The following list describes some of the commonly used session state storage options:
o The InProc mode. Stores the session in the ASP.NET process.

o The StateServer mode. Stores session information in dedicated services running on the web server
or on a dedicated server. It does not store information on Internet Information Services (IIS).

o The SQLServer mode. Stores the session information in a SQL Server database.

o The Off mode. Deactivates the session state support in your application.
MCT USE ONLY. STUDENT USE PROHIBITED
12-10 Building a Resilient ASP.NET MVC 4 Web Application

Question: Consider that you have a web server that contains multiple web servers. You want to allow
users to access the same state information for all the web servers they access. In this case, which state
storage mode should you used?

Configuring State Storage


You can configure state storage to use the
StateServer and SQLServer modes.

The StateServer Mode

You need to run the ASP.NET state service on the


server used for storing session information, before
using the StateServer mode. The ASP.NET state
service is installed as a service when ASP.NET and
the .NET Framework are installed as part of
Microsoft .NET Framework. The ASP.NET service is
installed at the following location:
systemroot\Microsoft.NET\Framework\versionNum
ber\aspnet_state.exe

To configure an ASP.NET application for using the StateServer mode, you should perform the following
steps:

o In the web.config file of the application, set the mode attribute of the sessionState element to
StateServer and set the stateConnectionString attribute to tcpip=<serverName>:42424.

To configure StateServer support in your application, you should perform the following steps:

1. Run the services.msc file.


2. Set the ASP.NET state service Startup Type attribute to automatic, and right-click the service.

3. Open the web.config file, identify the sessionState element, and then update the code.

Consider that a session state is stored on the SampleStateServer remote computer. The following code
shows how to configure the StateServer mode.

<configuration>
<system.web>
<sessionState mode="StateServer"
stateConnectionString="tcpip=SampleStateServer:42424"
cookieless="false"
timeout="20"/>
</system.web>
</configuration>

Note: The objects stored in session state must be serializable, if the mode is set to
StateServer or SQLServer.

The SQLServer Mode

Before using the SQLServer mode, you need to install the ASP.NET session state database on the SQL
Server. You can install the ASP.NET session state database by using the aspnet_regsql.exe tool. To
configure an ASP.NET application for using the SQLServer mode, you should perform the following steps:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-11

o In the web.config file of the application, set the mode attribute of the sessionState element to
SQLServer and set the sqlConnectionString attribute to a connection string for your SQL Server
database.

To create the session state database on SQL Server, you should perform the following steps:

1. Open the command prompt with administrative privileges.

2. From the <systemroot\Microsoft.NET\Framework\versionNumber> folder on your web server, run the


Aspnet_regsql.exe tool.

3. Open the web.config file, identify the sessionState element, and then update the code.
Consider that a session state is stored on the SampleSqlServer SQL Server. The following code shows
how to configure the SQLServer mode.

<configuration>
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Integrated Security=SSPI;data
source=SampleSqlServer;" />
</system.web>
</configuration>

Additional Reading: For more information on session state management in Azure, go to:
http://msdn.microsoft.com/en-us/library/windowsazure/gg185668.aspx

You can customize the behavior of the aspnet_regsql.exe tool, by using switches such as -E, -S, and -d.
You can use these switches to change the name of the default ASPState database.

Additional Reading: For more information about the aspnet_regsql.exe tool, go to:
http://msdn.microsoft.com/en-us/library/ms229862(v=vs.100).aspx

Question: Which tool should you install before using the SQLServer mode?

Scaling State Storage Mechanisms


The StateServer and SQLServer modes help access
the state storage on a web server. These modes
allow multiple servers to access state storage
information. This process distributes the web
workload among multiple web servers.

You can implement partitioning of session state to


allow multiple state servers or multiple SQL Server
databases to handle session state information. You
can apply the logic for partitioning, by
implementing the IPartitionResolver interface in
your application. You need to configure the
web.config file to control the session management
engine. Configuring the session management engine helps you use the logic to identify the appropriate
web server on which you can store information. The following code shows how to configure the
web.config file, to enable the IPartitionResolver interface.
MCT USE ONLY. STUDENT USE PROHIBITED
12-12 Building a Resilient ASP.NET MVC 4 Web Application

<configuration>
<system.web>
<sessionState
mode="SQLServer"
partitionResolverType=
"PartitionResolverClass" />
</system.web>
</configuration>

You can use the IPartitionResolver interface only when the session state uses the SQLServer or
StateServer modes. The partitionResolverType attribute specifies the class that is implementing
IPartitionResolver. In the preceding code sample, note that no connection string is specified by using
the sqlConnectionString or stateConnectionString attribute as the partitionResolverType class will
take over and manage the connectionstring to be used for each request.

Additional Reading: For more details about implementing state partitioning, go to:
http://msdn.microsoft.com/en-us/magazine/cc163730.aspx#S7

Question: When should you implement the IPartitionResolver interface?

Demonstration: How to Store and Retrieve State Information


In this demonstration, you will see how to:

o Store and retrieve user preferences from session state in controller actions.

Demonstration Steps
1. Add a new action to the HomeController.cs file by using the following information:

o Scope: public
o Return type: ContentResult

o Name: GetBackground

o Parameters: None
2. In the GetBackground action, create a string variable named style, and then add an if…else
statement that checks whether the Session["BackgroundColor"] variable is null.

3. If the Session["BackgroundColor"] variable is not null, use it to set the style string, by using the
following information:

o Method: String.Format

o Format: background-color: {0};

o Argument: Session["BackgroundColor"]

4. If the Session["BackgroundColor"] variable is null, set the style string to background-color:


#dc9797;

5. Return the style string by using the Content method.

6. Add a new action to the HomeController.cs file by using the following information:

o Scope: public
o Return type: ActionResult
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-13

o Name: SetBackground

o Parameters: a string variable named color

7. In the SetBackground action, set the Session["BackgroundColor"] variable to the color variable,
and then return the Index view.

8. Open the Index.cshtml file from the Views\Home folder.


9. At the end of the view file, add a P element with the content Choose a background color:

10. In the P element you just added, after the text, render a link to the SetBackground action, by using
the following information:

o Helper: Html.ActionLink

o Link text: Pink

o Action: SetBackground
o Color parameter: #dc9797

11. After the first link, render a second link to the SetBackground action, by using the following
information:
o Helper: Html.ActionLink()

o Link text: Blue

o Action: SetBackground
o Color parameter: #82bbf2

12. In the _SiteTemplate.cshtml file, use the GetBackground action to set the style attribute of the
BODY element, by using the following information:

o Helper: Html.Action()

o Action: GetBackground

o Controller: Home
13. In the OperaContoller.cs file, remove the OutputCache annotation from the Index action method

14. Start the web application in debugging mode.

15. Browse to the Cosi Fan Tutte opera page to verify that the pink and blue background colors have
been applied to all the pages of the web application.

16. Stop debugging, and then close Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
12-14 Building a Resilient ASP.NET MVC 4 Web Application

Lab: Building a Resilient ASP.NET MVC 4 Web Application


Scenario
The senior developer has asked you to implement the following functionality in your Photo Sharing web
application.

o Any visitor of the application, including anonymous users, should be able to mark a photograph
as a favorite.

o If a user has marked a favorite, a link should be available to display the favorite photo.

o Favorite photos should be displayed in the slideshow view.

Objectives
After completing this lab, you will be able to:

o Store a setting for an anonymous or authenticated user in session state.

o Check a user preference when rendering an action link.

Render a webpage by checking state values in the application.Lab Setup


Estimated Time:

30 minutes

Virtual Machine: 20486A-SEA-DEV11

User name: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

Before starting the lab, you need to perform the following step:

 Enable the Allow NuGet to download missing packages during build option in Package
Manager.

Exercise 1: Creating Favorites Controller Actions


Scenario
You have been asked to build functionality that stores the favorite photos of the visitors in the session
state of the web application. After users add photos to their favorites, they will be able to view a slideshow
of all the photos they selected as favorites.

In this exercise, you will:

o Create the Favorites Slideshow action.

o Create the Add Favorite action.

The main tasks for this exercise are as follows:

1. Create the Favorites Slideshow action.

2. Create the Add Favorite action.

 Task 1: Create the Favorites Slideshow action.


1. Start the virtual machine, and log on with the following credentials:

o Virtual machine: 20486A-SEA-DEV11

o User name: Admin


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-15

o Password: Pa$$w0rd

2. Open the PhotoSharingApplication.sln file from the following location:

o File location: Allfiles (D:)\Labfiles\Mod12\Starter


\PhotoSharingApplication

3. In the PhotoController.cs code file, add a new action by using the following information:
o Scope: public

o Return type: ActionResult

o Name: FavoritesSlideShow

4. In the FavoritesSlideShow action, create and instantiate a new enumerable list of Photo objects
named favPhotos.

5. Create a new enumerable list of integers named favoriteIds. Set this list to be equal to the
Session["Favorites"] variable by casting this variable as a list of integers.

6. If favoriteIds is null, set favoriteIds to be a new enumerable list of integers.

7. Create a new Photo object named currentPhoto. Do not instantiate the new object.
8. Create a new foreach code block that loops through all the integers in the favoriteIds list.

9. For each integer in the favoriteIds list, obtain the Photo object with the right ID value by using the
context.FindPhotoById() method. Store the object in the currentPhoto variable.

10. If the currentPhoto variable is not equal to null, then add currentPhoto to the favPhotos list.

11. At the end of the FavoritesSlideShow action, return the SlideShow view and pass the favPhotos list
as a model class.

12. Save all the changes.

 Task 2: Create the Add Favorite action.


1. Add a new action to the PhotoController.cs file by using the following information:

o Scope: public
o Return type: ContentResult

o Name: AddFavorite

o Parameter: an integer named PhotoId


2. Create a new enumerable list of integers named favoriteIds. Set this list to be equal to the
Session["Favorites"] variable, by casting the variable as a list of integers.

3. If favoriteIds is null, set favoriteIds to be a new enumerable list of integers.

4. Add the PhotoId value to the favoriteIds list of integers.

5. Set the Session["Favorites"] variable to equal the favoriteIds variable.

6. Return HTML content by using the following information:


o Method: Content()

o Content: The picture has been added to your favorites

o Content type: text/plain


o Encoding: System.Text.Encoding.Default

7. Save all the changes.


MCT USE ONLY. STUDENT USE PROHIBITED
12-16 Building a Resilient ASP.NET MVC 4 Web Application

Results: After completing this exercise, you will be able to create controller actions that store values in the
session state of the web application, and retrieve values from the same session state.

Exercise 2: Implementing Favorites in Views


Scenario
You have created the necessary controller actions to implement favorite photos. Now, you should
implement the user interface components to display a control for adding a favorite. If a user has favorites,
you should display a link to the FavoritesSlideShow action.

In this exercise, you will:

o Add an AJAX action link in the Photo Display view.

o Add a link and update the site map.


The main tasks for this exercise are as follows:

1. Add an AJAX action link in the Photo Display view.

2. Add a link and update the site map.


3. Test favorites.

 Task 1: Add an AJAX action link in the Photo Display view.


1. Open the Display.cshtml view for the Photo controller.

2. At the end of the <div class="photo-metadata"> element, insert a new <div> element with the ID,
addtofavorites.

3. In the DIV element, include the Ajax.ActionLink() helper to render a link to the AddFavorite action
by using the following information:
o Text: Add this photo to your favorites

o Action: AddFavorite

o Controller: Photo
o Route values: Pass the Model.PhotoID value to the PhotoId action parameter.

o Pass a new AjaxOptions object.

o Update target ID: addtofavorites


o HTTP method: GET

o Insertion mode: Replace

4. Save all the changes.

 Task 2: Add a link and update the site map.


1. Open the _MainLayout.cshtml view for editing.

2. After the breadcrumb trail, add a Razor @if statement to ensure that the Session["Favorites"]
variable is not null.

3. If the Session["Favorites"] variable is not null, render a <div> element with the ID, favorites-link.

4. In the DIV element, render a link to the FavoritesSlideShow action by using the following
information:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 12-17

o Helper: Html.ActionLink()

o Link text: Favorite Photos

o Action: FavoritesSlideShow

o Controller: Photo

5. Save all the changes.

6. Open the Mvc.sitemap file.

7. Add a new site map node to the Mvc.sitemap file by using the following information:

o Title: Favorites
o Visibility: SiteMapPathHelper,!*

o Controller: Photo

o Action: FavoritesSlideShow

8. Save all the changes.

 Task 3: Test favorites.


1. Start the web application in debugging mode.

2. Add three photos of your choice to your favorite photos.


3. Browse to the home page and click the Favorite Photos link.

4. Stop debugging and close Visual Studio.

Results: After completing this exercise, you will be able to :

Create the user interface components for the favorite photos functionality.

Test the functionality of the user interface components.

Question: In this lab, you stored the list of favorite photos in the session state. While testing,
your manager notices that authenticated users lose their favorite photos list whenever they
close their browser. Where would you store a list of favorites for each authenticated user so
that the list is preserved whenever a user logs on to the web application?

Question: How would you create a view of favorite photos with the card-style presentation
users see on the All Photos page?
MCT USE ONLY. STUDENT USE PROHIBITED
12-18 Building a Resilient ASP.NET MVC 4 Web Application

Module Review and Takeaways


Web applications are usually subject to different kinds of attacks. These attacks enable attackers to access
sensitive information stored in the database and to perform malicious actions. You can use AntiXSS and
request validation, to protect the applications from web attacks. You can also use state management
techniques to store information for multiple HTTP requests to help avoid users from typing the same
information more than once.

Real-world Issues and Scenarios


While implementing web applications, you may want to use a rich format input editor, to enable users to
format the input within text boxes. Therefore, you may need to disable request validation, to enable
ASP.NET to capture and process user input.

Complex business functions usually involve multiple views. Such functions can pose problems because
information must be shared across multiple views. Session state management helps resolve these
problems, because it enables retaining information pertinent to multiple views.
MCT USE ONLY. STUDENT USE PROHIBITED
13-1

Module13
Using Windows Azure Web Services in ASP.NET MVC 4 Web
Applications
Contents:
Module Overview 13-1 

Lesson 1: Introducing Windows Azure 13-2 

Lesson 2: Designing and Writing Windows Azure Services 13-6 

Lesson 3: Diagnostic Logs 13-9 

Lesson 4: Consuming Windows Azure Services in a Web Application 13-12 

Lab: Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications 13-15 
Module Review and Takeaways 13-22 

Module Overview
Developers often place all application logic in the application code. While this practice works for small-
scale applications, it may not work for applications with a large number of users. An alternate approach is
to make the code modular, by modifying the applications to use web services.
Windows Azure is a cloud-based platform that allows you to host web applications and services. You can
use Windows Azure to make your web application more scalable and manageable. To effectively use
Windows Azure, you need to know how to write a web service. You also need to know how to call a web
service from a web application or from other applications, such as a mobile device application.

Objectives
After completing this module, you will be able to:

o Describe Windows Azure.

o Design Windows Azure services.

o Call Windows Azure services from a web application.


MCT USE ONLY. STUDENT USE PROHIBITED
13-2 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

Lesson 1
Introducing Windows Azure
Windows Azure is a Microsoft platform that helps address specific needs of hosting web applications, such
as creating scalable and highly available web applications. Windows Azure enables you to host services,
applications, and databases. Windows Azure also helps you create cost-effective and secure web offerings
that suit businesses of various types and sizes. You need to know how to host applications, services, and
databases on Windows Azure to increase the scalability, flexibility, and availability of your web offerings.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe Windows Azure.

o Describe the hosting services available in Windows Azure.

o Explain how to host web applications in Windows Azure.


o Describe the data storage options offered by Azure Storage and SQL Azure.

o Determine when to use Windows Azure.

What Is Windows Azure?


The Windows Azure application platform provides
comprehensive services to host applications, files,
media, and databases. Windows Azure is a flexible
platform that supports businesses of various sizes,
helps reduce the effort to develop scalable web
applications, and decreases development costs.
Microsoft hosts Windows Azure in multiple data
centers, across the United States, Europe, and Asia.

Windows Azure provides the following categories


of services:

o Execution Models. Provides three different


execution models—virtual machines
(VMs), web applications, and cloud services, for running applications

o Windows Azure Identity Management. Includes Windows Azure Active Directory and Access
Control services to help manage identities, authentication, and authorization in a web
application.

o Data Management. Includes services to host and manage relational database and Blob content
o Business Analytics Solutions. Includes services that helps perform business information analysis by
using SQL Server Reporting Services and Apache Hadoop. Business analytics solutions support all
the data stores distributed across multiple Windows Azure data centers.

o Networking Services. Includes services that provide networking capabilities to securely connect
multiple services in Microsoft data centers.

o Messaging with Service Bus. Includes services that enable applications to communicate on
Windows Azure.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-3

o Media Management. Includes services that bring content geographically closer to users and help
store large volumes of content outside the web application. Storing content outside the
application helps improve the performance of the application.

Question: What are the benefits of hosting applications on Windows Azure?

Benefits of Hosting Services in Windows Azure


Windows Azure allows you to configure
application logic to run as services. Services
denote a portion of the application code, which
performs specific business logic based on user
inputs, such as retrieving product pricing
information. These services usually do not require:

o Administrator access to Windows

o Complete access to the user interface of


Windows

o Access to other functionalities that allow


interacting with users, which VMs
facilitate

Windows Azure cloud services do not use a dedicated VM. However, Windows Azure cloud services use a
role-based model that relies on the VM to host applications. The role-based model includes a web role
and a worker role. Each role is a VM that is preconfigured to support automated deployment and
monitoring of applications.

While deploying these cloud services, you need not create the VM. You only need to upload a
configuration file to direct Windows Azure how to configure the application. You need not upload this
configuration file on the VM; you can upload the file on to the staging environment. Then, the Windows
Azure management agent copies this configuration information to the production servers.

Windows Azure cloud services do not retain state data in a role, because the VM has no permanent
storage. You can either use other services to retain state data or design the application in a stateless
manner.

Question: What are the benefits of using Windows Azure cloud services?
MCT USE ONLY. STUDENT USE PROHIBITED
13-4 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

Benefits of Hosting Web Applications in Windows Azure


Hosting web applications on Windows Azure is
similar to hosting services, because both cases use
virtual machines. However, unlike services, web
applications provide full access to the IIS instance
assigned to your application. You can use a web
application to host server-side web applications
such as ASP.NET MVC, static content, open source
applications, and other file-based web
applications.

Hosting web applications in Windows Azure


provides the following benefits:

o It automates the process of deploying


applications to different hosting servers.

o It simplifies the process of scaling up and down as your application usage grows or contracts.

Question: What is the key benefit of using Windows Azure web applications?

Windows Azure Storage Services


Windows Azure includes the following storage
services:

o Azure Storage. This is also known as


binary large object (Blob) service. You can
use Azure storage to store objects or file
content relevant to your web
applications. Each Blob includes a unique
URL that allows applications to directly
access content in the Azure Storage.

o SQL Azure. This is a relational database


service on the Windows Azure platform.
Most applications use SQL Azure as the
database engine for storing application data. You can use SQL Azure to:
 Increase the availability and scalability of the data store and any applications built on that
data store
 Reduce investments on hardware
o SQL Database. This service helps maintain a set of VMs that allow hosting a database. Microsoft
manages the database of an application, but does not provide access to the Windows console.
SQL databases include clustering functionalities that increase the data availability in multiple SQL
Server hosts.

Question: What are the benefits of using SQL Azure?


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-5

Discussion: Windows Azure Scenarios


Consider the following scenarios. In each case,
discuss which Azure service will best suit the
scenario.

1. You are creating a photo sharing application,


and you want to enable each user to discuss
the photos with their friends. You need to
ensure that the photos in your application do
not occupy large disk storage in your web
farm. Therefore, you do not want to store the
photos, which the users upload, in your
application database or server.

2. You are creating a Windows-based business


application. This application requires a centralized database that users can access by using the
Internet.

3. You are writing business logic for your company, and both public and internal applications want to
use this business logic.

4. You are creating an application that uses a third-party component. The third-party component
requires full access to the Windows console.
MCT USE ONLY. STUDENT USE PROHIBITED
13-6 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

Lesson 2
Designing and Writing Windows Azure Services
Windows Azure cloud services provide a scalable hosting platform for web applications. You can use
Windows Azure cloud services to host Windows Communication Foundation (WCF) services. Hosting such
services helps transfer key business logic from a web application onto a cloud platform. Hosting services
on Windows Azure enables multiple applications to access the services from anywhere, anytime. You need
to know about the life cycle of the Windows Azure services to add and modify code during the different
stages of application development. You also need to know how to use diagnostic tools in Windows Azure,
to identify the problems that occur in your application.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe Windows Azure Visual Studio project templates.


o Write code to create a web service.

o Describe the life cycle of a web service.

o Deploy a web service.


o Debug a Windows Azure web service.

Windows Azure Visual Studio Project Templates


Microsoft Visual Studio provides project templates
such as Windows Azure project, web role project,
and worker role project, which help create
applications and host them on Windows Azure.
Windows Azure Project

The Windows Azure project template facilitates


role-based projects. This template includes service
definition and service configuration files.
Web Role Project

The web role project provides templates for the


following web roles:

o ASP.NET (Web Forms)

o ASP.NET MVC4

o ASP.NET MVC3

o ASP.NET MVC2

o WCF Service

o Silverlight Business Application


Worker Role Project
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-7

A worker role performs background processing by communicating with storage services and other
Internet-based services, without interacting with end users. Worker role projects support long running
services and other logic that applications comprise.

Question: Which project template should you use to create Windows Communication Foundation (WCF)
services?

Coding a Web Service


Modular programming plays an important role in
developing applications, because it enables you to
reuse application code. Reusing services enables
cross-application sharing of business logic.

To code a web service, you need to perform the


following steps:

1. Open Microsoft Visual Studio and create a


Windows Azure WCF Service web role project.

2. Modify the interface code in IService1.cs to


define the interface-level functionalities. The
following code shows how to modify the
interface code in IService1.cs.

[ServiceContract]
public interface IService1
{
[OperationContract]
string GetHelloWorld();
}

3. Modify the service code in Service1.cs to add service-level logic. The following code shows how to
modify the service code in IService1.cs.

public class Service1 : IService1


{
public string GetHelloWorld()
{
return "Hello Azure
}
}

4. Press F5 to test the services locally using the Windows Azure emulator. This practice ensures that the
services function appropriately, before deploying the services on Windows Azure.

Question: Why should you test the service code, before deploying the service on Windows Azure?
MCT USE ONLY. STUDENT USE PROHIBITED
13-8 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

The Life Cycle of a Service


The RoleEntryPoint class defines the method that
Windows Azure hosts call, based on the
development stage of the application. The
RoleEntryPoint class also helps define the
functions that add logic to services. WCF services
may sometimes inherit the RoleEntryPoint class;
however, worker roles services mandatorily inherit
the RoleEntryPoint class.

The RoleEntryPoint class helps define the


following three life-cycle methods:

o OnStart. Windows Azure triggers this


method when the application starts.

o OnStop. Windows Azure triggers this method when the application stops.

o Run. Windows Azure triggers this method to start long running processes.

The OnStart and OnStop methods return a Boolean value to the Windows Azure host, when a function
completes running. If the OnStart and OnStop methods returns false, Windows Azure stops the
application from running. You should ensure that the OnStart and OnStop methods always return true,
to ensure that your application continues running.

Windows Azure does not trigger the OnStop method if a process terminates because of exceptions.
Windows Azure treats all uncaught exceptions as unhandled exceptions that cause processes to terminate.

While defining the RoleEntryPoint class, you must:

o Call the Application_Start method after the RoleEntryPoint.OnStart method finishes.

o Call the Application_End method before calling the RoleEntryPoint.OnStop method.

Question: When should you use the RoleEntryPoint class?

Deploying a Web Service


When you create an application project, the
system creates two files—ServiceDefinition.csdef
and ServiceConfiguration.cscfg. These files control
the Windows Azure instance that helps run the
application.

The following list describes the


ServiceDefinition.csdef and
ServiceConfiguration.cscfg files:

o ServiceDefinition.csdef. It helps define


roles for your application.

o ServiceConfiguration.cscfg. It is a hosted
file that helps configure the instance assigned for your application. For example, you can use
ServiceConfiguration.cscfg to configure the number of instances for your application.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-9

Two copies of ServiceConfiguration.cscfg are available, a cloud version and a local version, which you can
use to locally test the services before deploying on Windows Azure. You can manually update the details
in the ServiceConfiguration.cscfg file by using the Windows Azure property window.

Additional Reading: For more information about configuring a Windows Azure project,
go to: http://msdn.microsoft.com/en-us/library/ee405486.aspx

You can use the Package functionalities in Microsoft Visual Studio, to generate the services package. Then
you need to upload the services package on the staging environment of the Windows Azure platform, by
using the management portal. Package files usually have the .cspkg extension. When you upload the
package, you also need to upload the ServiceConfiguration.cscfg file. The ServiceConfiguration.cscfg file
directs Windows Azure on how to configure the hosting environment.
After uploading and deploying the package on the Windows Azure platform, you can access the service
by using the following URL:

http:// <guid>.cloudapp.net/<service file name>

Question: When should you use the ServiceConfiguration.cscfg file?

Debugging a Windows Azure Web Service


You can debug a Windows Azure application by
using the following tools:

o Diagnostic log
o IntelliTrace

o Remote Desktop

Diagnostic Logs

To debug Windows Azure web services, you need


to include the Diagnostic module in the
ServiceDefinition.csdef file. The Diagnostics
module helps access the Application Programming Interface (API), to perform diagnosis. The following
code shows how to include the Diagnostic module in the ServiceDefinition.csdef file.

<?xml version="1.0" encoding="utf-8"?>


<ServiceDefinition name="MyHostedService"
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole1">
<Imports>
<Import moduleName="Diagnostics" />
</Imports>
</WebRole>
</ServiceDefinition>

Running the code in the preceding sample displays diagnostic data.

Additional Reading: For more information about the data types that you can configure
your application to collect, go to: http://www.windowsazure.com/en-us/develop/net/common-
tasks/diagnostics/
MCT USE ONLY. STUDENT USE PROHIBITED
13-10 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

To enable your application to monitor additional information, you need to add the following lines of code
in the OnStart method of your application file.

var config = DiagnosticMonitor.GetDefaultInitialConfiguration();


config.WindowsEventLog.DataSources.Add("System!*");
DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"
, config);

The lines of code in the preceding sample help:

o Configure the Windows Azure service to capture the Windows Event log. The Windows Azure
engine generates this log when the service runs.

o Save the Windows Event log to the log files that are stored in LocalStorage. You can access the
log files by using the Windows Azure storage explorer and download the files.
IntelliTrace

IntelliTrace is a feature in Windows Azure, which allows you to:


o Access event log information.

o Debug the application.

Note: The virtual machines in Windows Azure always run 64-bit operating systems. If you
have Visual Studio 2010 Ultimate installed on a machine that runs a 32-bit operating system, you
must install this Intellitrace hotfix. This Intellitrace hotfix helps install the 64-bit IntelliTrace
components.
To install the Intellitrace hotfix, go to: http://archive.msdn.microsoft.com/KB983301

You can enable IntelliTrace, while you deploy or package services. You can also configure services to
enable IntelliTrace for specific events. This configuration helps capture detailed information such as
Windows log and system configurations.

The following image displays the IntelliTrace Settings dialog box.

FIGURE 13.1:THE INTELLITRACE SETTINGS DIALOG BOX


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-11

IntelliTrace also helps in problem diagnosis. You can simulate some problems that you may encounter
while running web services. Then, you can download the IntelliTrace logs from Windows Azure by using
the Server Explorer. These logs help access and understand the problems.

The following image displays some IntelliTrace logs.

FIGURE 13.2:INTELLITRACE LOGS


Remote Desktop

When you are using Windows Azure Cloud Services, you can configure the application to provide remote
desktop access to an instance. This configuration provides full access to Windows event logs.

You can enable remote desktop access by navigating to the Properties dialog box of your application
and selecting Remote Desktop Configuration. Then, you should specify the credentials that a user
should use to access the remote desktop console. All instances of your application that run on Windows
Azure will use these credentials.
After publishing the application on Windows Azure, you can access the remote desktop by using the
management portal.

Question: What is the difference between using diagnostic logs and IntelliTrace?
MCT USE ONLY. STUDENT USE PROHIBITED
13-12 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

Lesson 3
Consuming Windows Azure Services in a Web Application
After completing the setup and deployment of the Windows Azure Services, you need to add logic to
your application, to use the services from ASP.NET MVC application and HTML code. You can call a
Windows Azure web service using server-side code or by using client-side code.

Lesson Objectives
After completing this lesson, you will be able to:

o Call a Windows Azure web service using server-side code.

o Explain how to call a Windows Azure web service using jQuery and JSON.
o Call a Windows Azure web service using jQuery.

Calling a Windows Azure Service Using Server-Side Code


You can call web services hosted on Windows
Azure in the same manner with which you call
standard web services or WCF services. To call a
web service, you need to:

1. Add the service reference in your application.

2. Use the generated proxy class.


To add a service reference in an application, you
need to use the URL of the service. You can
deploy a service in the production environment,
by using the following URL format:

http://<urlname>.cloudapp.net/<servicename>.sv
c
You can deploy a service in the staging environment, by using the following URL format:

http://<guid>.cloudapp.net/<servicename>.svc

To use the WCF service hosted on Windows Azure, from your MVC application, you need perform the
following steps:

1. In the Solution Explorer pane, right-click your project name and click Add Service Reference.

2. Enter the URL of the WCF service, and click Go.

3. Define the local namespace of the service and click Add Service Reference.

4. Add the following code to an MVC controller class to use the generated proxy class and call the web
service created earlier in this module.

public ActionResult Index()


{
Service1Client client = new Service1Client();
GetHelloWorldRequest request = new GetHelloWorldRequest();
GetHelloWorldResponse response = client.GetHello(request);
ViewBag.Response = response.GetHelloWorldResult;
return View();
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-13

Question: What is the key difference between deploying a service in the production environment and
deploying a service in the staging environment?

Calling a Windows Azure Service Using jQuery


You can enable the jQuery ajax function to call
WCF services, by configuring the services to
accept POST requests in the JavaScript Object
Notation (JSON) data format. JSON is a data
format that facilitates requests and responses
between client and server systems. The following
code shows how to configure services to accept
POST requests in the JSON data format.

[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
string GetHelloWorld();
}

You can use the ajax function in jQuery to submit requests to WCF services. While adding the ajax
function you need to specify parameters such as type, url, contentType, and dataType. The following
code shows how to use the ajax function in jQuery, to call a Azure-hosted WCF service.

// Function to call WCF Service


function CallService() {
$.ajax({
type: 'POST',
url: 'http://<guid>.cloudapp.net/service1.svc/GetHelloWorld',
data: '{}',
contentType: 'application/json; charset=utf-8',
dataType: “json”,
processdata: true,
success: function(msg) {
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function ServiceFailed(result) {
}
function ServiceSucceeded(result) {
result=result.GetHelloWorldResult;
}

After successfully calling the WCF service, the code in the preceding sample trigger the
ServiceSucceeded callback function.

Question: What is the commonly used data format used to communicate with WCF Services, from
jQuery?
MCT USE ONLY. STUDENT USE PROHIBITED
13-14 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

Demonstration: How to Call a Windows Azure Service Using jQuery


In this demonstration, you will see how to call a web service using jQuery.

Demonstration Steps
1. In the Index.cshtml view for the Home controller, after the welcome paragraph, add a FORM
element.

2. In the FORM element, create a new INPUT element by using the following attributes:

o Type: button

o Value: “Get Latest Quote”

o Name: “GetLatestQuote”

o onClick: “callWebService();”

3. In the FORM element, create a P element with the ID quote-display.

4. After the FORM element, create a SCRIPT element with the type, text/javascript.

5. In the SCRIPT element, create a JavaScript function named callWebService with no parameters.
6. Formulate an absolute URL to the ~/WebServices/QuotesService.asmx web service by using the
Url.Content() helper. Store this URL in a new variable named serviceUrl.

7. Call the LatestQuote method in the QuotesService service by using the jQuery.ajax() function
along with the following information:

o Type: POST

o URL: serviceUrl + "/LatestQuote"


o Data: {}

o Content type: application/json;

o charset=utf-8
o Data type: json

o Success function: OnSuccess

o Error function: OnError

8. Create a new JavaScript function by using the following information:

o Name: OnSuccess

o Parameter: response

9. In the OnSuccess function, set the contents of the element with id quote-display to be response.d.

10. Create a new JavaScript function by using the following information:

o Name: OnError

o Parameter: response

11. In the OnError function, set the contents of the element with id quote-display to be Could not
obtain the latest quote.

12. Start the web application in debugging mode, and click Get Latest Quote.

13. Stop debugging and close Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-15

Lab: Using Windows Azure Web Services in ASP.NET MVC


4 Web Applications
Scenario
In the Photo Sharing application, the users have the option to add location information of a photo when
they upload it. The senior developer recommends that you should store the location as a longitude and
latitude, and an address so that other applications can use the data in mash-ups. You have been asked to
create a service, hosted in Windows Azure, which will perform this conversion. You have to call this service
from the Photo Upload page in the Photo Sharing application.

Objectives
After completing this lab, you will be able to:

o Install the Windows Azure software development kit (SDK).

o Create a Bing Maps developer account and trial key.


o Write a web service in Visual Studio that is hosted in Windows Azure.

o Call a Window Azure web service from server-side code in a web application.

Lab Setup
Estimated Time: 45 minutes

Virtual Machine: 20486A-SEA-DEV11

User name: Admin

Password: Pa$$w0rd
Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.
o Before initiating this lab, you need to perform the following steps:
 Apply the Snapshot of the virtual machine, 20486A-SEA-DEV11, that was taken after
completing the lab in module 11.
 Navigate to Allfiles (D):\Labfiles\Mod
11\Starter\PhotoSharingApplication\PhotoSharingApplication, and then copy the
web.config file.
 Navigate to Allfiles (D):\Labfiles\Mod
13\Starter\PhotoSharingApplication\PhotoSharingApplication, and then paste the
web.config file.
 Enable the Allow NuGet to download missing packages during build option in Package
Manager.
o After completing this lab, you need to take the Snapshot of the virtual machine. You need to
apply this Snapshot before initiating the labs in module 14 and module 15.

Exercise 1: Accessing Windows Azure and Bing Maps


Scenario
To develop a Windows Communication Foundation (WCF) service that is hosted in Windows Azure, you
must install the Windows Azure SDK. To resolve address details to latitude and longitude data, you will
use the Bing Maps Location application programming interface (API). You will call this API from the WCF
service hosted in Windows Azure so that you can re-use your web service from other Adventure Works
websites and applications.
MCT USE ONLY. STUDENT USE PROHIBITED
13-16 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

In this exercise, you will:

o Install the Windows Azure SDK.

o Create a Bing Maps developer account.

o Create a Bing Maps Key.

The main tasks for this exercise are as follows:

1. Install the Windows Azure SDK.

2. Create a Bing Maps Developer Account.

3. Create a Bing Maps key.

 Task 1: Install the Windows Azure SDK.


1. Start the virtual machine, and log on with the following credentials:

o Virtual Machine: 20486A-SEA-DEV11

o User name: Admin


o Password: Pa$$w0rd

2. Navigate to the following webpage:

o http://www.microsoft.com/web/downloads/platform.aspx

3. Download and run the Web Platform Installer.

4. Use the Web Platform Installer to download and install the Windows Azure SDK for .NET (VS
2012).

 Task 2: Create a Bing Maps Developer Account.


1. Navigate to the following webpage:

o https://www.bingmapsportal.com

2. Log on to the Bing Maps Account Center web application by using following the Windows Live
account credentials:

o User name: <Your Windows Live account name>

o Password: Pa$$w0rd

3. Register a new Bing Maps developer account by using the following information:

o Account Name: <Your Windows Live account name>

o Email Address: <Your Windows Live account name>

 Task 3: Create a Bing Maps key.


1. Create a new Bing Maps key in the Photo Sharing application by using the following information:

o Application Name: Photo Sharing Application

o Key Type: Trial


o Application Type: Public website
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-17

Results: After completing this exercise, you will be able to register an application to access the Bing Maps
APIs.

Exercise 2: Creating a WCF Service for Windows Azure


Scenario
You want to create a WCF service to resolve a locality string to a latitude and longitude by looking up the
information in the Bing Maps Geocode SOAP service. After creating the WCF service, you need to host the
service Windows Azure by using the Windows Azure Cloud Service project template from the Windows
Azure SDK.

In this exercise, you will:

o Add a new Windows Azure Cloud Service project to the web application.

o Create the Location Checker Service interface.


o Add a service reference to the Bing Maps Geocode service.

o Write the Location Checker service.

o Publish the Service in Windows Azure.


The main tasks for this exercise are as follows:

1. Add a new Windows Azure Cloud Service project to the web application.

2. Create the Location Checker Service interface.


3. Add a service reference to the Bing Maps Geocode service.

4. Write the Location Checker service.

5. Publish the Service in Windows Azure.

 Task 1: Add a new Windows Azure Cloud Service project to the web application.
1. Open the PhotoSharingApplication.sln file from the following location:

o File location: Allfiles (D):\Labfiles\Mod13\Starter\PhotoSharingApplication

2. Add a new project to the PhotoSharingApplication web application by using the following
information:

o Template: Windows Azure Cloud Service

o Name: LocationChecker

o .NET Framework 4.5 Role: WCF Service Web Role

3. Rename the WCFServiceWebRole1 project as LocationCheckerWebRole.

4. Rename the IService1.cs file as ILocationCheckerService.cs.

5. Rename the Service1.svc file as LocationCheckerService.svc.

6. Rename the WCFServiceWebRole1 namespace to LocationCheckerWebRole throughout the


LocationCheckerService.svc.cs file.

7. Rename the Service1 class to LocationCheckerService.

8. In the ServiceDefinition.csdef file, set the value of vmsize for the LocationCheckerWebRole to
ExtraSmall.
MCT USE ONLY. STUDENT USE PROHIBITED
13-18 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

9. Save all the changes.

 Task 2: Create the Location Checker Service interface.


10. Remove all the method declarations from the ILocationCheckerService interface.

1. Add a new method declaration to the ILocationCheckerService interface by using the following
information:

o Annotation: [OperationContract]

o Return type: string

o Name: GetLocation

o Parameter: a string named address

2. Save all the changes.

 Task 3: Add a service reference to the Bing Maps Geocode service.


1. Add a service reference to the LocationCheckerWebRole project by using the following information:

o Address:
http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl.

o Namespace: GeocodeService

2. Add a using statement for the following namespace:

3. LocationCheckerWebRole.GeocodeService

4. Save all the changes.

 Task 4: Write the Location Checker service.


1. Remove the following public methods from the LocationCheckerService class:
o GetData

o CompositeType

2. Add a new method to the LocationCheckerService class by using the following information:
o Scope: public

o Return type: string

o Name: GetLocation

o Parameter: a string named address

3. In the GetLocation method, create a string variable, results, and initialize the value of the results
variable to an empty string.
4. From the Bing Maps Account Center page, copy the key you created for the Photo Sharing
Application.

5. In the GetLocation method, create a string variable, key, and paste the key from the Bing Maps
Account Center page as a value of the key variable.

6. Open the Geocoding Code.txt from the following location:

o Allfiles (D):\Labfiles\Mod13\Service Code

7. Copy all the text from the Geocoding Code.txt file and paste the text into the GetLocation method,
immediately after the key variable initialization.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-19

8. At the end of the GetLocation method, return the string variable results.

9. Save all the changes.

 Task 5: Publish the Service in Windows Azure.


1. Package the LocationChecker project by using the following information:

o Service Configuration: Cloud

o Build Configuration: Debug


2. In Internet Explorer, log on to the Windows Azure Portal.

3. Create a new cloud service by using the following information:

o URL: <Your Windows Live Account Name>LocationService

o Region: <Choose a location near you>

4. Upload a new staging deployment to the new cloud service by using the following information:

o Name: LocationChecker
o Package location: Allfiles
(D):\Labfiles\Mod13\Starter\PhotoSharingApplication\LocationChecker\bin\Debug\app.p
ublish
o Package: LocationChecker.cspkg

o Configuration: ServiceConfiguration.Cloud.cscfg

o Deploy even if one or more roles contain a single instance.

Results: After completing this exercise, you will be able to create a WCF cloud service. You will also be
able to publish the WCF cloud service in Windows Azure.

Exercise 3: Calling a Web Service from Controller Action


Scenario
Now that you have created and deployed the Location Checker WCF service in Windows Azure, you can
call the service from the Photo Sharing ASP.NET MVC web application. You can also call the service from
other .NET code, such as desktop applications, if necessary.

In this exercise, you will use the Location Checker service to add latitude and longitude data to new
photos as they are added to the Photo Sharing application.

The main tasks for this exercise are as follows:

1. Add a service reference to the Photo Sharing application.

2. Call the WCF service from the photo create action.

3. Configure the Services Database Connection String

4. Test the Location Checker service.

 Task 1: Add a service reference to the Photo Sharing application.


1. Add a new Service Reference to the PhotoSharingApplication project by using the following
information:
MCT USE ONLY. STUDENT USE PROHIBITED
13-20 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

o Address: Copy the Site URL from the CLOUD SERVICE page in the Windows Azure Portal

o Namespace: GeocodeService

2. Add a using statement for the following namespace to the PhotoController.cs code file:

o PhotoSharingApplication.GeocodeService

3. Save all the changes.

 Task 2: Call the WCF service from the photo create action.
1. Add a new method to the PhotoController class by using the following information:

o Scope: private

o Return type: string


o Name: CheckLocation

o Parameter: a string named location

2. In the CheckLocation method, create a new variable by using the following information:

o Type: LocationCheckerServiceClient

o Name of the variable: client

o Value of the variable: Null


3. In the CheckLocation method, create a new string variable, response, and initialize the value of the
response variable to Null.

4. Add a new try…catch statement that catches all errors in a variable named e.

5. In the try block, set the client variable to be a new LocationCheckerServiceClient.

6. Pass the location parameter to the client.GetLocation method and store the result in the response
variable.

7. In the catch block, store the e.Message property in the response variable.

8. At the end of the CheckLocation method, return the response string.

9. At the start of the Create action method for the HTTP POST verb, add an if statement that checks
that photo.Location is not an empty string.

10. In the if statement, pass the photo.Location property to the CheckLocation method and store the
result in a new string variable named stringLongLat.

11. Add an if statement to check whether the stringLongLat variable starts with the string, Success.

12. In the if statement, create a new array of characters named splitChars and add the single character
":" to the array.

13. Pass the splitChars array to the Split method of stringLongLat, and store the result in a new array of
strings named coordinates.

14. Set the photo.Latitude property to the second string in the coordinates array and set the
photo.Longitude property to the third string in the coordinates array.

15. Save all the changes.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 13-21

 Task 3: Configure the Services Database Connection String


1. In the Windows Azure Portal, copy the ADO.NET connection string for the
PhotoSharingAppServices database to the clipboard.

2. Paste the connection string into the connectionString value for the AzureAppServices connection
string.

3. Save all the changes.

 Task 4: Test the Location Checker service.


1. Set PhotoSharingApplication as the startup project for the solution.

2. Run the web application in debugging mode, and then log on to the web application by using the
following credentials:

o User name: David Johnson

o Password: Pa$$w0rd2

3. Add a new photo to the application by using the following information:


o Title: Testing Locations

o Photo: Allfiles (D):\Labfiles\Mod13\Sample Photos\Beach.jpg

o Location: Florence, OR
4. Browse to the Home page and display your new photo to check the Latitude and Longitude
properties.

5. Stop debugging and close Visual Studio.

Results: After completing this exercise, you will be able to add a service reference for a Windows Azure
WCF service to a .NET Framework application. You will also be able to call a WCF service from an MVC
controller action.

Question: What is the advantage of calling the Bing Maps Geocoding service from a WCF
service in Windows Azure, instead of calling the Geocoding service directly from the Create
action in the MVC web application?

Question: Why is latitude and longitude data useful for photos in the Photo Sharing
application?
MCT USE ONLY. STUDENT USE PROHIBITED
13-22 Using Windows Azure Web Services in ASP.NET MVC 4 Web Applications

Module Review and Takeaways


You can upload applications and services on Windows Azure to enable better scalability and reusability of
the application logic. Windows Azure includes different roles that support application needs such as
hosting and storage. You learned how to use cloud services to host WCF services. You also learned to use
the hosting services available in MVC and HTML applications.
MCT USE ONLY. STUDENT USE PROHIBITED
14-1

Module14
Implementing Web APIs in ASP.NET MVC 4 Web
Applications
Contents:
Module Overview 14-1 

Lesson 1: Developing a Web API 14-2 

Lesson 2: Calling a Web API from Mobile and Web Applications 14-12 

Lab: Implementing APIs in ASP.NET MVC 4 Web Applications 14-15 

Module Review and Takeaways 14-22 

Module Overview
Most web applications require integration with external systems such as mobile applications. You need to
know how to use Web APIs to promote application interaction with external systems. You can use the
Web API to implement Representational State Transfer (REST) services in your application. REST services
help reduce application overhead and limit the data that is transmitted between client and server systems.
You need to know how to call Web API services by using server-side code, jQuery code, and JSON.NET
library to effectively implement REST-style Web APIs in your application.

Objectives
After completing this module, you will be able to:
o Develop a Web API.

o Call a Web API from mobile and web applications.


MCT USE ONLY. STUDENT USE PROHIBITED
14-2 Implementing Web APIs in ASP.NET MVC 4 Web Applications

Lesson 1
Developing a Web API
You need to know how to develop Web API for applications, because Web API facilitates creating APIs for
mobile applications, desktop applications, web services, web applications, and other applications. By
creating a Web API, you make the information in your web application available for other developers to
use in their systems. Each web application has a different functional methodology; this difference can
cause interoperability issues in applications. REST services have a lightweight design, and Web API helps
implement REST services to solve the interoperability issues. You need to know how to use the different
routing methods that ASP.NET provides to implement REST services.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe Web APIs.


o Create a Web API for an MVC 4 web application.

o Describe RESTful services.

o Describe data return formats.


o Explain how to use routes and controllers to implement REST in Web APIs.

o Explore a Web API by using Internet Explorer as a client.

What Is a Web API?


Web API is a framework that is part of ASP.NET
MVC 4 that enables you to build Representational
State Transfer (REST)-enabled APIs. REST-enabled
APIs help external systems use the business logic
implemented in your application to increase the
reusability of the application logic. Web API
facilitates two-way communication between the
client system and the server, through tasks such
as:

o Instructing an application to perform a


specific task.

o Reading data values.

o Updating data values.

Web API enables developers to obtain business information by using REST, without creating complicated
XML requests such as Simple Object Access Protocol (SOAP). Web APIs use URLs in requests, thereby
eliminating the need for complicated requests. For example, the following URL obtains information for a
customer entity with the ID 1:
http://api.contoso.com/api/customers/1

Web API uses such URLs in requests and obtains results in the JSON format. The following code shows a
Web API request response in the JSON format.

[{"Id":1,"Name":"Tomato soup","Category":"Groceries","Price":1.0},{"Id":2,"Name":
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-3

"Yo-yo","Category":"Toys","Price":3.75},{"Id":3,"Name":"Hammer","Category":
"Hardware","Price":16.99}]

REST and Web API enable all kinds of different applications, including mobile device applications, to
interact with services. In particular, REST and Web API provide the following benefits for mobile
applications:

o They reduce the processing power needed to create complex request messages for data retrieval.

o They enhance the performance of the application by reducing the amount of data exchange
between client and server.

Question: What is the key benefit of using REST with Web APIs?

Routing in Web API


When you create a new project by using the Web
API template in ASP.NET MVC 4, it includes a
default routing rule. This routing rule helps map
HTTP requests to the Web API controllers and
actions by using HTTP verbs and the request URL.
You can make use of a naming convention to map
requests to actions, or you can control the
behavior of the mapping by using annotations on
action methods.

The Default API Route


Like standard MVC web applications, MVC Web
API uses routes to map requests to the right API
controller and action. In the Visual Studio project templates, the default API route is defined in the
WebApiConfig.cs file in the App_Start folder. The following code shows the default route.

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

In the preceding code sample, observe that the default route includes the literal path segment api. This
segment ensures that Web API requests are clearly separate from MVC controller routes, because Web API
requests must start with api.

The first place holder variable, {controller} helps identify the API controller to forward the request to. As
for MVC controllers, Web API appends the word Controller to this value to locate the right API controller
class. For example, Web API routes a request to the URI api/products to the controller called
ProductsController. As for MVC controllers, the optional place holder variable {id} is sent to the action as
a parameter.

You can also define your own API routes in the same manner as you do for MVC routes. Observe,
however, that Web API routes can handle requests from many types of client systems, including mobile
device applications, desktop applications, web applications, and web services. MVC routes only handle
web browser requests.

Using the Action Naming Convention


MCT USE ONLY. STUDENT USE PROHIBITED
14-4 Implementing Web APIs in ASP.NET MVC 4 Web Applications

The default Web API route does not include a place holder variable for the action name. This is because
Web API uses the HTTP verb and a naming convention to route requests to the right action within a given
controller.

Clients can make HTTP requests with one of four standard verbs: GET, POST, PUT, and DELETE. Other
verbs are possible. Web API looks for an action whose name begins with the requested HTTP verb. For
example, if the client sends a DELETE request to the URI api/products/23, Web API looks for a controller
called ProductsController. Within this controller it locates an action whose name begins with Delete.
According to the default route, the segment 23 is the {id} parameter. If there is more than one action
whose name begins with Delete, Web API chooses the action that accepts a parameter called id.

The HttpGet, HttpPut, HttpPost, and HttpDelete Attributes

You can use the HttpGet, HttpPut, HttpPost, or HttpDelete attributes in your controller action to
override the action naming convention. You can also use these verbs to specify that a function is mapped
to a specific HTTP verb. The following table describes how the HTTP attributes map to the HTTP verbs.

Attribute HTTP Verb

HttpGet GET

HttpPut PUT

HttpPost POST

HttpDelete DELETE

The following code illustrates the use of the HttpGet attribute on the FindProduct action.

public class ProductsController : ApiController


{
[HttpGet]
public Product FindProduct(id) {}
}

In the preceding code sample, observe that the HTTP attributes only allow mapping of one HTTP Verb to
an action in the controller.

The AcceptVerbs Attribute


The use of the AcceptVerbs attribute allows you to specify custom HTTP Verbs or multiple HTTP Verbs to
the same actions in the controller. The following code shows the use of the AcceptVerbs attribute to map
specific HTTP Verbs to the action.

public class ProductsController : ApiController


{
[AcceptVerbs("GET", "HEAD")]
public Product FindProduct(id) { }
[AcceptVerbs("MKCOL")]
public void MakeCollection() { }
}

The ActionName attribute


By default, the action name is the name of the action you specify in the controller. You can use the
ActionName attribute to specify the action name to be used in the routing. The following code shows
how to map an action to a HTTP request using custom action name.

public class ProductsController : ApiController


{
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-5

[HttpGet]
[ActionName("Thumbnail")]
public HttpResponseMessage GetThumbnailImage(int id);
[HttpPost]
[ActionName("Thumbnail")]
public void AddThumbnailImage(int id);
}

Question: What is the purpose of using the HTTP attributes?

Creating a Web API for an MVC 4 Web Application


MVC4 provides a Web API project template that
helps implement Web API in a project.

The following image shows the list of project


templates available in Microsoft Visual Studio.

FIGURE 14.1:THE NEW PROJECT DIALOG BOX


To implement a Web API template in your project, you need to perform the following steps:

1. In the New Project dialog box, click ASP.NET MVC 4 Web Application.
2. In the Select a Template box of the New ASP.NET MVC 4 Project dialog box, click Web API.

The following image shows the New ASP.NET MVC 4 Project dialog box.
MCT USE ONLY. STUDENT USE PROHIBITED
14-6 Implementing Web APIs in ASP.NET MVC 4 Web Applications

FIGURE 14.2:THE NEW ASP.NET MVC 4 PROJECT DIALOG BOX


After selecting the Web API template, you need to add a new MVC controller class that derives from
ApiController. The API controller class hosts application code for handling Web API requests. ASP.NET
MVC4 engine maps the URL together with the HTTP Verb and the controller or the action function of a
controller, in the following format:

<http verb> http://<hostname>/api/<entity name>/<parameters>

The HTTP verb communicates to the Web API about the operations that it should perform; whereas the
rest is to communicate which entity and operations to perform on. Therefore, HTTP plays an important
role. For example, consider the following API controller class.

public class ProductsController : ApiController


{
public IEnumerable<Product> GetAllProducts()
{
}
public Product GetProductById(int id)
{
}
}

In the preceding code sample, note the controller classes GetAllProducts and GetProductById. The
GetAllProducts controller class helps obtain a full list of products from the database. You can map the
following URL and HTTP verb with the GetAllProducts controller class.

GET /api/products
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-7

The GetProductById controller class helps obtain a specific product by using the ID detail. You can map
the following URL with the GetProductById controller class.

GET /api/products/id

Question: What is the syntax that the ASP.NET MVC engine uses for mapping controllers and action
functions?

RESTful Services
REST uses URLs and HTTP verbs to uniquely
identify the entity that it operates on and the
action that it performs. REST helps retrieve
business information from the server. However, in
addition to data retrieval, business applications
perform more tasks such as creating, updating,
and deleting information on the database. Web
API and REST facilitate handling such additional
tasks. They use the HTTP method to identify the
operation that the application needs to perform.

The following table provides information on some


HTTP methods that Web API and REST use.

HTTP Method Description

GET Use this method with the following URL to obtain a list of all customers.

/api/customers

GET Use this method with the following URL to obtain a customer by using the ID detail.

/api/customers/id

GET Use this method with the following URL to obtain customer by using the category detail.

/api/customers?country=country

POST Use this method with the following URL to create a customer record.

/api/customers

PUT Use this method with the following URL to update a customer record.

/api/customers/id

DELETE Use this method with the following URL to delete a customer record.

/api/customers/id
MCT USE ONLY. STUDENT USE PROHIBITED
14-8 Implementing Web APIs in ASP.NET MVC 4 Web Applications

Web API allows developers to use strong typed model for developers to manipulate HTTP request. The
following code shows how to use the POST, PUT, and DELETE methods for create, update, and delete
requests for handling the create, update and delete the customer records.

public HttpResponseMessage PostCustomer(Customer item)


{
}
public void PutCustomer(int id, Customer item)
{
}
public void DeleteProduct(int id)
{
}

Question: What is the mandatory requirement of create and update requests?

Data Return Formats


When a client makes a request to a Web API
controller, the controller action often returns
some data. For GET requests, for example, this
data might be all the properties of a specific
product or all the properties of all the products.
Web API can return this data in one of two
formats: JavaScript Object Notation (JSON) or
XML.

JSON and XML Data Formats


Both JSON and XML are text formats that
represent information as strings. You can also use
JSON outside of JavaScript code. The following
code is a simple example of JSON.

{"Name":"Albert","Age":29,"Height":145,"Skills":["Programming", "Technical Writing"]}

You can represent the same data in the previous code sample, by using XML as shown in the following
code:

<Employee Name="Albert" Age="29" Height="145">


<Skills>
<Skill Name="Programming" />
<Skill Name="Technical Writing" />
</Skills>
</Employee>

When a client makes a request, the client can specify the data format for the response. If the data format
is not specified, Web API formats data as JSON by default.

Media Formatters
Web API uses a media formatter to format or serialize the information that a Web API REST service
returns. Web applications usually use the JSON format to format the data that functions return. However,
you can alternatively use the XML media formatter or add a custom media formatter to control the the
data returned. Media formatters format the content that the server renders to the client systems.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-9

For example, consider that you want Web API to return CSV files. In this case, you need to create a custom
media formatter, to create as output data files in the CSV format, instead of XML files in the JSON format.
The following code shows to add a new class in your project, to create a custom media formatter.

public class CsvFormatter : BufferedMediaTypeFormatter


{
}

Media formatter classes inherit from the following classes:

o MediaTypeFormatter. This is the fundamental class for all formatter classes.

o BufferedMediaTypeFormatter. This is an extended class of formatter classes, which provides


support for buffering and asynchronous operations.

Additional Reading: To learn more about creating media formatters, go to:


http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

Question: Why should you use amedia formatter for Web API REST services?

Using Routes and Controllers in Web APIs


ASP.NET MVC4 uses a route table to map a URL
and a controller. When you create a project,
ASP.NET adds a default route by using the Web
API template. This default route helps support the
operations of the REST-style Web APIs. The
following code shows the default route.

routes.MapHttpRoute(
name: "API Default",
routeTemplate:
"api/{controller}/{id}",
defaults: new { id =
RouteParameter.Optional }
);

Consider that you want to include multiple actions, such as creating customers with XML and JSON, in the
same HTTP method. In this case, you cannot use the default route because it requires a new request for
each HTTP method and URL combination. Therefore, you need to update the routing by modifying the
Route Map in the WebApiConfig class. The following code shows how to update the routing, to support
multiple operations in the same HTTP method.

routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

You can use the WebApiConfig class to enable multiple versions of API to coexist in the same project. For
example, you can include /api/v1/{controller} as Version One of your API and include
/api/v2/{controller} as a new version or Version Two of the API.

You may want to include supporting functions in the controller class, and hide the supporting functions
from the REST functions. You can eliminate the exposure of the function to the REST interface by adding
MCT USE ONLY. STUDENT USE PROHIBITED
14-10 Implementing Web APIs in ASP.NET MVC 4 Web Applications

the NoAction attribute to the action function. The following code shows how to add the NoAction
attribute to the action function.

[NonAction]
public string GetPrivateData()
{
}

By default, Web API exposes all public methods as REST services. You can prevent this by making the
function private, but this action prevents application code in the same project to access the function.
Therefore, you can use the NonAction attribute for functions that need to be in public, but do not need
to be exposed in REST.

Question: What is the key benefit of using the routing map?

Discussion Topic: Scenario for using Web API


Consider the following scenarios. In each case,
discuss with the rest of the class whether the
scenario would use Web API to create REST
services and how to implement them.

1. You are creating a photo sharing application,


and you want to enable each user to discuss
photos and cycling trips with their friends.
You also need to prevent other users from
seeing these discussions. You are building the
API to allow mobile applications to perform
such functions.

2. You want to ensure that MVC calls the


GetImage action method by using ID, to specify the ID of the image that downloads.

3. You are writing a photo sharing application and you need to allow external applications to download
the photo and metadata from the application.

4. You are writing a photo sharing application and you need generate a lightweight HTML site for users
to use in mobile devices.

Demonstration: How to Explore a Web API by Using Internet Explorer


In this demonstration, you will see how to:

o Create a simple web API for an existing ASP.NET MVC 4 web application.

o Download and examine the JSON files that the web API generates by using Internet Explorer.

Demonstration Steps
1. Add a new controller to the web application by using the following information:

o Name: OperasApiController

o Template: Empty API controller


2. Add a using statement for the following namespace to the OperasApiController.cs file:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-11

o OperasWebSite.Models

3. In the OperasApiController class, create a new variable by using the following information:

o Scope: private

o Type: OperasDB

o Name: contextDB

o Value: new OperasDB()

4. In the OperasApiController class, create a new action by using the following information:

o Scope: public

o Return type: IEnumerable<Opera>

o Name: GetOperas

o Parameters: None
5. Return an enumerable collection of all the operas in the contextDB object.

6. In the OperasApiController class, create a new action by using the following information:

o Scope: public
o Return type: Opera

o Name: GetOperas

o Parameters: an integer named id


7. In the GetOperas action, find the Opera object with ID, id, and store it in a new Opera variable
named opera.

8. If the opera object is null, throw a new HttpResposeException with the HTTP status code Not
Found.

9. At the end of the GetOperas action, return the opera object.

10. Save all the changes.


11. Start the web application in debugging mode.

12. Type the following URL in the Address bar of Internet Explorer to verify that the JSON file that is
returned displays the data related to the Rigoletto opera:

o http://localhost:<yourPortNumber>/api/OperasApi

13. Examine the JSON file that is returned by using the following URL:

o http://localhost:yourPortNumber/api/OperasApi/3

14. Stop debugging and close Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
14-12 Implementing Web APIs in ASP.NET MVC 4 Web Applications

Lesson 2
Calling a Web API from Mobile and Web Applications
After you complete the development of the Web API services, you need to create the client applications
to call these services. Calling Web API services is different from calling WCF services. However, the
methods that you need to use to call these services are similar, regardless of the platform. You need to
know how to call Web API services by using server-side code, jQuery code and JSON.NET library, to
effectively implement Web API services in most application platforms.

Lesson Objectives
After completing this lesson, you will be able to:

o Call Web APIs by using server-side code.

o Call Web APIs by using jQuery.

o Call Web APIs by using Windows Phone applications.

Calling Web APIs by Using Server-Side Code


You can call REST-style services by using ASP.NET
server-side code. You can use the
HttpWebRequest class to create a manual HTTP
request to the REST services. ASP.NET provides a
.NET library that you can use in web applications,
to call REST-enabled Web API services from the
.NET server. To use the .NET library, you need to
install the Microsoft.AspNet.WebApi.Client
NuGet package. This NuGet package provides
access to the HttpClient class. The HttpClient
class simplifies interacting with Web APIs, because
it reduces coding efforts.

After installing the NuGet package, you need to initialize the HttpClient class. The following code
illustrates how to initialize the HttpClient class.

HttpClient client = new HttpClient();


client.BaseAddress = new Uri("http://localhost/");
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));

The last line of code in the preceding code sample informs the client system about the media type that
the client system should use. The default media type that applications use is application/json. However,
applications can use any other media type, based on the media type that the REST-style services support.

The following code shows how to call Web API REST services by using server-side code.

HttpResponseMessage response = client.GetAsync("api/customers").Result;


if (response.IsSuccessStatusCode)
{
var products = response.Content.ReadAsAsync<IEnumerable<Customer>>().Result;
}
else
{
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-13

Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);


}

After running the code in the preceding code sample, you need to define a data model that aligns itself
with the one used by the Web API service to enable the .NET library to:

o Process the results of the server-side code.

o Return results as .NET objects for the application to use.

Then, you can use the GetAsync and ReadAsAsync methods to:

o Create requests to Web API REST services.

o Parse the content into .NET objects.

The PostAsJsonAsync function uses the HTTP POST method to call Web API services that support the
POST method.

Question: What is the benefit of using the Microsoft.AspNet.WebApi.Client NuGet package?

Calling Web APIs by Using jQuery Code


You can call Web API services in the same manner
as you call other services that use technologies
such as WCF. You can also call Web API services,
by using the jQuery ajax function. The following
code shows how to call a Web API service by
using the jQuery ajax function.

$.ajax({
url:
'http://localhost/api/customers/,
type: 'GET',
dataType: 'json',
success: function (data) {
},
error: function (e) {
}
});

In the preceding code sample, observe the dataType parameter of the ajax function. You should set this
parameter to json or another data type that the Web API service supports. Most applications use JSON
because it is light weight. The ajax function has built-in functionalities that parse JSON results for the ease
of developers.

You can use JSON.stringify() in the data parameter of the ajax function to serialize the JavaScript
objects into JSON objects for sending to the Web API method. The following code shows how to use
JSON.stringify() in the data parameter of the ajax function.

var customer = {
ID:'1',
CustName: 'customer 1'
};
$.ajax({
url: 'http://localhost/api/customer',
type: 'POST',
data:JSON.stringify(customer),
contentType: "application/json;charset=utf-8",
MCT USE ONLY. STUDENT USE PROHIBITED
14-14 Implementing Web APIs in ASP.NET MVC 4 Web Applications

success: function (data) {


},
error: function (x) {
}
});

Question: What is the benefit of using JSON.stringify() in the ajax function?

Calling Web APIs Using Windows Phone Applications


You can use NuGet packages to call Web API REST
services in .NET applications. However, in
Windows Phone web applications, you need to
use the JSON.NET library to call Web API REST
services. The JSON.NET library is more light weight
and it enhances the performance of Windows
Phone applications. You can download the library
from http://json.codeplex.com/

The JSON.NET library contains a set of .NET classes


that help create JSON requests to any REST-style
APIs. To use the JSON.NET library, you need to
download it and place the JSON.NET assembly in
the /WindowsPhone folder of the /bin folder.

The following code shows how to call an HTTP request by using the WebClient class.

WebClient webClient = new WebClient();


Uri uri = new Uri("http://localhost/api/customer/");
webClient.DownloadStringCompleted += new
DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(uri);

When you make a request by using the WebClient class, the JSON.NET library processes the request and
produces results from the WebClient class to return .NET objects. When you call Web APIs by using the
library, you need to add the DownloadStringCompleted event handler. This event handler helps place
logic to handle the data that Web APIs return. In the DownloadStringCompleted event handler, you
should add the code similar to the following.

using Newtonsoft.Json;
/// Lines skipped
List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(e.Result);
foreach (Customer em in customers)
{
}

The code in the preceding sample helps enable the JSON.NET library to deserialize the results into.NET
objects.

Question: What is the key benefit of using the JSON.NET library?


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-15

Lab: Implementing APIs in ASP.NET MVC 4 Web


Applications
Scenario
Your manager wants to ensure that the photos and information stored in the Photo Sharing application
can be integrated with other data in web mash-ups, mobile applications, and other locations. To re-use
such data, while maintaining security, you need to implement a RESTful web API for the application. You
will use this web API to display the locations of photos on a Bing Maps page.

Objectives
After completing this lab, you will be able to:

o Create a web API by using the new features of ASP.NET MVC 4.

o Add routes and controllers to an application to handle REST requests.

o Call a REST web API from jQuery client-side code.

Lab Setup
Estimated Time: 40 minutes

Virtual Machine: 20486A-SEA-DEV11


User name: Admin

Password: Pa$$w0rd
Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

 Before initiating this lab, perform the following steps:


a. Apply the Snapshot of the virtual machine, 20486A-SEA-DEV11, that was taken after completing
the lab in module 13.

b. Navigate to Allfiles (D):\Labfiles\Mod


13\Starter\PhotoSharingApplication\PhotoSharingApplication, and then copy the
web.config file.

c. Navigate to Allfiles (D):\Labfiles\Mod


14\Starter\PhotoSharingApplication\PhotoSharingApplication, and then paste the
web.config file.

d. Enable the Allow NuGet to download missing packages during build option in Package
Manager.

Exercise 1: Adding a Web API to the Photo Sharing Application


Scenario
You have been asked to implement a web API for the Photo Sharing application to ensure that photos can
been used in third-party websites, mobile device applications, and other applications.
In this exercise, you will

o Add a web API controller for the Photo model class.

o Configure formatters and routes to support the web API.

o Test the API by using Internet Explorer.


MCT USE ONLY. STUDENT USE PROHIBITED
14-16 Implementing Web APIs in ASP.NET MVC 4 Web Applications

The main tasks for this exercise are as follows:

1. Add a Photo API controller.

2. Configure API routes.

3. Configure media-type formatters.

4. Test the web API with Internet Explorer.

 Task 1: Add a Photo API controller.


1. Start the virtual machine and log on with the following credentials:

o Virtual machine: 20486A-SEA-DEV11

o User name: Admin


o Password: Pa$$w0rd

2. Open the PhotoSharingApplication solution from the following location:

o File location: Allfiles (D):\Labfiles\Mod14\Starter\PhotoSharingApplication

3. Add a new controller to the PhotoSharingApplication project by using the following information:

o Name: PhotoApiController

o Template: Empty API controller


4. Add a using statement for the following namespace to PhotoApiController.cs:

o PhotoSharingApplication.Models

5. Add a new variable to the PhotoApiController class by using the following information:

o Scope: private

o Type: IPhotoSharingContext

o Name: context
o Initial value: a new PhotoSharingContext object

6. Add a new action to the PhotoApiController by using the following information:

o Scope: public

o Return type: IEnumerable<Photo>

o Name: GetAllPhotos

o Parameters: none

7. In the GetAllPhotos action, return the context.Photos collection as an enumerable object.

8. Add a new action to the PhotoApiController by using the following information:

o Scope: public

o Return type: Photo

o Name: GetPhotoById

o Parameters: an integer named id

9. In the GetPhotoById action, pass the id parameter to the context.FindPhotoById() method. Store
the returned Photo object in a variable named photo.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-17

10. If the photo variable is null, throw a new HttpResponseException and pass the
HttpStatusCode.NotFound value.
11. At the end of the GetPhotoById action, return the photo object.

12. Add a new action to the PhotoApiController by using the following information:

o Scope: public

o Return type: Photo

o Name: GetPhotoByTitle

o Parameters: a string named title


13. In the GetPhotoByTitle action, pass the title parameter to the context.FindPhotoByTitle() method.
Store the returned Photo object in a variable named photo.

14. If the photo variable is null, throw a new HttpResponseException and pass the
HttpStatusCode.NotFound value.

15. At the end of the GetPhotoById action, return the photo object.

16. Save all the changes.

 Task 2: Configure API routes.


1. In the WebApiConfig.cs code file, in the Register method, remove all existing route registrations.

2. Add a new route to the Register method by using the following information:

o Name: PhotoApi

o Route template: api/photos/{id}

o Default controller: PhotoApi

o Default action: GetPhotoById

o Constraint regular expression: [0-9]+

3. After the PhotoApi route, add a new route to the Register method by using the following
information:
o Name: PhotoTitleApi

o Route template: api/photos/{title}

o Default controller: PhotoApi

o Default action: GetPhotoByTitle

4. After the PhotoTitleApi route, add a new route to the Register method by using the following
information:
o Name: PhotosApi

o Route template: api/photos

o Default controller: PhotoApi

o Default action: GetAllPhotos

5. Save all the changes.


MCT USE ONLY. STUDENT USE PROHIBITED
14-18 Implementing Web APIs in ASP.NET MVC 4 Web Applications

 Task 3: Configure media-type formatters.


1. In the WebApiConfig.cs code file, at the end of the Register method, create a new variable named
json. Set this variable to config.Formatters.JsonFormatter.

2. Set the json.SerializerSettings.PreserveReferencesHandling property to


Newtonsoft.Json.PreserveReferencesHandling.Object.

3. Remove the XmlFormatter object from the config.Formatters collection.

4. Save all the changes.

 Task 4: Test the web API with Internet Explorer.


1. Start that web application in debugging mode.

2. Request the photo with ID 4 by using the web API. Display the returned JSON file by using Visual
Studio and check that the Title property is Sample Photo 4.

3. Request the photo with title Sample Photo 5 by using the web API. Display the returned JSON file by
using Visual Studio and check that the Title property is Sample Photo 5.
4. Request all photos by using the web API. Display the returned JSON file by using Visual Studio, and
check that both Sample Photo 9 and Sample Photo 13 are present.

5. Close Visual Studio and stop debugging.

Results: At the end of this exercise, you will be able to create a simple web API for an ASP.NET MVC 4
web application.

Exercise 2: Using the Web API for a Bing Maps Display


Scenario
You need to use the new web API to obtain the photos in the client-side jQuery code. You will use
latitude and longitude properties to display these photos as pins on a Bing API map.

To create the map display in the Photo Sharing application, you must add a new view and action for the
photo controller. You must also add a new template view because the Bing Maps AJAX control requires a
different <!DOCTYPE> directive to the one in use elsewhere in the Photo Sharing application. You will
import a JavaScript file with basic Bing Maps code in place. To this JavaScript file, you will add code to call
the web API, obtain photo details, and display them on the map.

In this exercise, you will:

o Create a new template view.

o Create a map action, view, and script file.

o Obtain and display photos.

o Test the Bing Maps control.

The main tasks for this exercise are as follows:

1. Create a new template view.

2. Create a map action, view, and script file.

3. Obtain and display photos.

4. Test the Bing Maps control.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-19

 Task 1: Create a new template view.


1. In the Views/Shared folder, create a copy of the _MainLayout.cshtml view file and name the copy
as _MapLayout.cshtml.

2. In the _MapLayout.cshtml file, replace the <!DOCTYPE> declaration with <!DOCTYPE html
PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">.

3. In the onload event of the BODY element, call the GetMap() JavaScript function.

4. Remove the links to jQueryUI and Unobtrusive AJAX.

5. Add a new link to the Bing Maps AJAX control in the HEAD element by using the following
information:

o Charset: UTF-8

o Type: text/javascript
o SRC: http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0

6. Save all the changes.

 Task 2: Create a map action, view, and script file.


1. Add a new action to the PhotoController class by using the following information:
o Scope: public

o Return type: ViewResult

o Name: Map
o Parameters: none

2. In the new Map action, return a View named Map.

3. Add a new view for the Map action by using the following information:

o View name: Map

o Type: Do not create a strongly-typed view.

o Layout: _MapLayout.cshtml
4. Remove the H2 element from the Map.cshmtl view.

5. Add a new JavaScript block to the Map.cshtml view.

6. In the new JavaScript block, create a new variable named webApiUrl by using the Url.Content()
helper to set the webApiUrl variable to ~/api/photos.

7. Create a new variable named pictureUrl by using the Url.Action() helper to set the pictureUrl
variable to the URL of the GetImage action of the Photo controller. Include a forward slash at the
end of the URL.

8. Create a new variable named displayUrl by using the Url.Action() helper to set the displayUrl
variable to the URL of the Display action of the Photo controller. Include a forward slash at the end
of the URL.

9. Add a new JavaScript script, with an empty src attribute, to the Map.cshtml view.

10. Use the Url.Content() helper to set the src attribute in the new JavaScript code to
~/Scripts/MapDisplay.js.
MCT USE ONLY. STUDENT USE PROHIBITED
14-20 Implementing Web APIs in ASP.NET MVC 4 Web Applications

11. Create a new DIV element by using the following information:

o ID: mapDiv

o Style position: absolute

o Style width: 650px

o Style height: 400px

12. Add the MapDisplay.js JavaScript file to the Scripts folder from the following location:

o Allfiles (D):\Labfiles\Mod14\Bing Maps Script

13. Add a new node to the site map by using the following information:

o Title: Map

o Visibility: *

o Controller: Photo
o Action: Map

14. Save all the changes.

15. Start the web application in debugging mode and browse to the Map page.
16. Stop debugging.

 Task 3: Obtain and display photos.


1. In the MapDisplay.js script file, add a new function by using the following information:

o Name: GetPhotos
o Parameter: serviceUrl

2. Set the $.support.cors value to true.

3. Use the $.ajax() jQuery function to call the GetPhotos web API by using the following information:
o URL: serviceUrl

o Type: GET

o Data type: json


o Success: DisplayPics

o Error: OnError

4. Add a new function by using the following information:

o Name: DisplayPics

o Parameter: response

5. In the DisplayPics function, create two variables named location and pin.

6. Use the jQuery $.each function to loop through all the photo objects in the response collection.

1. For each photo object in the response collection, set the location variable to a new location by
using the following information:

o Object: Microsoft.Maps.Location

o Latitude: photo.Latitude
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 14-21

o Longitude: photo.Longitude

2. Set the pin variable to a new push pin by using the following information:

o Object: Microsoft.Maps.PushPin

o Location: location

3. Set the pin.Title property to photo.Title and the pin.ID property to photo.PhotoID.

4. Ensure that the DisplayInfoBox method handles the click event for pushpins by using the following
information:

o Method: Microsoft.Maps.Events.addHandler

o Object: pin

o Event: 'click'

o Handler method: DisplayInfoBox


5. Add the pin object to the dataLayer object by using the dataLayer.push function.

6. Add a new function by using the following information:

o Name: OnError
o Parameter: response

7. In the OnError function, use the alert function to inform the user that the picture coordinates could
not be obtained.

8. At the end of the GetMap function, call the GetPhotos function and pass the webApiUrl variable.

9. Save all the changes.

 Task 4: Test the Bing Maps control.


1. Start the web application in debugging mode and browse to the Map page to check the map control.
2. Click a pin of your choice.

3. Click the thumbnail.

4. Stop debugging and close Visual Studio.

Results: After completing this exercise, you will be able to create a template view to display a Bing Map
AJAX control, and create a view and script file to display a Bing Map. You will also use jQuery to call a web
API and obtain details of photos. You will then mash up the data from a web API with Bing Maps data.

Question: How do the API actions you added to the PhotoApiController controller in
Exercise 1 differ from other actions in MVC controllers?
MCT USE ONLY. STUDENT USE PROHIBITED
14-22 Implementing Web APIs in ASP.NET MVC 4 Web Applications

Module Review and Takeaways


You can use the Web API framework to facilitate creating REST-style Web API calls in applications. REST-
style Web API is recommended for mobile applications, because of the light weight design of REST
services.

REST services use HTTP methods such as GET, POST, and PUT to notify the API of the action that it needs
to perform. Web APIs use the media formatter and the JSON.NET library to serialize and deserialize
information, respectively. You can call Web API services by using server-side code, jQuery code, and the
JSON.NET library.

Real-world Issues and Scenarios


Consider that you develop a mobile application by using Web APIs and the application needs to use
currency rate services. For this application, you cannot use WCF, because WCF can impede the
performance of the application by using XML for data exchanges. Therefore, you should use REST and
JSON in the application, to reduce the data that is transmitted between the client system and the server.

 
MCT USE ONLY. STUDENT USE PROHIBITED
15-1

Module15
Handling Requests in ASP.NET MVC 4 Web Applications
Contents:
Module Overview 15-1 

Lesson 1: Using HTTP Modules and HTTP Handlers 15-2 

Lesson 2: Using Web Sockets 15-6 

Lab: Handling Requests in ASP.NET MVC 4 Web Applications 15-11 

Module Review and Takeaways 15-17 

Module Overview
ASP.NET MVC provides functionalities that help develop web applications. However, ASP.NET MVC does
not include functionalities that help change the encoding of the output. In such cases, you need to know
how to use the HTTP module or the HTTP handler, to facilitate such specific requests. Most web
applications require two-way communication between the client and server systems. The web sockets
protocol facilitates two-way communications in a robust manner.

Objectives
After completing this module, you will be able to:

o Use HTTP modules and HTTP handlers.

o Use web sockets.


MCT USE ONLY. STUDENT USE PROHIBITED
15-2 Handling Requests in ASP.NET MVC 4 Web Applications

Lesson 1
Using HTTP Modules and HTTP Handlers
Applications perform actions on a request in the HTTP pipeline, before rendering a webpage. You need to
know how to use the HTTP module to implement custom authentication mechanism for a webpage,
before rendering the webpage. You can also use HTTP module to create code that renders content in
different encoding. Sometimes, you may need the application to handle requests by using different
application logic, instead of the built-in ASP.NET page rendering logic. In such cases, you should know
how to use the HTTP handler, to process such specific requests.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe how HTTP modules intercept web requests.

o Create HTTP modules.


o Describe how HTTP handlers intercept web requests.

o Create HTTP handlers.

o Determine when to use HTTP modules and HTTP handlers.

What Is an HTTP Module?


The HTTP module is a program that runs the
application logic on a webpage, before ASP.NET
renders the webpage. To understand the
relevance of the HTTP module, you need to know
about the HTTP request pipeline. The IIS HTTP
request pipeline is a sequence of programs that
run on requests, to help the application perform
tasks. When the application receives a request, it
passes the request to the isapi_aspnet.dll ISAPI
library and starts the ASP.NET execution pipeline,
to process the request.

Then, the request passes through the


HttpApplication instance of the application and the HttpModule library.
HTTP modules provide the following features to an application:

o Security. Provides support for the custom authentication and authorization logic of your
application

o Logging and statistics. Gathers low-level information about the web application execution cycle,
for monitoring and logging purposes

o Custom headers and footers. Allows insertion of the custom header information in the response of
each request

The following is a list of HTTP modules in ASP.NET:

o OutputCache

o Session
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-3

o WindowsAuthentication

o FormsAuthentication

o PassportAuthentication

o UrlAuthorization

o FileAuthorization

o DefaultAuthentication

If you configure HTTP modules in an application, HTTP modules apply to all the HTTP requests that the
application receives. You cannot configure an HTTP module for a specific page.

Question: What are the benefits of using HTTP modules in web applications?

Creating HTTP Modules


You can create an HTTP module by adding the
CustomModule class to your project; this class
implements the IHttpModule interface in your
application. The following lines of code show how
to create the CustomModule class in a project.

public class CustomModule :


IHttpModule
{
public CustomModule ()
{
}
public String ModuleName
{
get { return "CustomModule"; }
}
public void Init(HttpApplication application)
{
context.Response.Write("<h1>Demo<h1>");
}
}

In the CustomModule class, you need to implement the following two methods:

o ModuleName property. Provides a display name that enables other application code to identify
the HTTP module.

o Init function. Provides the location to implement all logic for an HTTP module. The
HttpApplication class triggers this function when the application receives a request.

After you complete the development of a HTTP module, you need to register the HTTP module in the
web.config file. The following lines of code show how to register an HTTP module for an application
running in IIS 6.0 or later versions configured in the Classic mode.

<configuration>
<system.web>
<httpModules>
<add name="CustomModule" type="CustomModule "/>
</httpModules>
</system.web>
</configuration>
MCT USE ONLY. STUDENT USE PROHIBITED
15-4 Handling Requests in ASP.NET MVC 4 Web Applications

The following lines of code show how to register an HTTP module for an application running in IIS 7.0 or
later versions configured in the Integrated mode.

<configuration>
<system.webServer>
<modules>
<add name="CustomModule" type="CustomModule"/>
</modules>
</system.webServer>
</configuration>

Question: What is the benefit of using the Init function?

What Is an HTTP Handler?


HTTP modules help apply logic to all HTTP
requests that an application receives. The
HttpHandler function is a processing engine that
processes specific HTTP requests, for example, you
can configure an HttpHandler function to handle
*.ashx files. You can map an HttpHandler
function to a URL based on the file extension of
the requested server-side page, such as *.aspx.
The following list describes some common
HttpHandler functions:

o ASP.NET page handler (*.aspx). This is the


default HTTP handler for ASP.NET pages.

o Web service handler (*.asmx). This is the default HTTP handler for web service pages.

o Generic web handler (*.ashx). This is the default HTTP handler for all web handlers that do not
have a user interface and include the @WebHandler directive.

o Trace handler (trace.axd). This is a handler that helps display all current page trace information.

You can use the *.ashx file extension with the Http handler because:

o The extension does not include any page-rendering logic.

o The extension allows developers to write logic to send responses to the client systems.

You can use a custom HTTP handler with the *.rss extension, to generate Real Simple Syndication (RSS)
feed content for user requests. You can also use a custom handler to request images from the database
and send the images to the client systems.

Question: What is the primary use of the HTTP generic handler (*.ashx)?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-5

Discussion: Scenarios for HTTP Modules and Handlers


Consider the following scenarios. In each case,
discuss with the rest of the class to determine
which Windows Azure service best suits the case.

o You are creating a photo sharing


application, and you want to enable each
user to discuss photos and cycling trips
with their friends. You need to ensure
that the application renders photos to
users, from the database table, without
requiring users to first save the file on
their system.

o You are creating a Representational State


Transfer (REST)-based business Application Programming Interface (API) that requires a custom
HTTP header, before accepting a request.

o You are creating a business API to provide content in custom XML format.
o You developing an application that requires saving diagnostic information about the header of
an HTTP request.
MCT USE ONLY. STUDENT USE PROHIBITED
15-6 Handling Requests in ASP.NET MVC 4 Web Applications

Lesson 2
Using Web Sockets
AJAX technologies are constantly evolving. The HTTP protocol does not cater to crucial requirements such as real-
time information updates from the server. You need to know how to use web sockets to create two-way
communication between the client and server systems. You also need to know when to use the traditional HTTP
model and the web sockets protocol, based on the needs of your application.

Lesson Objectives
After completing this lesson, you will be able to:

o Describe the WebSocket protocol.

o Describe how to create web sockets connections.

o Determine when to use web sockets.


o Add a web sockets-based application to the MVC 4 web application.

What Is the WebSocket Protocol?


The WebSocket protocol is a standard from the
World Wide Web Consortium (W3C), which
mandates browsers to support web sockets as part
of the HTML5 implementation.

Additional Reading: For more information


about the WebSocket protocol, go to:
http://dev.w3.org/html5/websockets/

The protocol facilitates two-way communication


between web browsers and web servers. The
protocol helps overcome the issue of HTTP not providing two-way communication.

Developers usually build applications by using different techniques such as long running of loops and
polling. These techniques extensively consume memory and other resources of both the server and client
systems. The WebSocket protocol resolves the problems that arise with using such techniques, by creating
a constant socket connection between the client and server. All communication between the client and
server systems occurs through the socket. This socket eliminates the need to recreate a request multiple
times, during real-time communications between the client and server systems. Remember that all
browsers may not support web sockets. Microsoft Internet Explorer 10 and Windows 8 applications, which
use JScript and HTML5, are compatible with web sockets.
The web sockets work in a similar manner as traditional network sockets. The only difference is that during
the initial handshake, web sockets use the Upgrade HTTP request that includes the Upgrade HTTP
header. If the server accepts to establish web sockets, the server returns a response with the status code,
101. Then, the client and server systems send the payload by using the socket.

When the client system no longer requires communication with the server, the application sends a close
connection payload, to notify the server to close the web socket.
Question: What is the key difference between traditional HTTP and web sockets?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-7

Coding Web Sockets Connections


To use web sockets, you need to implement
server-side services that can handle the two-way
communication between the server and the client
systems. To simplify the programming work,
Microsoft provides the Microsoft.WebSockets
NuGet package. This package includes a library
that helps handle web socket operations in the
server. While coding web sockets, you can:

o Use the Web API framework to create a


REST service that handles the initial
handshake.

o Use the WebSocket library to promote


communication between the client and server systems.

The following lines of code show how to create a REST service and use the WebSocket library, to handle
the operations of web sockets in the server.

public class ChatController : ApiController


{
public HttpResponseMessage Get(string username)
{
HttpContext.Current.AcceptWebSocketRequest(new ChatWebSocketHandler());
return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
}
class ChatWebSocketHandler : WebSocketHandler
{
public ChatWebSocketHandler()
{
}
public override void OnOpen()
{
}
public override void OnMessage(string message)
{
}
}
}

Observe the Get function in the preceding code sample. The ASP.NET engine maps this function to a URL
and the controller with a GET request. After the initial handshake:

o The WebSocket library establishes the web socket at the client and server end.

o The application passes the handling of communications to the ChatWebSocketHandler class


inherited from the WebSocketHandler class.

Whenever the client system sends a message to the service application hosted on the web server, the
message triggers the OnMessage function. The OnMessage function enables you to respond to the
message.
To ensure that a message triggers the OnMessage function, you need to add the following lines of code
to the web.config file.

<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
MCT USE ONLY. STUDENT USE PROHIBITED
15-8 Handling Requests in ASP.NET MVC 4 Web Applications

</configuration>

On the client side, you can use the WebSocket object to establish two-way communication between the
client and server systems. The following lines of code show how to use the WebSocket object in JScript.

$(document).ready(function () {
websocket = new WebSocket('ws://localhost/api/service1');
websocket.onopen = function () {
};
websocket.onerror = function (event) {
};
websocket.onmessage = function (event) {
};
websocket.send('Hello');
websocket.close();
});

The preceding code sample shows how to add JScript functions to respond to events. The following list
describes some common JScript functions.

o onmessage. You can use this function when the client system receives a message from the server.

o onopen. You can use this function when the WebSocket library establishes the web socket.

o onerror. You can use this function when an error occurs.

To send a message in JScript, you can use the WebSocket.send() function. After all communications
between the client and server systems end and you no longer need the web socket connection, you can
use the WebSocket.close() function. This function helps close the web socket communication channel
between the client and server systems.

Question: What is the purpose of using Web APIs to create a service for handling web sockets?

Discussion: Scenarios for Web Sockets


Consider the following scenarios. In each case,
discuss with the rest of the class to determine
when you should use web sockets.

o You are creating a photo sharing


application, and you want to enable each
user to discuss photos and cycling trips
with their friends. When users upload
photos, you need to ensure that the
application renders them immediately
visible to all friends.

o You are creating a web-based


application. You want the API to display
the product information that is stored in the database.

o You are creating a web-based application to display the latest stock price of your company.

o You are developing an application to download large files from the server to the client.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-9

Demonstration: How to Add a Chat Room to a Web Application by using


SignalR
In this demonstration, you will see how to add a simple chat room to a web application by using SignalR.

Demonstration Steps
1. Add a new class file named ChatHub to the OperasWebSite project.

2. In the ChatHub.cs file, add a reference to the following namespace:

o Microsoft.AspNet.SignalR

3. Ensure that the ChatHub class inherits from the Microsoft.AspNet.SignalR.Hub class.

4. In the ChatHub class, create a new method by using the following information:

o Scope: public

o Return type: void

o Name: Send
o First parameter: a string named name

o Second parameter: a string named message

5. In the Send method, call the Clients.All.broadcastMessage() method and pass the name and
message parameters.

6. In the Chat view for the Home controller, within the final <script> element, add an anonymous
function that runs when the page has loaded completely.
7. In the new anonymous function, create a new variable named chat, and set the value of this variable
to $.connection.chatHub.

8. Set the chat.client.broadcastMessage value to be a new anonymous function that accepts two
parameters, name and message.

9. In the new anonymous function, create a new variable named listItem. Set the variable to a new
HTML <li> element that includes the name and message values.

10. Append the listItem element to the page element with the ID, discussion.

11. After the broadcastMessage function, prompt users to enter their names. Store the value entered in
a variable named displayname.

12. Set the control focus to the page element with ID, chat-message.

13. Create an anonymous function that runs when the $.connection.hub.start() method is done.

14. In the new anonymous function, create a new anonymous function that runs when the element with
ID sendmessage is clicked.

15. In the new anonymous function, call the chat.server.send() method. Pass the displayname variable
and the value of the element with ID chat-message as parameters.
16. Set the value of the element with ID chat-message to an empty string and ensure that element has
focus.

17. Start the web application in debugging mode. Send a message of your choice by using Operas chat
room. Use the following name to navigate to the chat room:

o Name: Rebecca Laszlo


MCT USE ONLY. STUDENT USE PROHIBITED
15-10 Handling Requests in ASP.NET MVC 4 Web Applications

18. Open another Internet Explorer window, and browse to the Operas website.

19. Send a message of your choice by using Operas chat room. Use the following name to navigate to
the chat room:

o Name: Elisa Graceffo

20. Switch to the first instance of Internet Explorer.

21. Stop debugging and close Visual Studio.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-11

Lab: Handling Requests in ASP.NET MVC 4 Web


Applications
Scenario
The Adventures Works board and managers are pleased with the Photo Sharing application, but have
requested that interactivity should be maximized to encourage users to register and participate fully in
the community. Therefore, you have been asked to add chat functionality to the application.
Authenticated members should be able to start a chat on a particular photo from the Display view. Chat
rooms for each photo should be separated from each other. Users in the chat room should be able to
send a message to all other users in that chat room, and they should be able to see all the messages that
have been sent since they joined the chat room.

You have decided to use SignalR to implement the chat room over Web
Sockets.Objectives
After completing this lab, you will be able to:

 Install SignalR in an ASP.NET MVC 4 web application.

 Configure SignalR on the server and create a SignalR hub.


 Link to the required script files for SignalR in an MVC view.

 Script SignalR connections and send messages to groups.

Lab Setup
Estimated Time:

45 minutes

Virtual Machine: 20486A-SEA-DEV11

User Name: Admin

Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.
Before initiating this lab, perform the following steps:

a. Apply the Snapshot of the virtual machine, 20486A-SEA-DEV11, that was taken after completing
the lab in module 13.

b. Navigate to Allfiles (D):\Labfiles\Mod


13\Starter\PhotoSharingApplication\PhotoSharingApplication, and then copy the
web.config file.

c. Navigate to Allfiles (D):\Labfiles\Mod


15\Starter\PhotoSharingApplication\PhotoSharingApplication, and then paste the
web.config file.

d. Enable the Allow NuGet to download missing packages during build option in Package
Manager.
MCT USE ONLY. STUDENT USE PROHIBITED
15-12 Handling Requests in ASP.NET MVC 4 Web Applications

Exercise 1: Creating a SignalR Hub


Scenario
Before you can write JScript code on the client to connect to SignalR, you must configure and code a
SignalR Hub on the web server.

In this exercise, you will:

 Install SignalR in the Photo Sharing application.

 Configure routing.
 Create a SignalR hub to accept messages from clients and to forward those messages to other clients
who are chatting about the same photo.

The main tasks for this exercise are as follows:

1. Install SignalR.

2. Create a Hub class.

3. Configure SignalR routes.

 Task 1: Install SignalR.


1. Start the virtual machine, and log on with the following credentials:

o Virtual machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Open the PhotoSharingApplication.sln file from the following location:

o File location: Allfiles (D):\Labfiles\Mod15\Starter\PhotoSharingApplication

3. In the NuGet Package Manager, search for the following pre-release package:

o Microsoft.aspnet.signalr

4. Install the Microsoft ASP.NET SignalR package in the PhotoSharingApplication project.

5. Notice the additions that the NuGet package has made to the project references and the Scripts
folder.

 Task 2: Create a Hub class.


1. Add a new class file named ChatHub.cs to the PhotoSharingApplication.

2. Remove the following namespace references from the ChatHub.cs class file:

o System.Collections.Generic;

o System.Linq;

3. Add the following namespace references to the ChatHub.cs class file:

o System.Threading.Tasks

o Microsoft.AspNet.SignalR

4. Ensure that the ChatHub class inherits from the Microsoft.AspNet.SignalR.Hub class.

5. In the ChatHub class, create a new method by using the following information:

o Scope: public
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-13

o Return type: Task

o Name: Join

o Parameter: an integer named photoId

6. In the Join method, return the result of the Groups.Add() method by using the following
information:
o Connection ID: Context.ConnectionId

o Group name: "Photo" + photoId

7. In the ChatHub class, create a new method by using following information:

o Scope: public

o Return type: Task

o Name: Send
o First parameter: a string named username

o Second parameter: an integer named photoId

o Third parameter: a string named message


8. In the Send method, create a new string variable named groupName and set the value to "Photo"
+ photoId.

9. In the Send method, return the result of the addMessage method for the groupName group by
using the following information:

o Method: Client.Group(groupName).addMessage()

o User name: username

o Message: message

10. Save all the changes.

 Task 3: Configure SignalR routes.


1. In the Global.asax code-behind file, add a reference to the Microsoft.AspNet.SignalR namespace.
2. In the Application_Start() method, immediately after the RegisterAllAreas() code, call the
RouteTable.Routes.MapHubs() method.

3. Save all the changes.

Results: After completing this exercise, you will be able to install SignalR in an MVC 4 web application,
configure routes for SignalR, and create a hub to receive and forward simple text messages.

Exercise 2: Creating a Photo Chat View


Scenario
Now that you have set up and configured SignalR and a SignalR hub on the server side, you must use
JScript and the SignalR JScript library to send and receive messages on the client side.

In this exercise, you will:


MCT USE ONLY. STUDENT USE PROHIBITED
15-14 Handling Requests in ASP.NET MVC 4 Web Applications

o Create a new MVC controller action and Razor view to display the chat user interface for a
particular photo.
o Link to the JScript libraries that SignalR requires and write client-side script to call the Join() and
Send() methods on the hub.

o Test the chat functionality.

The main tasks for this exercise are as follows:

1. Create a chat action and view.

2. Link to the chat view.

3. Link to JScript files.

4. Script SignalR connections.

5. Script SignalR messages.

6. Test the chat room.

 Task 1: Create a chat action and view.


1. Add a new action to the PhotoController class by using the following information:

o Annotation: [Authorize]
o Scope: public

o Return type: ActionResult

o Name: Chat
o Parameter: an integer named id

2. Create a new Photo object named photo and get the photo value by passing the id parameter to
the context.FindPhotoById() method.

3. If the photo object is null, return an HTTP Not Found status code.

4. At the end of the action, return the Chat view and pass the photo object as the model class.

5. Add the following view file to the Views/Photo folder:


o Allfiles (D):\labfiles\Mod15\Chat View\Chat.cshtml

6. Save all the changes.

 Task 2: Link to the chat view.


1. In the Display.cshtml view file, after the DIV element with ID addtofavorites, add a new DIV
element, with ID chataboutthisphoto.

2. In the new DIV element, render a link to the Chat view by using the following information:

o Helper: Html.ActionLink()

o Text: Chat about this photo

o View: Chat

o Route values: pass the Model.PhotoID value to the id parameter.

3. Start the web application in the debugging mode, display a photo of your choice, and then click
Chat.

4. Log on with the following credentials:


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-15

o User name: David Johnson

o Password: Pa$$w0rd2

5. Attempt to send a chat message.

6. Stop debugging.

 Task 3: Link to JScript files.


1. Add a new SCRIPT element at the end of the Chat.cshtml view file, with type text/javascript.

2. In the new SCRIPT element, create a new variable named username and use the
User.Identity.Name property to set the value for the variable.

3. Create a second new variable named photoid and use the Model.PhotoID property to set the value
for the variable.

4. Add a new SCRIPT element, with type text/javascript, and an empty src attribute.

5. In the new script element, use the Url.Content() helper to set the src attribute to the following path:
o ~/Scripts/jquery.signalR-1.0.0-rc2.js

6. Add a new SCRIPT element, with type text/javascript, and an empty src attribute.

7. In the new script element, use the Url.Content() helper to set the src attribute to the following path:

o ~/signalr/hubs

8. Add a new SCRIPT element, with type text/javascript, and an empty src attribute.

9. In the new script element, use the Url.Content() helper to set the src attribute to the following path:
o ~/Scripts/ChatRoom.js

10. Save all the changes.

 Task 4: Script SignalR connections.


1. Add a new JScript file named ChatRoom.js to the Scripts folder.
2. In the new JavaScript file, use jQuery to create an anonymous function that runs when the page loads.

3. Use the $.connection.chatHub property to obtain the ChatHub you already created, and then store
the hub in a variable named chat.

4. Use jQuery to set the initial focus on the element with ID chat-message.

5. Create an anonymous function that runs when the $.connection.hub.start() method is done.

6. Call the chat.server.join() function on the SignalR hub, pass the photoid value as a parameter, and
then create an anonymous function that runs when the function is done.

7. Save all the changes.

 Task 5: Script SignalR messages.


1. In the ChatRoom.js JScript file, after creating and instantiating the chat variable set the
chat.client.addMessage property to be a new anonymous function with two parameters, name and
message.

2. In the new function, create a variable named encodedName, and use jQuery to set this variable to a
new <div> with the name parameter as its HTML content.
MCT USE ONLY. STUDENT USE PROHIBITED
15-16 Handling Requests in ASP.NET MVC 4 Web Applications

3. In the new function, create a variable named encodedMessage, and use jQuery to set this variable to
a new <div> with the message parameter as its HTML content.
4. Create a new variable named listItem. Set the value of this variable to an HTML LI element that
includes the encodedName and encodedMessage variables.

5. Append the listItem element to the page element with ID discussion.

6. In the function that runs when the client.server.join() method is done, create an anonymous
function that runs when the button with ID sendmessage is clicked.

7. In the new anonymous function, call the chat.server.send() method by using the following
information:

o User name: username

o Photo ID: photoid

o Message: use the value of the element with ID chat-message

8. Use jQuery to obtain the element with ID chat-message, set its value to an empty string, and give it
the focus.

9. Save all the changes.

 Task 6: Test the chat room.


1. Start the web application in debugging mode and log on with the following credentials:

o User name: David Johnson


o Password: Pa$$w0rd2

2. Browse to the chat page for Sample Photo 1.

3. Send a message of your choice and observe the results.


4. Start a new instance of Windows Internet Explorer, and browse to the Photo Sharing application
home page.

5. Register a new user account with the following credentials:

o User name: Mark Steele

o Password: Pa$$w0rd

6. Browse to the chat page for Sample Photo 1, and then send a message of your choice.
7. Switch to the first instance of Internet Explorer, and then send a second message of your choice.
Observe the messages sent between the two users.

8. Stop debugging and close Visual Studio.

Results: After completing this exercise, you will be able to create MVC controller actions and views to
display a user interface for SignalR functionality, link to the JScript libraries that SignalR requires, and use
JScript to connect to a SignalR hub and send messages.

Question: In the chat functionality that you created, each photo in the Photo Sharing
application has a separate chat room. How is this separation possible with one SignalR hub?

Question: In Exercise 2, you wrote JScript code that called the chat.server.join() and
chat.server.send() functions. In which script file are these functions defined?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 15-17

Module Review and Takeaways


Most web applications do not limit themselves to rendering HTML content. They require functionalities
that support custom logic such as custom authentication. To suit such cases, you can use HTTP modules
and HTTP handlers to run custom logic before or after rendering a webpage. The web socket technology
in MVC 4 provides support for two-way communication between client and server systems. This
technology is useful for web applications that require constant updates between the client and server
systems.

Real-world Issues and Scenarios


You create an application that obtains the latest product pricing information from the internal database.
The pricing is constantly updated every time business users feel the need for updates. Therefore, you need
to ensure that the application updates pricing information every five minutes. In such cases, you can to
use the web socket technology to implement price update. You can also add code to download the
product image stored in product database by using a generic handler (*.ashx file).
MCT USE ONLY. STUDENT USE PROHIBITED
15-18 Handling Requests in ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
16-1

Module16
Deploying ASP.NET MVC 4 Web Applications
Contents:
Module Overview 16-1 

Lesson 1: Deploying a Web Application 16-2 

Lesson 2: Deploying an ASP.NET MVC 4 Web Application 16-7 

Lab: Deploying ASP.NET MVC 4 Web Applications 16-11 

Module Review and Takeaways 16-15 

Module Overview
After developing an application, you will need to deploy the application to the production environment
to make the application available to users. The configuration or deployment method of each application
varies based on the setup of the production environment. ASP.NET MVC applications have some
additional considerations such as the configuration needed for IIS.

Objectives
After completing this module, you will be able to:
o Configure and deploy an ASP.NET web application.

o Deploy an ASP.NET MVC 4 web application.


MCT USE ONLY. STUDENT USE PROHIBITED
16-2 Deploying ASP.NET MVC 4 Web Applications

Lesson 1
Deploying a Web Application
The considerations for deploying a web application to a web server are different for single and multi-
server configurations. For example, in multi-server web farms you need to ensure that the state
information is available on all web servers. You also need to ensure that the dependencies needed for
hosting ASP.NET MVC 4 web applications are present in the web farm. If any of the application
dependencies are missing on the production web servers, some or all of your applications may not
function and users may encounter errors. You need to apply special considerations while deploying web
applications on Windows Azure. Deploying a web application on Windows Azure provides advantages
such as high availability and flexibility.

Lesson Objectives
After completing this lesson, you will be able to:
o Ensure that dependencies are present for hosting ASP.NET MVC4 applications.

o Deploy an ASP.NET MVC4 web application to a single-server web farm.

o Deploy an ASP.NET MVC4 web application to a multi-server web farm.


o Deploy an ASP.NET MVC 4 web application on Windows Azure.

ASP.NET MVC 4 Dependencies


ASP.NET MVC 4 is built on the ASP.NET
Framework 4.0. Each web application requires a
range of different components to be present on
the web server, to function appropriately.
Examples of common requirements include:

o The ASP.NET Framework 4.0 Common


Language Runtime (CLR). The CLR runs
any managed code such as C# classes. All
ASP.NET web applications require the
CLR, and you can install the CLR with
Internet Information Services (IIS).

o The ASP.NET MVC 4 runtime. The


ASP.NET MVC 4 runtime locates controllers and actions to handle each request that the web
application receives, and returns the compiled webpages to users. You can install the MVC
runtime in your web application by installing the MVC 4 NuGet package. All MVC web
applications require the MVC runtime.

o A Database server. Most web applications use a database to store information such as product
details, customer details, images, and other entities. You need to ensure that the production
environment supports the database that you used during development. For example, if you use
SQL Compact during development, you need to ensure that Microsoft SQL Compact is installed
on the production server. Alternatively, you can migrate the database to Microsoft SQL Server.

o Entity Framework. If you use Entity Framework to model data in your web application, you need
to ensure that you deploy the framework with your application.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 16-3

o Membership Providers. If your application uses membership providers to store user accounts and
roles, you need to ensure that these providers are available on the server. The .NET Framework
includes the SQL Server Membership Provider. If you use the Universal Membership Providers by
installing the NuGet package, you need to ensure that you include the NuGet package with your
deployed application.
Question: Why should we use specific mapping for IIS 6.0 or earlier or IIS 7.0 in classic mode?

Deploying Web Applications to Web Servers


Before you deploy a web application to a
production IIS web server, you must create and
configure the IIS application and the folder that
hosts the application.

Creating an IIS Web Application


You need to host ASP.NET MVC 4 web
applications in an IIS web folder that is marked as
a web application starting point. You can use the
Internet Service Manager tool to configure this
setting on the top-level folder of the web
application. When you mark a web folder as a web
application, you enable IIS to recognize that the
content of the folder is ASP.NET and requires the ASP.NET runtime engine to process each request.

After creating an application, you need to configure the application to use .NET 4.0 or above. This practice
enables ASP.NET MVC 4 to run, because the MVC runtime components are part of the .NET framework.

IIS Application Pools


In IIS, web applications are run within a context known as an application pool. You can run many
applications in the same application pool or install an application in its own isolated pool. Applications
that run in the same application pool share the following resources:

o Worker Process. The application uses a single execution process to run all the applications in a
pool.
o Memory. The applications share a single memory range, because the applications in a pool share
a single worker process.

o Identity. The applications within a single pool share a user account.

You can place two or more applications in an application pool, and thereby reduce the total memory
usage, because the applications share memory. However, this arrangement can reduce the reliability of
each application, because a malfunctioning application can affect all the applications in the pool. To
maximize the reliability of an application, you should install the application in an isolated pool that runs
only one specific application.

The shared identity within an application pool affects the way the applications authenticate resources
such as database servers when using integrated authentication. Each application within a pool shares a
single user account. If each application has a separate database, you need to set permissions, to enable
the user accounts to access all the databases. If you separate applications into isolated pools, you can use
a different user account for each application. This practice can enhance the security of the application,
because each account can access only one database.
MCT USE ONLY. STUDENT USE PROHIBITED
16-4 Deploying ASP.NET MVC 4 Web Applications

Deploying the Application


When the IIS web application is ready, you need to copy the web application files to IIS. These files include
all the model classes, controllers, views, script files, style sheets, images, and any other content in the
Microsoft Visual Studio ASP.NET MVC 4 project. You can use the deployment tools available in Microsoft
Visual Studio or other programs such as FTP, to copy these files. When the copy operation is complete,
the web application is ready for deployment.

Question: What is the purpose of configuring additional application pools?

Deploying Web Applications to Multi-Server Farms


You can deploy a web application to a multi-
server web farm to increase performance,
resilience, and reliability. In a multi-server web
farm, a group of two or more web servers host a
single web application. All the servers in the farm
share a single server name, such as
www.contoso.com. Windows Network Load
Balancing or dedicated hardware load balancers
distribute browser requests to the servers.

A multi-server farm usually has greater capacity


than a single web server, because the multi-server
farm almost equally shares the load amongst
several servers. Multi-server farms also increase reliability. When a server fails for any reason, the load
balancing mechanism automatically directs requests to anothe r server that remains online.
If you decide to deploy your web application to a multi-server farm you need to perform the following
steps. To complete these steps, you may need to work with the web server administrator who is
responsible for the server farm:
1. Create IIS applications and application pools on each server. This step is the same as configuring the
applications and application pools in a single-server scenario. However, you need to perform this step
on every server in the farm. You also need to ensure that applications and application pools have the
same configuration, throughout the farm.

2. Create a matching IIS configuration on each server. IIS configuration options, such as any encryption
certificates, file extensions, and optional components, should be identical on every server in the farm.
3. Use external hosted session state or session affinity. You can configure ASP.NET web applications to
store session state in the Windows State Service or a database. You should use this technique in a
multi-server farm, because each request from a single user session might be directed to a different
web server. Alternatively, by configuring session affinity, you can ensure that all requests from a user
session are always sent to the same web server. If you do not use external hosted session state or
session affinity, the user preference on one web server may get lost when a request is sent to another
web server.

4. Configure the machineKey element in the web.config file. If you are using external hosted session
state, you can encrypt the connection between the web servers and the State Service server or
database server. This technique improves security by protecting session state during network
transmissions. You can configure this encryption by setting up the machineKey element in the
Windows registry.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 16-5

Additional Reading: For more information about configuring the machineKey element,
go to: http://msdn.microsoft.com/en-us/library/vstudio/w8h3skw9(v=vs.100).aspx

Question: What is the purpose of configuring the machineKey element in the web.config file?

Deploying Web Applications on Windows Azure


You can deploy any ASP.NET web application,
including ASP.NET MVC 4 web applications, on
Windows Azure. By choosing Windows Azure as a
platform, you can use a highly-available and
flexible infrastructure that you can optimize to
cope with intense load. You have a choice of
subscriptions which can help you to pay only for
the traffic that your web application uses.

Deploying a web application on Windows Azure is


different from deploying the application on on-
premise versions of IIS. To deploy an application
on Windows Azure, you need to perform the
following steps:

1. Create a new web application in the Windows Azure management portal.


2. Download and save a publishing profile for the new web application.

3. In Microsoft Visual Studio, open your web application solution. Then, start the Publish wizard and
import the publishing profile.
4. Ensure that you select the correct connection strings for Entity Framework connections, service
database connections, and any other database connections.

5. Complete the Publish wizard.


6. Observe that Microsoft Visual Studio publishes the web application to Windows Azure.

When the process is complete, you can access the Windows Azure-hosted web application from any
internet-connected browser.

Note: At the time of writing, Windows Azure web applications are still in preview, within
the Windows Azure platform.

Windows Azure Web Applications and Windows Azure Cloud Services


Instead of publishing an ASP.NET MVC web application as a web application in Windows Azure, you can
choose to build and publish a Windows Azure cloud service. A cloud service is a Windows Azure
component that you can use to build a flexible multi-tiered application, with a discrete architecture.

In a Windows Azure web application, all components of your web application run in a single process. You
can administer the web application as a single object.

Windows Azure cloud services help separate user interface logic that you build in a web-role project, from
business logic that you build in a worker process project. This separation provides greater flexibility, and
enables calling the business logic from other processes, such as mobile applications. You can separately
MCT USE ONLY. STUDENT USE PROHIBITED
16-6 Deploying ASP.NET MVC 4 Web Applications

administer web roles and worker processes, and run them in separate processes. However, to publish a
web application as a cloud service with separate web roles and worker roles, you need to download and
install the Windows Azure SDK. You can download and install the Windows Azure SDK by using the Web
Platform Installer. You can use the Windows Azure cloud service templates as the starting point for your
application.
Question: How does Windows Azure cloud services help enhance the flexibility of an application?

Demonstration: How to Create a Windows Azure Website


In this demonstration, you will see how to create a new database and a new website in Windows Azure.

Demonstration Steps
1. Start Internet Explorer and sign in to the Windows Azure Portal by using the following information:

o URL: http://www.windowsazure.com

o Microsoft account: <your username>


o Password: Pa$$w0rd

2. Create a new website and a new database in Windows Azure by using the following information:

o URL: <your username>operas

o Region: <A region near you>

o Database: Create a new SQL database

o DB Connection String Name: OperasDB


o Database Name: OperasDB

o Database Server: New SQL Database Server

o Logon name: <your first name>

o Password: Pa$$w0rd

3. Close Internet Explorer.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 16-7

Lesson 2
Deploying an ASP.NET MVC 4 Web Application
Deploying ASP.NET MVC applications is similar to deploying other ASP.NET applications. This process is
simple and straightforward. However, you need to update the configuration of the application, for use in
the production environment. Microsoft Visual Studio includes features that simplify the process of
updating the configuration of the application. You can also automate the deployment process by using
deployment tools that are available in Microsoft Visual Studio.

Lesson Objectives
After completing this lesson, you will be able to:

o Reconfigure an ASP.NET MVC application to deploy on a production web server.

o Use the Bin Deploy method to deploy the .NET Framework libraries along with your web
application.
o Use the deployment tools in Visual Studio.

Reviewing Configuration for Production


Each ASP.NET MVC web application includes a
web.config file. In addition to this file, Microsoft
Visual Studio creates two variance files to specify
deployment transformations to the web.config
file. You can use these variance files for different
build configurations. The variance files include:
o The web.release.config file. Stores changes
that Microsoft Visual Studio applies to
the web.config file, when you compile the
application in the Release mode.

o The web.debug.config file. Stores changes


that Microsoft Visual Studio applies to
the web.config file, when you compile the application in the Debug mode.

Microsoft Visual Studio can automatically transform the web.config file, for use in different environments.
Before you publish the application, you can remove the debug attribute from the web.config file. The
following code shows how to remove the debug attribute from the web.config file by using a deployment
transformation.

<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>

The preceding code sample uses the xdt:Transform attribute to modify the existing web.config file and
produce the published web.config file. You can also use the Insert value to add additional elements to
the resultant web.config file. For example, the following code shows how to insert a new connection string
setting in the resultant web.config file.

<connectionStrings>
MCT USE ONLY. STUDENT USE PROHIBITED
16-8 Deploying ASP.NET MVC 4 Web Applications

<add name="DemoConnStr" connectionString="Data Source=|DataDirectory|demo.sdf"


providerName="System.Data.SqlServerCe.4.0" xdt:Transform="Insert"/>
</connectionStrings>

Question: How can you configure the web.config file for publishing to a production environment without
using web.config configuration files?

Using Bin Deploy


ASP.NET MVC applications depend on a range of
.NET assemblies; these assemblies ensure that the
application MVC applications depend on a range
of .NET assemblies; these assemblies ensure that
the application functions as required. These
assemblies are DLL files available in your project
references. The assembly files are usually already
present on the web server, because they are
included as part of the MVC library installation.
However, sometimes the web server
administrators do not install all .NET framework
assemblies. Microsoft Visual Studio provides the
Bin Deploy feature to copy all depending assembly files into a folder within the deployed web application.
This feature helps deploy dependencies on the server.

You can use the Add Deployable Dependencies feature to generate the _bin_deployableAssemblies
folder. This folder includes the following libraries:

o Microsoft.Web.Infrastructure

o System.Web.Helpers

o System.Web.Mvc

o System.Web.Razor

o System.Web.WebPages

o System.Web.WebPages.Deployment

o System.Web.WebPages.Razor
You can choose a specific set of libraries to include in Microsoft Visual Studio. This practice enables you to
ensure that Microsoft Visual Studio deploys the:

o Right assemblies.
o Right versions of the assemblies.

You can then copy the library to the bin folder of the production server, after adding it to the library of
your project.

Question: Why should we use the deployable assembly as part of the deployment plan?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 16-9

Using Visual Studio 2012 Deployment Tools


The Publish feature helps generate a copy of the
web application, when the application is ready for
deployment in the production environment. The
feature includes three methods to deploy the
application

o File Share
o FTP

o Web Deploy

In the Publish web application page, you can


publish the application by using File Share or
FTP. You can use File Share for servers to which
you have direct network access. Alternatively, you can use File Share to publish a web application first to
a local folder. Then, you can manually copy the files to the web server. File Share also allows you to
specify a profile to store all deployment options. This practice eliminates the need to specify the
deployment options every time you publish the application.

If you select Web Deploy, you can specify database settings, such as connection strings, in the publishing
wizard. These settings will override the settings in the web.config file, in the target environment. Web
Deploy also includes tools to automatically update the schema of the database. These tools eliminate the
need to manually update the schema. The publishing tool detects all schema changes and generates
scripts to apply those changes to the published database. If your development and production web
servers are isolated from each other, you can run these generated scripts on the production environment.
This practice replicates schema changes on a server where Microsoft Visual Studio is not installed.

The following image shows the Publish web application page.

FIGURE 16.1:THE PUBLISH WEB APPLICATION PAGE


MCT USE ONLY. STUDENT USE PROHIBITED
16-10 Deploying ASP.NET MVC 4 Web Applications

Question: What is the benefit of using the Web Deploy publish tool?

Demonstration: How to Deploy a Website to Windows Azure


In this demonstration, you will see how to obtain a publish profile from Windows Azure and use it to
publish a website from Visual Studio.

Demonstration Steps
1. Start Internet Explorer and sign in to the Windows Azure Portal by using the following information:

o URL: http://www.windowsazure.com

o Microsoft account: <your username>

o Password: Pa$$w0rd

2. Download the publish profile for the <your username>operas website.

3. Start the Publish wizard for the OperasWebSite project, use the <your
username>operas.azurewebsites.net.PublishSettings file that you just downloaded, and then
validate the connection.
4. Accept the default database settings and preview the changes that the publish operation will make.

5. Publish the website to Windows Azure.

6. Browse the list of operas.


7. Close the Internet Explorer and the Visual Studio windows.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 16-11

Lab: Deploying ASP.NET MVC 4 Web Applications


Scenario
You have completed the development and testing of the photo sharing application. Your managers and
senior developers have signed off the project, and have requested you to deploy the application to the
Adventure Works Windows Azure account.

Objectives
After completing this lab, you will be able to:

o Prepare an MVC web application for deployment.

o Deploy an MVC web application to Windows Azure.

Lab Setup
Estimated Time: 30 minutes

Virtual Machine: 20486A-SEA-DEV11

User name: Admin


Password: Pa$$w0rd

Note: In Hyper-V Manager, start the MSL-TMG1 virtual machine if it is not already running.

In this lab, you will create a web application in Windows Azure that runs in Free mode. Free mode is an
excellent tool for testing and running small, non-intensive web applications. However, there is a limit of
165 MB of data transfer per day on the Free mode tool. After completing Exercise 2, you are encouraged
to further test the deployed web application. However, if you do a lot of extra testing, you may encounter
the limit and the application may become unavailable.
Also, before initiating this lab, perform the following steps:

1. Apply the Snapshot of the virtual machine, 20486A-SEA-DEV11, that was taken after completing the
lab in module 13.
2. Enable the Allow NuGet to download missing packages during build option in Package
Manager.

Exercise 1: Deploying a Web Application to Windows Azure


Scenario
In this exercise, you will:

o Reconfigure the Photo Sharing application for release deployment.

o Configure the Entity Framework initializer class, which fills the database with initial data, and
ensure that the build configuration and connection strings are correct.

o Create a new web application in Windows Azure and deploy the Photo Sharing application to the
new site.

The main tasks for this exercise are as follows:

1. Prepare the Photo Sharing application project for deployment.

2. Create a new website in Windows Azure.

3. Deploy the Photo Sharing application.


MCT USE ONLY. STUDENT USE PROHIBITED
16-12 Deploying ASP.NET MVC 4 Web Applications

 Task 1: Prepare the Photo Sharing application project for deployment.


1. Start the virtual machine, and log on with the following credentials:

o Virtual machine: 20486A-SEA-DEV11

o User name: Admin

o Password: Pa$$w0rd

2. Open the Photo Sharing application solution from the following location:

o File location: Allfiles (D):\Labfiles\Mod16\Starter\PhotoSharingApplication

3. In the Build properties of the PhotoSharingApplication project, select the Release configuration.

4. Log on to the Windows Azure portal by using the following information:


o URL: http://www.windowsazure.com

o User name: <Your Windows Live account name>

o Password: <your password>

5. Copy the ADO.NET connection string for the PhotoSharingDB to the clipboard.

6. In the Web.config file, paste the connection string into the connectionString attribute of the
PhotoSharingDB connection string. Set the password in the pasted connection string to Pa$$w0rd.

7. In the Windows Azure portal, copy the ADO.NET connection string for the
PhotoSharingAppServices to the clipboard.

8. In the Web.config file, paste the connection string into the connectionString attribute of the
PhotoSharingDB connection. Set the password in the pasted connection string to Pa$$w0rd.

9. Log on to the Bing Maps Account Center web application by using the following information:

o URL: https://www.bingmapsportal.com

o User name: <Your Windows Live account name>

o Password: Pa$$w0rd

10. Copy the Bing Maps key.

11. Replace the text, {Your Bing Key}, in the MapDisplay.js file, with the copied key.

12. Save all the changes.

 Task 2: Create a new website in Windows Azure.


1. In Windows Azure, create a new website by using the following information:

o URL: <your username>PhotoSharing

o Region: <a region near you>

o Database: PhotoSharingDB

o Database connection string name: PhotoSharingDB.

o Database user: <Your first name>

o Password: Pa$$w0rd
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 16-13

 Task 3: Deploy the Photo Sharing application.


1. In Windows Internet Explorer, download the publish profile for the <your username>PhotoSharing
web application.

2. In Microsoft Visual Studio, start the Publish Web wizard and import the publish profile you just
downloaded.

3. For the Photo Sharing Context database connection, select the PhotoSharingDB connection string.

4. For the Photo Sharing DB database connection, select the PhotoSharingDB connection string.

5. For the UsersContext database connection, select the PhotoSharingDB connection string.

6. Preview the files that require changes, and then publish the web application.

Results: After completing this exercise, you will be able to prepare an ASP.NET MVC web application for
production deployment, create a web application in Windows Azure, and deploy an MVC web application
to Windows Azure.

Exercise 2: Testing the Completed Application


Scenario
You have completed and fully deployed the Photo Sharing web application in Windows Azure. Now, you
want to perform some final functionality tests before you confirm the completion of the application to
your manager.
The main tasks for this exercise are as follows:

1. Add a photo and a comment.

2. Use the slideshow and favorites options.


3. Test the chat room.

 Task 1: Add a photo and a comment.


1. Log on to the Adventure Works Photo Sharing web application by using the following information:

o User name: David Johnson

o Password: Pa$$w0rd2

2. Add a new photo to the web application by using the following information:

o Title: Test New Photo


o Photo: Allfiles (D):\Labfiles\Mod16\Sample Photos\track.JPG

o Description: This is the first photo added to the deployed web application

o Location: Devon, UK
3. Display the new photo and check if the latitude and longitude data has been added by the Location
Checker WCF service.

4. Add a new comment to the new photo by using the following information:

o Subject: Functionality Check

o Body: The location checker service has correctly resolved the coordinates
MCT USE ONLY. STUDENT USE PROHIBITED
16-14 Deploying ASP.NET MVC 4 Web Applications

 Task 2: Use the slideshow and favorites options.


1. Examine the Slideshow webpage and check that the display of all photos functions as designed.

2. Add three photos to your favorites list.

3. View the Favorites slideshow.

 Task 3: Test the chat room.


1. Enter the chat room for Sample Photo 1.

2. Send a message.

3. Start a new instance of Internet Explorer and browse to the Photo Sharing application by using the
following information:

o URL: http://<yourusername>PhotoSharing.azurewebsites.net

o User name: Mark Steele

o Password: Pa$$w0rd

4. Enter the chat room for Sample Photo 1.


5. Send a message and switch to the first instance of Internet Explorer. Send a third message.

6. Close Internet Explorer and Visual Studio.

Results: After completing this exercise, you will be able to confirm that all the functionalities that you
built in the Photo Sharing application run correctly in the Windows Azure deployment.

Question: Why is it unnecessary to use bin deployment in this lab?

Question: Why is it not necessary to use Entity Framework Code-First migrations to migrate
data to Windows Azure SQL Database in this lab?
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications 16-15

Module Review and Takeaways


Deployment is usually the last task that developers perform; however, this is the task that developers do
not spend much time on. The target environment usually impacts the deployment procedure. In the case
of Windows Azure, service packages allow the management portal to deploy the package to the virtual
machine that hosts the workload.

To simplify the deployment process, Microsoft Visual Studio consists of a number of tools that help
developers generate files for deployment. These tools also help handle the database during deployment.
You can also generate the web.config file based on the environment that it deploys to.
MCT USE ONLY. STUDENT USE PROHIBITED
16-16 Deploying ASP.NET MVC 4 Web Applications

Course Evaluation

Keep this evaluation topic page if this is the final module in this course. Insert the Product_Evaluation.ppt
on this page.

If this is not the final module in the course, delete this page
Your evaluation of this course will help Microsoft understand the quality of your learning experience.

Please work with your training provider to access the course evaluation form.

Microsoft will keep your answers to this survey private and confidential and will use your responses to
improve your future learning experience. Your open and honest feedback is valuable and appreciated.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L01-1

Module01: Exploring ASP.NET MVC 4


Lab: Exploring ASP.NET MVC 4
Exercise 1: Exploring a Photo Sharing Application
 Task 1: Register a user account.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Visual Studio 2012.

5. On the taskbar, right-click the Start Page - Microsoft Visual Studio icon, and then click Pin this
program to taskbar.
6. On the FILE menu of the Start Page – Microsoft Visual Studio window, point to Open, and then
click Project/Solution.

7. In the Open Project dialog box, navigate to Allfiles (D):\Labfiles\Mod01\PhotoSharingSample,


click PhotoSharingSample.sln, and then click Open.

8. On the DEBUG menu of the PhotoSharingSample – Microsoft Visual Studio window, click Start
Without Debugging.
9. On the Home page of the http://localhost:<yourportnumber>/ window, in the upper-right corner,
click Register.

Note: In the http://localhost:<yourportnumber>/ window, if a notification bar appears,


click Don’t show this message box again.

10. On the Register page, in the User name box, type <A username of your choice>.

11. On the Register page, in the Password box, type <A password of your choice>, in the Confirm
password box, type <A password of your choice>, and then click Register.

 Task 2: Upload and explore photos.


1. On the Home page of the http://localhost:<yourportnumber>/window, click All Photos.
2. On the Photo Index page, click the Details link corresponding to the “Orchard” image.

3. On the Photo: Orchard page, in the Subject box, type Just a Test Comment.

4. In the Body box, type This is a Sample, and then click Create.

Note: The comment you have added appears in the Comments section.

5. On the Photo: Orchard page of the http://localhost:<yourportnumber>/photo/2 window, click Add


a Photo.

6. On the Create New Photo page, in the Title box, type Fall Fungi.

7. In the Upload new Picture box, click Browse.


MCT USE ONLY. STUDENT USE PROHIBITED
L01-2 Developing ASP.NET MVC 4 Web Applications

8. In the Choose File to Upload dialog box, navigate to Allfiles (D):\ Labfiles\Mod01\Pictures, click
fungi.JPG, and then click Open.

9. In the Description box, type Sample Text, and then click Create.

10. On the Photo Index page, verify that the newly added photo is displayed.

11. On the Photo Index page, click the Details link corresponding to the “Fall Fungi” image.
12. On the Photo: Fall Fungi page, note that the description provided for the photo is updated.

 Task 3: Use slideshows.


1. On the Photo: Fall Fungi page of the http://localhost:<yourportnumber>/photo/6 window, click the
Slideshow tab.

Note: You can view all the images on the Photo Index page. Verify that all the images in
the gallery are displayed in the slideshow.

2. On the SlideShow page of the http://localhost:<yourportnumber>/Photo/SlideShow window, click


All Photos.

3. On the Photo Index page, click the Details link corresponding to the “Fall Fungi” image.

4. On the Photo: Fall Fungi page, click the Add to favorites link.

Note: Verify that the message “The picture has been added to your favorites” is displayed.

5. At the lower-left corner of the Photo: Fall Fungi page, click the Back to List link.
6. On the Photo Index page, click the Details link corresponding to the “Orchard” image.

7. On the Photo: Orchard page, click the Add to favorites link.

Note: Verify that the message “The picture has been added to your favorites” is displayed.

8. At the lower-left corner of the Photo: Orchard page, click the Back to List link.

9. On the Photo Index page, click the Details link corresponding to the “Flower” image.

10. On the Photo: Flower page, click the Add to favorites link.

Note: Verify that the message “The picture has been added to your favorites” is displayed.

11. On the Photo: Flower page of the http://localhost:<yourportnumber>/photo/1 window, click


Favorites.

Note: Verify that all the images that you have added as favorites are displayed in the
slideshow.

 Task 4: Test the authorization.


1. In the upper-right corner of the SlideShow page, click the Log Off link.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L01-3

Note: Before clicking All Photos, on the Privacy tab of the Internet Options dialog box,
click Clear Sites, and then click OK.
After clearing the sites, press F5, or click the Refresh button, before you click All Photos.

2. On the Home page, click All Photos.

3. On the Photo Index page, click the Details link corresponding to the “Fall Fungi” image.

4. On the Photo: Fall Fungi page, in the Comments section, note that the message “To comment you
must log in” is displayed.

5. On the Photo: Fall Fungi page, click All Photos.

6. On the Photo Index page, click the Create New link.

Note: Ensure that you are not able to view your user name in the web application. If you
are able to view your user name, press F5 or click the Refresh button, before proceeding to the
next step.
Note: You are directed to the Logon page, and prompted to enter the logon credentials.

7. In the Windows Internet Explorer window, click the Close button.

8. In the PhotoSharingSample – Microsoft Visual Studio window, click the Close button.

Results:

At the end of this exercise, you will be able to understand the functionality of a photo sharing application,
and implement the required application structure in the Adventure Works photo sharing application.
MCT USE ONLY. STUDENT USE PROHIBITED
L01-4 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Exploring a Web Pages Application


 Task 1: Create a Web Pages application.
1. On the Windows 8 Start screen, click Visual Studio 2012.

2. On the FILE menu of the Start Page - Microsoft Visual Studio window, point to New, and then
click Web Site.

3. In the navigation pane of the New Web Site dialog box, expand Installed, expand Templates, and
then click Visual C#.

4. In the result pane of the New Web Site dialog box, click ASP.NET Web Site (Razor v2), and then
click OK.

5. On the DEBUG menu of the WebSite1 – Microsoft Visual Studio window, click Start Debugging.

6. On the home page of the http://localhost:<yourportnumber>/ window, click Contact to review its
contents.

7. In the Windows Internet Explorer window, click the Close button.

 Task 2: Explore the application structure.


1. In the Solution Explorer pane of the WebSite1(1) - Microsoft Visual Studiowindow, click
Web.config.

2. In the Web.config code window, locate the <DbProviderFactories> element, and then locate the
<add> element.

3. In the <add> element, note that the value of the description attribute is .NET Framework Data
Provider for Microsoft SQL Server Compact.

4. In the Solution Explorer pane of the WebSite1(1) - Microsoft Visual Studio window, click
Default.cshtml.

5. In the Default.cshtml code window, note that the value of Layout is ~/_SiteLayout.cshtml.

6. In the Solution Explorer pane, click Contact.cshtml.


7. In the Contact.cshtml code window, note that the value of Layout is ~/_SiteLayout.cshtml.

Note: This indicates that the Default.cshtml and the Contact.cshtml files use the same
layout file, ~/_SiteLayout.cshtml.

8. In the Contact.cshtml code window, examine the Razor code and note that there are no links to .css
files.

9. In the Solution Explorer pane, click _SiteLayout.cshtml.

10. In the _SiteLayout.cshtml code window, in the HEAD element, note that there is a link to
~/Content/Site.css.
11. In the Solution Explorer pane, expand Content, and then click Site.css.

12. In the Site.css code window, verify the style sheet for _SiteLayout.cshtml.

 Task 3: Add simple functionality.


1. In the WebSite1(1) – Microsoft Visual Studio window, in the Solution Explorer pane, right-click
WebSite1(1), point to Add, and then click Add New Item.

2. In the Add New Item – WebSite1(1) dialog box, click Web Page (Razor v2).
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L01-5

3. In the Name box of the Add New Item – WebSite1(1) dialog box, type TestPage.cshtml,and then
click Add.

4. In the TestPage.cshtml code window of the WebSite1(1) – Microsoft Visual Studio window, in the
BODY element, type the following code.

<h1>This is a Test Page</h1>

5. On the FILE menu of the WebSite1(1) – Microsoft Visual Studio window, click Save All.

6. In the Solution Explorer pane of the WebSite1(1) – Microsoft Visual Studio window, click
Default.cshtml.

7. In the Default.cshtml code window, in the P element, type the following code.

<a href="~/TestPage.cshtml">
Test Page
</a>

8. On the FILE menu of the WebSite1(1) – Microsoft Visual Studio window, click Save All.
9. On the DEBUG menu of the WebSite2 – Microsoft Visual Studio window, click Start Debugging.

10. In the http://localhost:<yourportnumber>/Default.cshtml window, click Test Page to view its


contents.

Note: The text “This is a Test Page” is displayed on the webpage that you have added.

11. In the Windows Internet Explorer window, click the Close button.

 Task 4: Apply the site layout.


1. In the Solution Explorer pane of the WebSite1(1) - Microsoft Visual Studio window, click
TestPage.cshtml.

2. In the TestPage.cshtml code window, place the mouse cursor before <!DOCTYPE html>, and then
press Enter twice.

3. In the first line of the TestPage.cshtml code window, type the following code block.

@{
}

4. In the Razor code block of the TestPage.cshtml code window, type the following code.

Layout = "~/_SiteLayout.cshtml";

5. On the FILE menu of the WebSite1(1) – Microsoft Visual Studio window, click Save All.

6. On the DEBUG menu of the WebSite1(1) – Microsoft Visual Studio window, click Start
Debugging.

7. In the http://localhost:<yourportnumber> window, click Test Page and note that the standard site
layout and styles have been applied.

8. In the http://localhost:<yourportnumber>/TestPage.cshtml window, click the Close button.

9. In the WebSite1(1) – Microsoft Visual Studio window, click the Close button.
MCT USE ONLY. STUDENT USE PROHIBITED
L01-6 Developing ASP.NET MVC 4 Web Applications

Results: At the end of this exercise, you will be able to build a simple Web Pages application in Visual
Studio.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L01-7

Exercise 3: Exploring a Web Forms Application


 Task 1: Create a web forms application.
1. On the taskbar, click the Start Page - Microsoft Visual Studio icon.

2. On the FILE menu of the Start Page -Microsoft Visual Studio window, point to New, and then click
Project.

3. In the left pane of the New Project dialog box, expand Installed, expand Templates, and then
expand Visual C#.

4. Under Visual C#, click Web, and then in the result pane, click ASP.NET Web Forms Application.
5. In the Name box, replace WebApplication1 with TestWebFormsApplication, and then click OK.

6. On the DEBUG menu of the TestWebFormsApplication – Microsoft Visual Studio window, click
Start Debugging.

7. In the http://localhost:<yourportnumber>/Default.aspx window, click Contact to review the


contents.

8. In the http://localhost:<yourportnumber>/Contact.aspx window, click the Close button.

 Task 2: Explore the application structure.


1. In the Solution Explorer pane of the TestWebFormsApplication – Microsoft Visual Studio window,
click Web.config.

2. In the Web.config code window, locate the <configuration> element, locate the
<connectionStrings> element, and then locate the <add> element.

3. In the <add> element, note that the value of the providerName attribute is System.Data.SqlClient.

4. In the Solution Explorer pane of the TestWebFormsApplication – Microsoft Visual Studio window,
click Default.aspx.

5. In the first line of the Default.aspx code window, note that the value of the MasterPageFile attribute
is ~/Site.Master.
6. In the Solution Explorer pane, click Contact.aspx.

7. In the first line of the Contact.aspx code window, note that the value of the MasterPageFile attribute
is ~/Site.Master.

Note: The Default.aspx and Contact.aspx files are linked to the same layout file.

8. In the Contact.aspx code window, examine the code, and note that there are no links for files with a
.css extension.
9. In the Solution Explorer pane of the TestWebFormsApplication – Microsoft Visual Studio window,
click Site.Master.

10. In the Site.Master code window, note that the path for BundleReference is ~/Content/css.

11. In the Solution Explorer pane, click Bundle.config.

12. In the Bundle.config code window, note that there are links to files with a .css extension.

13. In the Solution Explorer pane, expand Content, and then click Site.css.

14. In the Site.css code window, note that the style sheet for Site.Master is displayed.
MCT USE ONLY. STUDENT USE PROHIBITED
L01-8 Developing ASP.NET MVC 4 Web Applications

 Task 3: Add simple functionality.


1. In the Solution Explorer pane of the TestWebFormsApplication – Microsoft Visual Studio window,
right-click TestWebFormsApplication, point to Add, and then click Web Form.

2. In the Item name box of the Specify Name for Item dialog box, replace WebForm1 with TestPage,
and then click OK.

3. In the Testpage.aspx code window, in the DIV element, type the following code.

<h1>This is a Test Page</h1>

4. On the FILE menu of the TestWebFormsApplication – Microsoft Visual Studio window, click Save
All.

5. In the Solution Explorer pane of the TestWebFormsApplication – Microsoft Visual Studio window,
click Default.aspx.

6. In the Default.aspx code window, in the P element, type the following code.

<a href="TestPage.aspx">
Test Page
</a>

7. On the FILE menu of the TestWebFormsApplication – Microsoft Visual Studio window, click Save
All.

8. On the DEBUG menu of the TestWebFormsApplication – Microsoft Visual Studio window, click
Start Debugging.

9. In the http://localhost:<yourportnumber>/Default.aspx window, click the Test Page link to view


the newly created Web Form page.

Note: The text “This is a Test Page” is displayed on the Web Form page.

10. In the http://localhost:<yourportnumber>/TestPage.aspx window, click the Close button.

 Task 4: Apply the master page.


1. In the Solution Explorer pane of the TestWebFormsApplication – Microsoft Visual Studio window,
click TestPage.aspx.

2. In the first line of the Testpage.aspx code window, place the mouse cursor after the word “Page”, and
then type the following code.

MasterPageFile="~/Site.master"

3. In the TestPage.aspx code window, select the code from <!DOCTYPE html> to </html>, and then
press Delete.
4. In the third line of the TestPage.aspx code window, type the following code.

<asp:Content runat="server"
ID="BodyContent"
ContentPlaceHolderID="MainContent">
<h1>This is a Test Page</h1>
</asp:Content>

5. On the FILE menu of the TestWebFormsApplication – Microsoft Visual Studio window, click Save
All.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L01-9

6. On the DEBUG menu of the TestWebFormsApplication – Microsoft Visual Studio window, click
Start Debugging.

7. In the http://localhost:<yourportnumber>/TestPage.aspx window, click Home.

8. In the http://localhost:<yourportnumber>/window, click the Test Page link, and then note that the
standard site layout and styles have been applied to the page.

9. In the http://localhost:<yourportnumber>/TestPages.aspx window, click the Close button.

10. In the TestWebFormsApplication – Microsoft Visual Studio window, click the Close button.

Results: At the end of this exercise, you will be able to build a simple Web Forms application in Visual
Studio.
MCT USE ONLY. STUDENT USE PROHIBITED
L01-10 Developing ASP.NET MVC 4 Web Applications

Exercise 4: Exploring an MVC Application


 Task 1: Create an MVC 4 application.
1. On the taskbar, click the Start Page - Microsoft Visual Studio icon.

2. On the FILE menu of the Start Page – Microsoft Visual Studio window, point to New, and then
click Project.

3. In the navigation pane of the New Project dialog box, expand Installed, expand Templates, and
then expand Visual C#.

4. In the navigation pane of the New Project dialog box, under Visual C#, click Web, and then, in the
result pane, click ASP.NET MVC 4 Web Application.

5. In the Name box, replace MvcApplication with TestMVCApplication, and then click OK.

6. In the Select a template section of the New ASP.NET MVC 4 Project dialog box, ensure that
Internet Application is selected, and then click OK.

7. On the DEBUG menu of the TestMVCApplication – Microsoft Visual Studio window, click Start
Debugging.

8. In the http://localhost:<yourportnumber>/ window, click Contact to review the contents of the


page.

9. In the http://localhost:<yourportnumber>/Home/Contact window, click the Close button.

 Task 2: Explore the application structure.


1. In the Solution Explorer pane of the TestMVCApplication – Microsoft Visual Studio window, click
Web.config.

2. In the Web.config code window, locate the <configuration> element, locate the
<connectionStrings>element, and then locate the <add> element.

3. In the <add> element, note that the value for the providerName attribute is
System.Data.SqlClient.
4. In the Solution Explorer pane, expand Views, and then click _ViewStart.cshtml.

5. In the _ViewStart.cshtml code window, note that the value for Layout is
~/Views/Shared/_Layout.cshtml.

6. In the Solution Explorer pane, under Views, expand Home, and then click Contact.cshtml.

7. In the Contact.cshtml code window, note that there are no files with .css extension.

8. In the Solution Explorer pane, under Views, collapse Home, expand Shared, and then click
_Layout.cshtml.

9. In the _Layout.cshtml code window, note that there are calls to the Styles helper.

10. In the Solution Explorer pane, expand Content, and then click Site.css.
11. In the Site.css code window, note that the style sheet for _Layout.cshtmlis displayed.

 Task 3: Add simple functionality.


1. In the Solution Explorer pane of the TestMVCApplication – Microsoft Visual Studio window,
expand Home.

2. In the Solution Explorer pane, right-click Home, point to Add, and then click View.

3. In the View name box of the AddView dialog box, replace View1 with TestPage, clear the Use a
layout or master page check box, and then click Add.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L01-11

4. In the DIV element of the TestPage.cshtml code window, type the following code.

<h1>This is a Test Page</h1>

5. On the toolbar, click the Save button.

6. In the Solution Explorer pane, expand Controllers, and then click HomeController.cs.

7. In the HomeController.cs code window, in the HomeController class, type the following code.

public ActionResult TestPage ()


{
return View();
}

8. On the toolbar, click the Save button.

9. In the Solution Explorer pane, under Home, click Index.cshtml.

10. In the Index.cshtml code window, in the first P element, type the following code:

<a href="~/Home/TestPage">
Test Page
</a>

11. On the FILE menu of the TestMVCApplication – Microsoft Visual Studio window, click Save All.

12. On the DEBUG menu of the TestMVCApplication – Microsoft Visual Studio window, click Start
Debugging.

13. In the http://localhost:<portnumber>/ window, click the Test Page link to view the newly created
page.

Note: The text “This is a Test page” is displayed on the newly created page.

14. In the http://localhost:<portnumber>/home/TestPage window, click the Close button.

 Task 4: Apply the site layout.


1. In the Solution Explorer pane of the TestMVCApplication – Microsoft Visual Studio window,
expand Views, and then expand Home, and then click TestPage.cshtml.

2. In the TestPage.cshtml code window, remove the following line of code:

Layout = null;

3. In the TestPage.cshtml code window, delete all tags, except the following.

<h1>This is a Test Page</h1>

4. On the toolbar, click the Save button.

5. On the FILE menu of the TestMVCApplication – Microsoft Visual Studio window, click Save All.

6. On the DEBUG menu of the TestMVCApplication – Microsoft Visual Studio window, click Start
Debugging.

7. In the http://localhost:<yourportnumber>/ window, click the Test Page link.

8. In the http://localhost:<yourportnumber>/home/TestPage window, note that the standard site


layout and styles have been applied.
MCT USE ONLY. STUDENT USE PROHIBITED
L01-12 Developing ASP.NET MVC 4 Web Applications

9. In the http://localhost:<yourportnumber>/home/TestPage window,click the Close button.

10. In the TestMVCApplication - Microsoft Visual Studio window, click the Close button.

Results: At the end of this exercise, you will be able to build a simple MVC application in Visual Studio.
MCT USE ONLY. STUDENT USE PROHIBITED
L02-1

Module02: Designing ASP.NET MVC 4 Web Applications


Lab: Designing ASP.NET MVC 4 Web
Applications
Exercise 1: Planning Models
 Task 1: Examining the initial investigation.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.
4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the File Explorer icon.

6. In the Libraries window, navigate to Allfiles (D):\Labfiles\Mod02, and then double-click


InitialInvestigation.docx.

7. On the View tab of the InitialInvestigation.docx - Microsoft Word window, in the Show group,
ensure that the Navigation Pane check box is selected.
8. In the Navigation pane, click Introduction to view the contents of the Introduction section.

9. In the Navigation pane, click General Description of the Photo Sharing Application to view the
contents.
10. In the Navigation pane, click Use Cases to view the contents.

11. In the Navigation pane, under Use Cases, view all the Use Cases in the document.

12. In the InitialInvestigation.docx - Microsoft Word window, review Figure 1: Use Case Summary.

13. In the InitialInvestigation.docx - Microsoft Word window, click the Close button.

 Task 2: Planning the photo model.


1. On the taskbar, click the File Explorer icon.

2. In the D:\Labfiles\Mod02 window, double-click DetailedPlanningDocument.docx


3. In the DetailedPlanningDocument.docx - Microsoft Word window, locate the MVC Model section.

4. In the MVC Model section, locate the Table 1: MVC Model table.

5. In the first row of the Model Class column, type <Model class name1>.

6. In the first row of the Description column, type <Description of the model class>.

7. In the Table 1: MVC Model table, in the first row of the Properties column, type <Property1>.

8. Add other properties to the model class.

9. In the Table 1: MVC Model table, in the first row of the Data Types column, type <Data type1>.

10. Add other data types corresponding to the photo properties.

11. In the Model Class column of the Table 1: MVC Model table, select all the rows adjacent to the
properties you have added, right-click, and then click Merge Cells.
MCT USE ONLY. STUDENT USE PROHIBITED
L02-2 Developing ASP.NET MVC 4 Web Applications

12. In the Descrption column of the Table 1: MVC Model table, select all the rows adjacent to the
properties you have added, right-click, and then click Merge Cells.

13. On the Quick Access Toolbar, click Save.

14. On the taskbar, move the pointer to the lower-left corner, and then click the Start icon.

15. On the Windows 8 Start screen, click Microsoft Visio 2010.


16. On the File tab of the Microsoft Visio window, click the New tab, and then, in the Template
Categories section, click Software and Database.

17. In the Choose a Template section, click UML Model Diagram, click Metric Units, and then, in the
result pane, click Create. If prompted to add a new template, click Yes.

18. In the Shapes pane of the Drawing1 - Microsoft Visio window, ensure UML Static Structure
(Metric) is selected.

19. In the UML Static Structure stencil, drag the Class shape onto the drawing page.

20. On the drawing page, right-click the Class shape, and then click Properties.

21. In the Name box of the UML Class Properties dialog box, type <Model name1>.

22. In the Categories pane of the UML Class Properties dialog box, click Attributes.

23. In the Attributes table, in the first row of the Attribute column, type <Property1>.

24. In the first row of the Type column, click <Data type1>.

25. Add the attributes for all the properties you added, and then click OK.

26. On the Quick Access Toolbar of the Drawing1 - Microsoft Visio window, click Save.

27. In the Save As dialog box, navigate to Allfiles (D):\Labfiles\Mod02, in the File name box, type
PhotoSharingLDM, and then click Save.

 Task 3: Planning the comment model.


1. On the taskbar, click the Microsoft Word icon.

2. In the DetailedPlanningDocument.docx - Microsoft Word window, locate the MVC Model section.
3. In the DetailedPlanning Document.docx - Microsoft Word window, locate the Table 1: MVC
Model table.

4. In the second row of the Model Class column, type <Model name2>.

5. In the second row of the Description column, type <Description of the model>.

6. In the Properties column of the Table 1: MVC Model table, add the properties corresponding to the
model class you created.
7. In the Data Types column of the Table 1: MVC Model table, add data types corresponding to the
comment properties.

8. In the Model Class column of the Table 1: MVC Model table, select all the rows adjacent to the
properties you added, right-click, and then click Merge Cells.

Note: The rows in the Model Class column should only be merged if you have created
more than one property.

9. In the Description column, select all the rows adjacent to the properties you added, right-click, and
then click Merge Cells.
MCT USE ONLY. STUDENT USE PROHIBITED
L02-3

Note: The rows in the Description column should only be merged if you have created
more than one property.

10. On the Quick Access Toolbar, click Save.

11. On the taskbar, click the PhotoSharingLDM.vsd - Microsoft Visio 2010 icon.
12. In the Shapes pane of the PhotoSharingLDM.vsd - Microsoft Visio window, ensure that the UML
Static Structure (Metric) stencil is selected.

13. In the UML Static Structure stencil, drag the Class shape onto the drawing page.
14. On the drawing page, right-click the Class shape, and then click Properties.

15. In the Name box of the UML Class Properties dialog box, type <Model name2>.

16. In the Categories pane of the UML Class Properties dialog box, click Attributes.

17. In the Attributes table, in the first row of the Attribute column, type the name of the property you
added.

18. In the first row of the Type column, select the corresponding data type.
19. Add the attributes for all the properties you added, and then click OK.

20. On the Home tab of the PhotoSharingLDM.vsd - Microsoft Visio window, in the Tools group, click
Connector.

21. On the drawing page, move the mouse pointer over one of the model classes, click the blue cross,
and then drag the connector to the second model class.

22. On the drawing page, right-click the connector, and then click Shape Display Options.

23. In the End Options section of the UML Shape Display Options dialog box, clear the First End Name
and the Second End Name check boxes, and then click OK.

24. On the drawing page of PhotoSharingLDM.vsd - Microsoft Visio window, right-click the connector,
and then click Properties.

25. In the Categories pane of the UML Association Properties dialog box, ensure that Association is
selected.
26. In the Association Ends table, for each end of the connector, select an appropriate value for the
Multiplicity column, and then click OK.

Note: The values you choose should be determined by the number of photos that can be
associated with a comment and the number of comment that can be associated with a photo.

27. On the Quick Access Toolbar of the PhotoSharingLDM.vsd - Microsoft Visio window, click Save.

Results: After completing this exercise, you will be able to create proposals for a model and configure the
properties and data types of the model classes.
MCT USE ONLY. STUDENT USE PROHIBITED
L02-4 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Planning Controllers


 Task 1: Planning the photo controller.
1. On the taskbar, click the Microsoft Word icon.

2. In the DetailedPlanningDocument.docx - Microsoft Word window, locate the MVC Controllers


section.

3. In the DetailedPlanning Document.docx - Microsoft Word window, locate the Table 2: MVC
Controllers table.

4. In the first row of the Controller column, type <Controller1>.


5. In the first row of the Action column, type <Action1>.

6. Add other required actions for the controller for photos.

7. In the first row of the Description column, type <Description1>.

8. Add other descriptions to the subsequent rows of the Action column.

Note: Describe when each action is run, what it does, and any views it returns.

9. In the Controller column of the Table 2: MVC Controllers table, select all the rows adjacent to the
Action column, right-click, and then click Merge.

10. On the Quick Access Toolbar, click Save.

 Task 2: Planning the comment controller.


1. In the Table 2: MVC Controllers table, in the second row of the Controller column, type
<Controller2>.

2. In the Action column, corresponding to <Controller2>, type <Action1>.

3. Add other required actions in the subsequent rows of the Action column.
4. In the Description column, type the description of the corresponding actions.

5. In the Controller column of the Table 2: MVC Controllers table, select all the adjacent rows to the
actions you planned, right-click, and then click Merge.

6. On the Quick Access Toolbar, click Save.

Results: After completing this exercise, you will be able to create proposals for controllers and configure
their properties and data types.
MCT USE ONLY. STUDENT USE PROHIBITED
L02-5

Exercise 3: Planning Views


 Task 1: Planning the single photo view.
1. In the DetailedPlanningDocument.docx - Microsoft Word window, locate the MVC Views section,
and then locate the Table 3: MVC Views table.

2. In the first row of the Controller column, type <Controller1>.

3. Add other required controllers to the Table 3: MVC Views.

4. In the first row of the View column, type <View1>.

5. In the subsequent rows of the View column, add other required views corresponding to the
controllers.

6. In the Description column, type the description of the corresponding views.

7. In the Controller column of the Table 3: MVC Views table, select all the rows adjacent to the actions
you planned for <Controller1>, right-click, and then click Merge.

8. In the Controller column, merge the rows corresponding to the other controllers.

9. On the Quick Access Toolbar, click Save.

10. On the taskbar, click the PhotoSharingLDM.vsd - Microsoft Visio icon.

11. On the File tab of the PhotoSharingLDM.vsd - Microsoft Visio window, click the New tab.

12. In the Template Categories section of the New tab, click Software and Database.
13. In the Choose a Template section, click Wireframe Diagram, and then, in the result pane, click
Create.

14. In the Shapes pane of the Drawing1 - Microsoft Visio window, ensure Dialogs (Metric) is selected.

15. In the Dialogs (Metric) stencil, drag the Application Form shape to the drawing page.

16. On the drawing page, resize the new shape to fill most of the page.

17. In the Application Form shape, double-click Application Title, and then type the title you want to
be displayed.

18. In the Dialogs (Metric) stencil, drag the Panel shape to the Application Form shape, and drop it
under the title.

19. Resize the panel to fill the width of the Application Form shape.

20. Resize the panel to the same height as the Application Form shape.

21. In the Shapes pane, click Controls (Metric).

22. In the Controls (Metric) stencil, drag a Hyperlink shape to the panel.

23. In the Hyperlinks dialog box, click Cancel.

24. On the drawing page of the Drawing2 - Microsoft Visio window, double-click Hyperlink, and then
type Home.

25. Add links for Gallery, Add a Photo, and Slideshow on the panel.

26. In the Shapes pane, click Dialogs (Metric).

27. In the Dialogs (Metric) stencil, drag a Panel shape to the Application Form shape, and drop the
panel beneath the menu.

28. Resize the panel to display a large photo.


MCT USE ONLY. STUDENT USE PROHIBITED
L02-6 Developing ASP.NET MVC 4 Web Applications

29. In the Shapes pane, click Web and Media Icons (Metric).

30. In the Web and Media Icons (Metric) stencil, drag a Photos shape to the panel you created.

Note: Add other elements to the wireframe diagram that you feel are necessary based on
your reading of the InitialInvestigation document. Steps will vary, depending on the elements
you feel are necessary.

31. On the Quick Access Toolbar of the Drawing1 - Microsoft Visio window, click Save.
32. In the Save As dialog box, navigate to Allfiles (D):\Labfiles\Mod02, in the File name box, type
SinglePhotoWireframe, and then click Save.

 Task 2: Planning the gallery view.


1. On the File tab of the SinglePhotoWireframe.vsd - Microsoft Visio window, click the New tab.
2. In the Template Categories section of the New tab, click Software and Database.

3. In the Choose a Template section, click Wireframe Diagram, and then, in the result pane, click
Create.
4. In the Shapes pane of the Drawing3 - Microsoft Visio window, ensure Dialogs (Metric) is selected.

5. In the Dialogs (Metric) stencil, drag an Application Form shape to the drawing page.

6. On the drawing page, resize the new shape to fill most of the page.

7. In the Application Form shape, double-click Application Title, and type the title you want to be
displayed.

8. In the Dialogs (Metric) stencil, drag the Panel shape to the Application Form shape, and drop it
under the title.

9. Resize the panel to fill the width of the Application Form shape.

10. Resize the panel to be approximately the same height as the title.
11. In the Shapes pane, click Controls (Metric).

12. In the Controls (Metric) stencil drag a Hyperlink shape to the panel.

13. In the Hyperlinks dialog box, click Cancel.


14. On the drawing page of the Drawing1 - Microsoft Visio window, double-click Hyperlink, and then
type Home.

15. On the panel, add Hyperlinks with the following labels: Gallery, Add a Photo, and Slideshow.

16. In the Shapes pane, click Dialogs (Metric).

17. In the Dialogs (Metric) stencil, drag a Panel shape to the Application Form shape, and drop the
panel beneath the menu.
18. Resize the panel to display a thumbnail photo.

19. In the Shapes pane, click Web and Media Icons (Metric).

20. In the Web and Media Icons (Metric) stencil, drag a Photos shape to the panel you created.

Note: You can add multiple panels to display many thumbnails in the Gallery view.
MCT USE ONLY. STUDENT USE PROHIBITED
L02-7

Note: Add other elements to the wireframe diagram that you feel are necessary based on
your reading of the initial investigation document. Note that steps will vary, depending on the
elements you feel are necessary.

21. On the Quick Access Toolbar, click Save.

22. In the Save As dialog box, navigate to Allfiles (D):\Labfiles\Mod02, in the File name box, type
PhotoGalleryWireframe and then click Save.

Results: After completing this exercise, you will create proposals for views and their layouts.
MCT USE ONLY. STUDENT USE PROHIBITED
L02-8 Developing ASP.NET MVC 4 Web Applications

Exercise 4: Architecting an MVC Web Application


 Task 1: Hosting options.
1. On the taskbar, click the DetailedPlanningDocument(docx) - Microsoft Word icon.

2. In the DetailedPlanningDocument(docx) - Microsoft Word window, locate the Hosting


Recommedations section, and then locate the Web Server section.

3. Under Web Server, type the recommended host and web server configuration.

 Task 2: Choosing a data store


1. In the Database section of the DetailedPlanningDocument(docx) - Microsoft Word window, type
the recommended database and server configuration.   

2. On the Quick Access Toolbar, click Save.

Results: After completing this exercise, you will be able to create proposals for hosting arrangements.
MCT USE ONLY. STUDENT USE PROHIBITED
L03-1

Module03: Developing ASP.NET MVC 4 Models


Lab: Developing ASP.NET MVC 4 Models
Exercise 1: Creating an MVC Project and Adding a Model
 Task 1: Create a new MVC project.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Visual Studio 2012.

5. On the File menu of the Start Page - Microsoft Visual Studio window, point to New, and then click
Project.
6. In the left pane of the New Project dialog box, under Installed, under Templates, and then expand
Visual C#.

7. Under Visual C#, click Web, and then in the result pane, click ASP.NET MVC 4 Web Application.
8. In the Name box of the New Project dialog box, type PhotoSharingApplication.

9. In the Location box, ensure that the location specified is Allfiles (D):\Labfiles\Mod03.

10. In the New Project dialog box, ensure that the Create directory for solution check box is selected,
and then click OK.

11. In the Select a template list of the New ASP.NET MVC 4 Project dialog box, click Empty, and then
click OK.

 Task 2: Add a new MVC model.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Models, point to Add, and then click Class.

2. In the Name box of the Add New Item - PhotoSharingApplication dialog box, type Photo.cs, and
then click Add.

3. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Models, point to Add, and then click Class.
4. In the Name box of the Add New Item - PhotoSharingApplication dialog box, type Comment.cs,
and then click Add.

Results: After completing this exercise, you will be able to create an MVC 4 web application and add
model classes to the web application.
MCT USE ONLY. STUDENT USE PROHIBITED
L03-2 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Adding Properties to MVC Models


 Task 1: Add properties to the Photo model class.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, click
Photo.cs.

2. In the Photo class of the Photo.cs code window, type the following code, and then press Enter.

public int PhotoID { get; set; }

3. Place the mouse cursor at the end of the PhotoID property code, press Enter, and then type the
following code.

public string Title { get; set; }

4. Place the mouse cursor at the end of the Title property code, press Enter, and then type the following
code.

public byte[] PhotoFile


{ get; set; }

5. Place the mouse cursor at the end of the PhotoFile property code, press Enter, and then type the
following code.

public string ImageMimeType


{ get; set; }

6. Place the mouse cursor at the end of the ImageMimeType property code, press Enter, and then type
the following code.

public string Description


{ get; set; }

7. Place the mouse cursor at the end of the Description property code, press Enter, and then type the
following code.

public DateTime CreatedDate


{ get; set; }

8. Place the mouse cursor at the end of the CreatedDate property code, press Enter, and then type the
following code.

public string UserName


{ get; set; }

 Task 2: Add properties to the Comment model class.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, click
Comment.cs.

2. In the Comment class of the Comment.cs code window, type the following code, and then press
Enter.

public int CommentID { get; set; }

3. Place the mouse cursor at the end of the CommentID property code, press Enter, and then type the
following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L03-3

public int PhotoID { get; set; }

4. Place the mouse cursor at the end of the PhotoID property code, press Enter, and then type the
following code.

public string UserName


{ get; set; }

5. Place the mouse cursor at the end of the UserName property code, press Enter, and then type the
following code.

public string Subject { get; set; }

6. Place the mouse cursor at the end of the Subject property code, press Enter, and then type the
following code.

public string Body { get; set; }

 Task 3: Implement a relationship between model classes.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
click Photo.cs.

2. In the Photo.cs code window, place the mouse cursor at the end of the UserName property code,
press Enter, and then type the following code.

public virtual ICollection<Comment>


Comments { get; set; }

3. In the Solution Explorer pane, click Comment.cs.

4. In the Comment.cs code window, place the mouse cursor at the end of the Body property code, press
Enter, and then type the following code.

public virtual Photo Photo


{ get; set; }

Results: After completing this exercise, you will be able to add properties to classes to describe them to
the MVC runtime. You will also be able to implement a one-to-many relationship between classes.
MCT USE ONLY. STUDENT USE PROHIBITED
L03-4 Developing ASP.NET MVC 4 Web Applications

Exercise 3: Using Data Annotations in MVC Models


 Task 1: Add display and edit data annotations to the model.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
click Photo.cs.

2. In the Photo.cs code window, place the mouse cursor at the end of the Title property code, press
Enter, and then type the following code.

[DisplayName("Picture")]

3. In the Photo.cs code window, place the mouse cursor at the end of the System.web namespace code,
press Enter, and then type the following code.

using System.ComponentModel;

4. Place the mouse cursor at the end of the ImageMimeType property code, press Enter, and then type
the following code.

[DataType(DataType.MultilineText)]

5. Place the mouse cursor at the end of the System.ComponentModel namespace code, press Enter, and
then type the following code.

using System.ComponentModel.DataAnnotations;

6. Place the mouse cursor at the end of the Description property code, press Enter, and then type the
following code.

[DataType(DataType.DateTime)]
[DisplayName("Created Date")]
[DisplayFormat(DataFormatString =
"{0:dd/MM/yy}")]

7. In the Solution Explorer pane, click Comment.cs.

8. In the Comment.cs code window, place the mouse cursor at the end of the Subject property code,
press Enter, and then type the following code.

[DataType(DataType.MultilineText)]

9. Place the mouse cursor at the end of the System.Web namespace code, press Enter, and then type the
following code.

using System.ComponentModel.DataAnnotations;

 Task 2: Add validation data annotations to the model.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
click Photo.cs.

2. In the Photo.cs code window, place the mouse cursor at the end of the PhotoID property code, press
Enter, and then type the following code.

[Required]

3. In the Solution Explorer pane, click Comment.cs.


MCT USE ONLY. STUDENT USE PROHIBITED
L03-5

4. In the Comment.cs code window, place the mouse cursor at the end of the UserName property code,
press Enter, and then type the following code.

[Required]
[StringLength(250)]

Results: After completing this exercise, you will be able to add property descriptions and data
annotations to the two model classes in the MVC web application.
MCT USE ONLY. STUDENT USE PROHIBITED
L03-6 Developing ASP.NET MVC 4 Web Applications

Exercise 4: Creating a New Windows Azure SQL Database


 Task 1: Add an Entity Framework Context to the model.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click References, and then click Manage NuGet Packages.

2. In the navigation pane of the PhotoSharingApplication - Manage NuGet Packages window, click
Online, click EntityFramework, and then click Install.

3. On the License Acceptance page, click I Accept.

4. In the PhotoSharingApplication - Manage NuGet Packages window, click Close.


5. In the Solution Explorer pane, right-click Models, point to Add, and then click Class.

6. In the Name box of the Add New Item - PhotoSharingApplication dialog box, type
PhotoSharingContext, and then click Add.

7. In the PhotoSharingContext.cs code window, place the mouse cursor at the end of the System.Web
namespace, press Enter, and then type the following code.

using System.Data.Entity;

8. In the PhotoSharingContext.cs code window, locate the following code.

public class PhotoSharingContext

9. Append the following code to the existing line of code.

: DbContext

10. In the PhotoSharingContext class, type the following code.

public DbSet<Photo> Photos


{ get; set; }
public DbSet<Comment> Comments
{ get; set; }

 Task 2: Add an Entity Framework Initializer.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, right-
click Models, point to Add, and then click Class.

2. In the Name box of the Add New Item - PhotoSharingApplication dialog box, type
PhotoSharingInitializer and then click Add.

3. In the PhotoSharingInitializer.cs code window, place the mouse cursor at the end of the System.web
namespace code, press Enter, and then type the following code.

using System.Data.Entity;
using System.IO;

4. In the PhotoSharingInitializer.cs code window, locate the following code.

public class
PhotoSharingInitializer

5. Append the following to the existing line of code.


MCT USE ONLY. STUDENT USE PROHIBITED
L03-7

: DropCreateDatabaseAlways
<PhotoSharingContext>

6. On the taskbar, click the File Explorer icon.

7. In the Libraries dialog box, navigate to Allfiles (D):\Labfiles\Mod03\CodeSnippets, and then


double-click getFileBytes.txt.

8. In the getFileBytes.txt - Notepad window, press Ctrl+A, and then press Ctrl+C.

9. On the taskbar, click the PhotoSharingApplication - Microsoft Visual Studio icon.

10. Place the mouse cursor in the PhotoSharingInitializer class of the PhotoSharingInitializer.cs code
window, press Enter, and then press Ctrl+V.

11. In a new line of the PhotoSharingInitializer class, type the following code, press Spacebar, and then
click Seed(PhotoSharingContext context).

override

12. In the Seed method, place the mouse cursor after the call to base.Seed, press Enter twice, and then
type the following code.

var photos = new List<Photo>


{
new Photo {
Title = "Test Photo",
Description = "Your
Description",
UserName = "NaokiSato",
PhotoFile = getFileBytes
("\\Images\\flower.jpg"),
ImageMimeType =
"image/jpeg",
CreatedDate = DateTime.Today
}
};

13. Place the mouse cursor at the end of the list of Photo objects, press Enter twice, and then type the
following code.

photos.ForEach(s =>
context.Photos.Add(s));
context.SaveChanges();

14. Place the mouse cursor at the end of the Entity Framework context code, press Enter twice, and then
type the following code.

var comments = new List<Comment>


{
new Comment {
PhotoID = 1,
UserName = "NaokiSato",
Subject = "Test Comment",
Body = "This comment " +
"should appear in " +
"photo 1"
}
};

15. Place the mouse cursor at the end of the list of Comment objects, press Enter twice, and then type
the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L03-8 Developing ASP.NET MVC 4 Web Applications

comments.ForEach(s =>
context.Comments.Add(s));
context.SaveChanges();

16. In the Solution Explorer pane, click Global.asax.

17. In the Global.asax code window, place the cursor at the end of the System.Web.Routing namespace,
press Enter, and then type the following code.

using System.Data.Entity;
using PhotoSharingApplication.Models;

18. In the Application_Start method code block, type the following code.

Database.SetInitializer
<PhotoSharingContext>
(new PhotoSharingInitializer());

 Task 3: Create a Windows Azure SQL database and obtain a connection string.
1. On the taskbar, click the Internet Explorer icon.

2. In the Address bar of the Internet Explorer window, type http://www.windowsazure.com/ and then
press Enter.

3. On the Windows Azure page, click Portal.

4. In the Username box, type <your Windows Live user name>.

5. In the Password box, type <password>, and then click Sign in.

6. In the left pane of the Windows Azure page, click SQL DATABASES.

7. In the lower pane, click NEW.

8. In the New dialog box, click SQL DATABASE, and then click CUSTOM CREATE.

9. In the Name box of the NEW SQL DATABASE - CUSTOM CREATE page, type PhotoSharingDB.

10. In the SERVER box, click New SQL Database Server, and then click the Next button.
11. In the LOGIN NAME box, type <your Windows Azure account ID>.

12. In the LOGIN PASSWORD box, type Pa$$w0rd.

13. In the LOGIN PASSWORD CONFIRMATION box, type Pa$$w0rd.


14. In the REGION box, click <a region close to you>, and then click the Complete button.

Note: In the sql databases result pane, note that a new database, PhotoSharingDB, has
been created.

15. In the Name column of the sql databases page, click PhotoSharingDB, and then click the
DASHBOARD tab.

16. In the quick glance section, click the Manage allowed IP addresses link.

17. In the RULE NAME box of the result window, type First Address Range.

18. In the START IP ADDRESS box, type <first address in range>.

19. In the END IP ADDRESS box, type <last address in range>.


MCT USE ONLY. STUDENT USE PROHIBITED
L03-9

20. In the lower pane of the SQL Databases – Windows Azure window, click Save.

Note: To add more IP address ranges, you can repeat the steps 18 – 20.

21. In the Name column of the sql databases page, click PhotoSharingDB.

22. In the quick glance section, click the Show connection strings link.

23. In the ADO.NET box of the Connection Strings window, select the given text, and the press Ctrl+C.

24. On the taskbar, click the PhotoSharingApplication - Microsoft Visual Studio icon.

25. In the Solution Explorer pane, click Web.config.

26. In the Web.config code window, place the mouse cursor after the </appsettings> tag, press Enter,
and then type the following code.

<connectionStrings>
<add name="PhotoSharingDB"
connectionString = ""
providerName =
"System.Data.SqlClient" />
</connectionStrings>

27. In the connectionStrings code block, place the mouse cursor within the quotes after
connectionString = and then press Ctrl-V.

28. In the pasted content, locate the text {your_password_here} and then replace it with Pa$$w0rd.
29. On the Build menu of the PhotoSharingApplication - Microsoft Visual Studio window click Build
Solution, and then note that the application is built successfully.

Results: After completing this exercise, you will be able to create an MVC application that uses Windows
Azure SQL Database as its data store.
MCT USE ONLY. STUDENT USE PROHIBITED
L03-10 Developing ASP.NET MVC 4 Web Applications

Exercise 5: Testing the Model and Database


 Task 1: Add a controller and views.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Controllers, point to Add, and then click Controller.

2. In the Controller name box of the Add Controller dialog box, type PhotoController.

3. In the Template list, click MVC Controller with read/write actions and views, using Entity
Framework.

4. In the Model class list, click Photo (PhotoSharingApplication.Models).


5. In the Data context class list, click PhotoSharingContext (PhotoSharingApplication.Models).

6. In the Views list, ensure that the Razor (CSHTML) view is selected, and then click Add.

 Task 2: Add an image and run the application.


1. In the File Explorer window, navigate to Allfiles (D):\Labfiles\Mod03\Images.
2. In the Images window, right-click flower.JPG and then click Copy.

3. On the taskbar, click the PhotoSharingApplication - Microsoft Visual Studio icon.

4. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click PhotoSharingApplication, point to Add, and then click New Folder.

5. In the NewFolder1 box, type Images.

6. Under PhotoSharingApplication, right-click Images, and then click Paste.

7. In the PhotoSharingApplication - Microsoft Visual Studio window, press F5.

Note: Note that the Internet Explorer window is displayed with an error message. The error
message is expected because the home page view has not been added.

8. In the Address bar of the Internet Explorer window, append the existing URL with photo/index, and
then press Enter.

Note: Note that the added image is displayed on the Index page.

9. On the Index page, click the Details link.

Note: Note that the details of the added image such as the image type, image description,
and image creation date are displayed on the Details page.

Results: After completing this exercise, you will be able to add controllers, views, and images to an MVC
web application and test the application by displaying data from a Windows Azure SQL database.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L04-1

Module04: Developing ASP.NET MVC 4 Controllers


Lab: Developing ASP.NET MVC 4 Controllers
Exercise 1: Adding an MVC Controller and Writing the Actions
 Task 1: Create a photo controller.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the File Explorer icon.

6. In the Libraries window, navigate to Allfiles (D):\Labfiles\Mod04\Starter\PhotoSharingApplication, and


then double-click PhotoSharingApplication.sln.

7. In the Solution Explorer pane, expand PhotoSharingApplication, right-click Controllers, point to


Add, and then click Controller.
8. In the Controller name box of the Add Controller dialog box, type PhotoController, in the
Template box, ensure that Empty MVC controller is selected, and then click Add.

9. In the PhotoController.cs code window, place the mouse cursor at the end of the System.Web.MVC
namespace, press Enter, and then type the following code.

using System.Collections.Generic;
using System.Globalization;
using PhotoSharingApplication.Models;

10. In the PhotoController class code block, press Enter, type the following code, and then press Enter.

private PhotoSharingContext context =


new PhotoSharingContext();

 Task 2: Create the Index action.


1. In the PhotoController.cs code window, replace the Index action code with the following code.

return View("Index",
context.Photos.ToList());

 Task 3: Create the Display action.


1. In the PhotoController.cs code window, place the mouse cursor at the end of the Index action code
block, press Enter, type the following code, and then press Enter.

public ActionResult Display


(int id)
{
}

2. In the Display action code block, type the following code, and then press Enter.
MCT USE ONLY. STUDENT USE PROHIBITED
L04-2 Developing ASP.NET MVC 4 Web Applications

Photo photo =
context.Photos.Find(id);

3. In the Display action code block, type the following code, and then press Enter.

if (photo == null)
{
return HttpNotFound();
}

4. In the Display action code block, type the following code, and then press Enter.

return View("Display", photo);

 Task 4: Write the Create actions for GET and POST HTTP verbs.
1. In the PhotoController.cs code window, place the mouse cursor at the end of the Display action code
block, press Enter twice, and then type the following code.

public ActionResult Create ()


{
}

2. In the Create action code block, type the following code, and then press Enter.

Photo newPhoto = new Photo();


newPhoto.CreatedDate =
DateTime.Today;

3. In the Create action code block, type the following code.

return View("Create", newPhoto);

4. Place the mouse cursor at the end of the Create action code block, press Enter, and then type the
following code.

[HttpPost]
public ActionResult Create
(Photo photo, HttpPostedFileBase image)
{
}

5. In the Create action code block that you newly created, type the following code, and then press
Enter.

photo.CreatedDate = DateTime.Today;

6. In the Create action code block, type the following code, and then press Enter.

if (!ModelState.IsValid)
{
return View("Create", photo);
}
else
{
if (image != null)
{
photo.ImageMimeType =
image.ContentType;
photo.PhotoFile = new
byte[image.ContentLength];
image.InputStream.Read(
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L04-3

photo.PhotoFile, 0,
image.ContentLength);
}
}

7. In the Create action code block, immediately after the code you just added, press Enter, type the
following code, and then press Enter.

context.Photos.Add(photo);
context.SaveChanges();
return RedirectToAction("Index");

 Task 5: Create the Delete actions for GET and POST HTTP verbs.
1. In the PhotoController.cs code window, place the mouse cursor at the end of the Create action code
block that you newly created, press Enter twice, and then type the following code.

public ActionResult Delete (int id)


{
}

2. In the Delete action code block, type the following code, and then press Enter.

Photo photo =
context.Photos.Find(id);

3. In the Delete action code block, type the following code, and then press Enter.

if (photo == null)
{
return HttpNotFound();
}

4. In the Delete action code block, type the following code.

return View("Delete", photo);

5. Place the mouse cursor at the end of the Delete action code block, press Enter twice, and then type
the following code.

[HttpPost]
[ActionName("Delete")]
public ActionResult DeleteConfirmed
(int id)
{
}

6. In the Delete action code block that you newly created, type the following code, and then press
Enter.

Photo photo =
context.Photos.Find(id);

7. In the Delete action code block, type the following code.

context.Photos.Remove(photo);
context.SaveChanges();
return RedirectToAction("Index");
MCT USE ONLY. STUDENT USE PROHIBITED
L04-4 Developing ASP.NET MVC 4 Web Applications

 Task 6: Create the GetImage action.


1. In the PhotoController.cs code window, place the mouse cursor at the end of the Delete action code
block, press Enter twice, and then type the following code.

public FileContentResult GetImage


(int id)
{
}

2. In the GetImage action code block, type the following code, and then press Enter.

Photo photo =
context.Photos.Find(id);

3. In the GetImage action code block, type the following code, and then press Enter.

if (photo != null)
{
return File(photo.PhotoFile,
photo.ImageMimeType);
}
else
{
return null;
}

4. On the toolbar of the PhotoSharingAppliaction - Microsoft Visual Studio window, click the Save
Controller\PhotoController.CS (Ctrl+S) button.

Results: After completing this exercise, you will be able to create an MVC controller that implements
common actions for the Photo model class in the Photo Sharing application.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L04-5

Exercise 2: Writing the Action Filters in a Controller


 Task 1: Add an action filter class.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Controllers, point to Add, and then click Class.

2. In the Add New Item - PhotoSharingApplication dialog box, in the Name box, type
ValueReporter, and then click Add.

3. In the ValueReporter.cs code window of the PhotoSharingApplication - Microsoft Visual Studio


window, place the mouse cursor at the end of System.Web namespace, press Enter, and then type the
following code.

using System.Diagnostics;
using System.Web.Mvc;
using System.Web.Routing;

4. In the ValueReporter.cs code window, locate the following code.

public class ValueReporter

5. Append the following code to the existing line of code.

: ActionFilterAttribute

 Task 2: Add a logValues method to the action filter class.


1. In the ValueReporter class code block of the ValueReporter.cs code window, press Enter, and then
type the following code.

private void logValues


(RouteData routeData)
{
}

2. In the logValues method code block, type the following code.

var controller =
routeData.Values["controller"];
var action =
routeData.Values["action"];
string message = string.Format(
"Controller: {0}; Action: {1}",
controller, action);
Debug.WriteLine(message,
"Action Values");

3. In the logValues method code block, press Enter twice, and then type the following code.

foreach (var item in


routeData.Values)
{
}

4. In the foreach loop, type the following code.

Debug.WriteLine(
">> Key: {0}; Value: {1}", item.Key,
item.Value);
MCT USE ONLY. STUDENT USE PROHIBITED
L04-6 Developing ASP.NET MVC 4 Web Applications

 Task 3: Add a handler for the OnActionExecuting event.


1. In the ValueReporter class code block, place the mouse cursor before the logValues method code
block, press Enter, and then type the following code.

override

2. Press the Spacebar, and then double-click the following code from the list.

OnActionExecuting(ActionExecutingContext filterContext)

3. In the OnActionExecuting event code block, select the following code, and then press Delete.

base.OnActionExecuting(filterContext);

4. In the OnActionExecuting event code block, type the following code.

logValues(filterContext.RouteData);

5. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Controllers\ValueReporter.cs (Ctrl+S) button.

 Task 4: Register the Action Filter with the Photo Controller.


1. In the Solution Explorer pane, under Controllers, click PhotoController.cs
2. In the PhotoController.cs code window, locate the following code.

public class PhotoController


: Controller

3. Place the mouse cursor before the PhotoController class code block, press Enter, and then type the
following code.

[ValueReporter]

4. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Controllers\PhotoController.cs (Ctrl+S) button.

Results: After completing this exercise, you will be able to create an action filter class that logs the details
of actions, controllers, and parameters to the Visual Studio Output window whenever an action is called.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L04-7

Exercise 3: Using the Photo Controller


 Task 1: Create the Index and Display views.
1. In the PhotoController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ActionResult Index()

2. In the Add View dialog box, ensure that the name in the View name box is Index.

3. In the View engine box, ensure that the Razor (CSHTML) view is selected.

4. In the Add View dialog box, select the Create a strongly-typed view check box.

5. In the Model class box, click Photo (PhotoSharingApplication.Models).

6. In the Scaffold template box, click List.

7. In the Add View dialog box, ensure that the Reference script libraries and the Use a layout or
master page check boxes are selected, and then click Add.

8. In the Index.cshtml code window, select the following code.

@Html.ActionLink("Details", "Details",
new { id=item.PhotoID } )

9. Replace the selected code with the following code.

@Html.ActionLink("Display", "Display", new { id=item.PhotoID } )

10. In the Solution Explorer pane, under Controllers, click PhotoController.cs.

11. In the PhotoController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ActionResult Display(int id)

12. In the Add View dialog box, ensure that the name in the View name box is Display.

13. In the Views engine box, ensure that the Razor (CSHTML) view is selected.

14. In the Add View dialog box, ensure that the Create a strongly-typed view check box is selected.

15. In the Model class box, ensure that the Photo (PhotoSharingApplication.Models) model class is
selected.

16. In the Scaffold template box, click Details.

17. In the Add View dialog box, ensure that the Reference script libraries and the Use a layout or
master page check boxes are selected, and then click Add.

 Task 2: Use the GetImage action in the Display view.


1. In the Display.cshtml code window, locate the following code.

<div class="display-field">
@Html.DisplayFor(model =>
model.Title)
</div>

2. Place the mouse cursor at the end of the model.Title property code block, press Enter, and then type
the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L04-8 Developing ASP.NET MVC 4 Web Applications

@if (Model.PhotoFile != null) {


}

3. In the if statement code block, type the following code.

<img width="800" src="" />

4. In the src attribute of the <img> tag, type the following code.

@Url.Action("GetImage", "Photo",
new { id=Model.PhotoID })

5. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Views\Photo\Display.cshtml (Ctrl+S) button.

6. On the BUILD menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Build Solution.

 Task 3: Run the application and display a photo.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

Note: The Internet Explorer window is displays an error message. The error message is
expected because the home page view has not been added.

2. In the Address bar of the Internet Explorer window, append the existing URL with photo/index, and
then press Enter.

3. On the taskbar, click the PhotoSharingApplication - Microsoft Visual Studio icon.

4. In the Output pane of the PhotoSharingApplication (Running) - Microsoft Visual Studio window,
click the Output tab.

5. On the Output tab, locate the following line of code.

>>Key: controller; Value : photo


>>Key: action; Value: index

Note: In the above line of code, note that there are no calls to the Display and GetImage
actions.

6. On the taskbar, click the Internet Explorer icon.

7. On the Index page, click the Display link corresponding to the title Me standing on top of a
mountain.

Note: The selected image is displayed on the Display page.

8. On the taskbar, click the PhotoSharingApplication - Microsoft Visual Studio icon.

9. In the Output pane of the PhotoSharingApplication (Running) - Microsoft Visual Studio window,
click the Output tab.

10. On the Output tab, locate the following code.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L04-9

>>Key: controller; Value: photo


>>Key: action; Value: Display
>>Key: id; Value: 1
>>Key: controller; Value: Photo
>>Key: action; Value: GetImage
>>Key: id; Value: 1

Note: In the above code, note that there are calls to the Display and GetImage actions.
Each call passes a PhotoID.

11. On the DEBUG menu of the PhotoSharingApplication (Running) - Microsoft Visual Studio
window, click Stop Debugging.

12. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Exit.

Results: After completing this exercise, you will be able to create an MVC application with views that you
can use to test controllers, actions, and action filters.
MCT USE ONLY. STUDENT USE PROHIBITED
L04-10 Developing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L05-1

Module05: Developing ASP.NET MVC 4 Views


Lab: Developing ASP.NET MVC 4 Views
Exercise 1: Adding a View for Photo Display
 Task 1: Add a new display view.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the File Explorer icon.

6. In the Libraries window, navigate to Allfiles (D):\


Labfiles\Mod05\Starter\PhotoSharingApplication, and then double-click
PhotoSharingApplication.sln.

7. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand PhotoSharingApplication, expand Controllers, and then click PhotoController.cs.

8. In the PhotoController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ActionResult Display (int id)

9. In the Add View dialog box, ensure that the name in the View name box is Display.

10. In the View engine box, ensure that the Razor (CSHTML) view is selected.

11. In the Add View dialog box, select the Create a strongly-typed view check box.

12. In the Model class box, click Photo (PhotoSharingApplication.Models).

13. In the Scaffold template box, click Empty.

14. In the Add View dialog box, clear the Use a layout or master page check box, and then click Add.

 Task 2: Complete the photo display view


1. In the Display.cshtml code window, locate the following code.

<title>Display</title>

2. Replace the TITLE element with the following code.

<title> Photo: @Model.Title </title>

3. In the DIV element, type the following code.

<h2>"@Model.Title"</h2>

4. Place the mouse cursor at the end of the </h2> tag, press Enter twice, and then type the following
code.
MCT USE ONLY. STUDENT USE PROHIBITED
L05-2 Developing ASP.NET MVC 4 Web Applications

<img width="800" src="" />

5. In the src attribute of the <img> tag, type the following code.

@Url.Action("GetImage", "Photo",
new { id = Model.PhotoID })

6. Place the mouse cursor at the end of the <img> tag, press Enter twice, and then type the following
code.

<p>
@Html.DisplayFor(
model =>
model.Description)
</p>

7. Place the mouse cursor at the end of the </p> tag corresponding to the Description property, press
Enter twice, and then type the following code.

<p>
@Html.DisplayNameFor( model => model.UserName):
@Html.DisplayFor( model => model.UserName)
</p>

8. Place the mouse cursor at the end of the </p> tag corresponding to the UserName property, press
Enter twice, and then type the following code.

<p>
@Html.DisplayNameFor(
model => model.CreatedDate):
@Html.DisplayFor(
model => model.CreatedDate)
</p>

9. Place the mouse cursor at the end of the </p> tag corresponding to the CreatedDate property,
press Enter twice, and then type the following code.

<p>
@Html.ActionLink("Back to List", "Index")
</p>

10. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Views\Photo\Display.cshtml(Ctrl+S) button.

Results: After completing this exercise, you will be able to add a single display view to the Photo Sharing
web application and display the properties of a photo.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L05-3

Exercise 2: Adding a View for New Photos


 Task 1: Add a new create view.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Controllers, click PhotoController.cs.

2. In the PhotoController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ActionResult Create ()

3. In the Add View dialog box, ensure that the name in the View name box is Create.

4. In the View engine box, ensure that the Razor (CSHTML) view is selected.

5. In the Add View dialog box, ensure that the Create a strongly-typed view check box is selected.

6. In the Model class box, ensure that the Photo (PhotoSharingApplication.Models) class is selected.

7. In the Scaffold template box, ensure that the Empty template is selected.
8. Ensure that the Create as a partial view and Use a layout or master page check boxes are clear,
and then click Add.

 Task 2: Complete the photo create view.


1. In the Create.cshtml code window, locate the following code.

<title>Create</title>

2. Replace the TITLE element with the following code.

<title>Create New Photo</title>

3. In the DIV element of the Create.cshtml code window, type the following code, and then press Enter
twice.

<h2>Create New Photo</h2>

4. In the DIV element, place the mouse cursor at the end of the </h2> tag, press Enter, and then type
the following code.

@using (Html.BeginForm("Create",
"Photo", FormMethod.Post,
new { enctype =
"multipart/form-data" }))
{
}

5. In the using code block, press Enter, and then type the following code.

@Html.ValidationSummary(true)

6. Place the mouse cursor at the end of the ValidationSummary helper, press Enter, and then type the
following code.

<p>
@Html.LabelFor(
model => model.Title):
@Html.EditorFor(
model => model.Title) @Html.ValidationMessageFor(
model => model.Title)
MCT USE ONLY. STUDENT USE PROHIBITED
L05-4 Developing ASP.NET MVC 4 Web Applications

</p>

7. Place the mouse cursor at the end of the </p> tag corresponding to the Title property, press Enter
twice, and then type the following code.

<p>
@Html.LabelFor(
model => model.PhotoFile):
<input type="file" name="Image" />
</p>

8. Place the mouse cursor at the end of the </p> tag corresponding to the Image controls, press Enter
twice, and then type the following code.

<p>
@Html.LabelFor(
model => model.Description):
@Html.EditorFor(
model => model.Description)
@Html.ValidationMessageFor(
model => model.Description)
</p>

9. Place the mouse cursor at the end of the </p> tag corresponding to the Description controls, press
Enter twice, and then type the following code.

<p>
@Html.LabelFor(
model => model.UserName):
@Html.DisplayFor(
model => model.UserName)
</p>

10. Place the mouse cursor at the end of the </p> tag corresponding to the UserName controls, press
Enter twice, and then type the following code.

<p>
@Html.LabelFor(
model => model.CreatedDate):
@Html.DisplayFor(
model => model.CreatedDate) </p>

11. Place the mouse cursor at the end of the </p> tag corresponding to the CreatedDate controls, press
Enter twice, and then type the following code.

<p>
<input type="submit"
value="Create" />
@Html.ActionLink("Back to List",
"Index")
</p>

12. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Views\Photo\Create.cshtml(Ctrl+S) button.

Results: After completing this exercise, you will be able to create a web application with a Razor view to
display new photos.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L05-5

Exercise 3: Creating and Using a Partial View


 Task 1: Add a gallery action to the Photo Controller.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Controllers, click PhotoController.cs.

2. In the PhotoController.cs code window, locate the Index action code block.

3. Place the mouse cursor at the end of the Index action code block, press Enter twice, and then type
the following code.

[ChildActionOnly]
public ActionResult _PhotoGallery
(int number = 0)
{
}

4. In the _PhotoGallery action code block, type the following code.

List<Photo> photos;
if (number == 0)
{
photos = context.Photos.ToList();
}

5. Place the mouse cursor at the end of the if code block, press Enter, and then type the following code.

else
{
photos = (
from p in context.Photos
orderby p.CreatedDate descending
select p).Take(number).ToList();
}

6. Place the mouse cursor at the end of the else code block, press Enter, and then type the following
code.

return PartialView("_PhotoGallery",
photos);

7. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Controllers\PhotoController.cs(Ctrl+S) button.

 Task 2: Add a photo gallery partial view.


1. In the Solution Explorer pane, under Controllers, click PhotoController.cs.

2. In the PhotoController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ActionResult _PhotoGallery


(int number = 0)

3. In the View name box of the Add View dialog box, ensure that the name is _PhotoGallery, and then
ensure that the Create a strongly-typed View check box is selected.

4. In the Model class box, ensure that the value is Photo (PhotoSharingApplication.Models), and
then in the Scaffold template box, ensure that the value is Empty.

5. In the Add View dialog box, select the Create as a partial view check box, and then click Add.
MCT USE ONLY. STUDENT USE PROHIBITED
L05-6 Developing ASP.NET MVC 4 Web Applications

Note: In the _PhotoGallery.cshtml code window, the code block @model


PhotosharingApplication.Models.Photo is displayed.

6. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Views, point to Add, and then click New Folder.

7. In the Solution Explorer pane, rename the newly created folder as Shared, and then press Enter.

8. In the Solution Explorer pane, drag the _PhotoGallery.cshtml file from the Photo folder to the
Shared folder.

 Task 3: Complete the photo gallery partial view.


1. In the _PhotoGallery.cshtml code window, locate the following code.

@model
PhotoSharingApplication.Models.Photo

2. Replace the preceding code with the following code.

@model
IEnumerable
<PhotoSharingApplication.Models.Photo>

3. Place the mouse cursor at the end of the @model code block, press Enter twice, and then type the
following code.

@foreach(var item in Model)


{
}

4. Place the mouse cursor in the For Each code block, and then type the following code.

<h3>"@item.Title"</h3>

5. Place the mouse cursor at the end of the H3 element, press Enter twice, and then type the following
code.

if (item.PhotoFile != null)
{
}

6. In the if code block, type the following code.

<img width="200"
src="@Url.Action("GetImage",
"Photo",
new { id = item.PhotoID })" />

7. Place the mouse cursor at the end of the if code block, press Enter twice, and then type the following
code.

<p>
Created By:
@Html.DisplayFor(
model => item.UserName)
</p>

8. Place the mouse cursor at the end of the </p> tag corresponding to the UserName display control,
press Enter twice, and then type the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L05-7

<p>
Created On:
@Html.DisplayFor(
model => item.CreatedDate)
</p>

9. Place the mouse cursor at the end of the </p> tag corresponding to the CreatedDate display
control, press Enter twice, and then type the following code.

@Html.ActionLink("Display", "Display",
new { id = item.PhotoID })

10. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Views\Shared\_PhotoGallery.cshtmal(Ctrl+S) button.

 Task 4: Use the photo gallery partial view.


1. In the Solution Explorer pane, under Controllers, click PhotoController.cs.

2. In the PhotoController.cs code window, locate the following code.

return View("Index",
context.Photos.ToList());

3. Replace the preceding code with the following code.

return View("Index");

4. In the PhotoController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ActionResult Index ()

5. In the View name box of the Add View dialog box, ensure that the name is Index, and then clear
the Create a strongly-typed view check box.

6. Ensure that the Use a layout or master page check box is clear, and then click Add.

7. In the Index.cshtml code window, locate the following code.

<title>Index</title>

8. Replace the TITLE element with the following code.

<title>All Photos</title>

9. In the DIV element, type the following code.

<h2>All Photos</h2>

10. Place the mouse cursor at the end of the H2 element, press Enter twice, and then type the following
code.

<p>
@Html.ActionLink("Add a Photo",
"Create", "Photo")
</p>

11. Place the mouse cursor at the end of the </p> tag, press Enter twice, and then type the following
code.
MCT USE ONLY. STUDENT USE PROHIBITED
L05-8 Developing ASP.NET MVC 4 Web Applications

@Html.Action("_PhotoGallery", "Photo")

12. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Views\Photo\Index.cshtml(Ctrl+S) button.

Results: After completing this exercise, you will be able to create a web application with a partial view to
display multiple photos.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L05-9

Exercise 4: Adding a Home View and Testing the Views


 Task 1: Add a Controller and View for the home page.
1. In the PhotoSharingApplication - Microsoft Visual Studio window, in the Solution Explorer pane,
right-click Controllers, point to Add, and then click Controller.

2. In the Controller name box of the Add Controller dialog box, type HomeController.

3. In the Template box, ensure that Empty MVC Controller is selected, and then click Add.

4. In the HomeController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ActionResult Index ()

5. In the View name box of the Add View dialog box, ensure that the name is Index.

6. In the Add View dialog box, ensure that the Create a strongly-typed view check box is not
selected.

7. In the Add View dialog box, ensure that the Use a layout or master page check box is clear, and
then click Add.

8. In the Index.cshtml code window, locate the following code.

<title>Index</title>

9. Replace the TITLE element with the following code.

<title>Welcome to Adventure Works


Photo Sharing</title>

10. In the DIV element, type the following code.

<p>
Welcome to Adventure Works Photo
sharing! Use this site to share
your adventures.
</p>

11. Place the mouse cursor at the end of the </p> tag, press Enter, and then type the following code.

<h2>Latest Photos</h2>

12. In the Index.cshtml code window, place the mouse cursor at the end of the </h2> tag, press Enter,
and then type the following code.

@Html.Action("_PhotoGallery",
"Photo", new { number = 3 })

13. On the toolbar of the PhotoSharingApplication - Microsoft Visual Studio window, click the Save
Views\Home\Index.cshtml(Ctrl+S) button.

 Task 2: Use the web application.


1. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, note that the total number of photos
displayed is three.
MCT USE ONLY. STUDENT USE PROHIBITED
L05-10 Developing ASP.NET MVC 4 Web Applications

3. On the Welcome to Adventure Works Photo Sharing page, click the Display link corresponding to
the photo that you want to display.

4. On the display page, note that the photo is displayed with the title, description, user name, and
created date, and then click the Back to List link.

5. On the All Photos page, note the total number of photos displayed.

6. On the All Photos page, click the Add a Photo link.

7. On the Create New Photo page, in the Title box, type My First Photo, and then click Browse.

8. In the Choose File to Upload dialog box, navigate to Allfiles (D):\ Labfiles\Mod05\SamplePhotos.
9. In the SamplePhotos folder, select the photo of your choice, and then click Open.

10. On the Create New Photo page, in the Description box, type This is the first test of the Create
photo view, and then click Create.

11. On the All Photos page, note that the photo titled My First Photo is displayed.

12. In the Internet Explorer window, click the Close button.

13. In the PhotoSharingApplication (Running) - Microsoft Visual Studio window, click the Close
button.

Note: If you receive the “Do you want to stop debugging?” message, click Yes.

Results: After completing this exercise, you will be able to create a web application in which users can
upload and view the photos.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L06-1

Module06: Testing and Debugging ASP.NET MVC 4 Web


Applications
Lab: Testing and Debugging ASP.NET MVC
4 Web Applications
Exercise 1: Performing Unit Tests
 Task 1: Create a test project.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the File Explorer icon.


6. In the Libraries window, navigate to Allfiles
(D):\Labfiles\Mod06\Starter\PhotoSharingApplication, and then double-click
PhotoSharingApplication.sln.
7. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Solution ‘Photo Sharing Application’ (1 project), click Add, and then click New Project.

8. In the navigation pane of the Add New Project dialog box, click Visual C#, click Test, and then, in
the result pane, click Unit Test Project.

9. In the Name box, type PhotoSharingTests, and then click OK.

10. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under PhotoSharingTests, right-click References, and then click Add Reference.

11. In the navigation pane of the Reference Manager - PhotoSharingTests window, click Solution, and
then click Projects.

12. In the result pane, select the check box corresponding to PhotoSharingApplication, and then click
OK.

13. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under PhotoSharingTests, right-click References, and then click Add Reference.

14. In the navigation pane of the Reference Manager - PhotoSharingTests window, click Assemblies,
and then click Extensions.

15. In the result pane, select the check box corresponding to System.Web.Mvc with version number
4.0.0.0, and then click OK.

 Task 2: Write the tests.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under PhotoSharingTests, right-click UnitTest1.cs, click Rename, type PhotoControllerTests, and
then press Enter.

2. In the Microsoft Visual Studio dialog box, click Yes.

3. In the PhotoControllerTests.cs code window, locate the following code.


MCT USE ONLY. STUDENT USE PROHIBITED
L06-2 Developing ASP.NET MVC 4 Web Applications

public void TestMethod1()

4. Replace the code with the following code.

public void Test_Index_Return_View()

5. Place the mouse cursor at the end of the Microsoft.VisualStudio.TestTools.UnitTesting namespace,


press Enter, and then type the following code.

using System.Collections.Generic;
using System.Web.Mvc;
using PhotoSharingApplication.Models;
using PhotoSharingApplication.Controllers;

6. Place the mouse cursor in the Test_Index_Return_View test method code block, and then type the
following code.

PhotoController controller =
new PhotoController();
var result = controller.Index()
as ViewResult;
Assert.AreEqual("Index",
result.ViewName);

7. Place the mouse cursor at the end of the Test_Index_Return_View test method code block, press
Enter twice, and then type the following code.

[TestMethod]
public void Test_PhotoGallery_Model_Type()
{
}

8. Place the mouse cursor in the Test_PhotoGallery_Model_Type test method code block, and then
type the following code.

var controller = new PhotoController();


var result = controller._PhotoGallery() as PartialViewResult;
Assert.AreEqual(typeof(List<Photo>), result.Model.GetType());

9. Place the mouse cursor at the end of the Test_PhotoGallery_Model_Type test method code block,
press Enter twice, and then type the following code.

[TestMethod]
public void Test_GetImage_Return_Type()
{
}

10. Place the mouse cursor in the Test_GetImage_Return_Type test method code block, and then type
the following code.

var controller = new PhotoController();


var result = controller.GetImage(1) as ActionResult;
Assert.AreEqual(typeof(FileContentResult), result.GetType());

11. On the TEST menu of the PhotoSharingApplication - Microsoft Visual Studio window, point to Run,
and then click All Tests.

12. In the Test Explorer pane, expand Passed Tests (1), and then note that only the
Test_Index_Return_View test is passed.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L06-3

13. Under Failed Tests (2), note that the Test_GetImage_Return_Type and the
Test_PhotoGallery_Model_Type tests are failed.

14. Under Failed Tests (2), click Test_GetImage_Return_Type, and then note that the test failed while
trying to connect to the database.

15. Under Failed Tests (2), click Test_PhotoGallery_Model_Type, and then note that the test failed while
trying to connect to the database.

16. In the Test Explorer pane, click the Close button.

 Task 3: Implement a repository.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand PhotoSharingApplication.

2. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Models, click Add, and then click New Item.

3. In the navigation pane of the Add New Item - PhotoSharingApplication dialog box, click Code,
and then, in the result pane, click Interface.

4. In the Name box, type IPhotoSharingContext, and then click Add.


5. In the IPhotoSharingContext.cs code window, locate the following code.

interface IPhotoSharingContext

6. Replace the code with the following code.

public interface IPhotoSharingContext

7. Place the mouse cursor in the IPhotoSharingContext interface code block, press Enter, and then
type the following code.

IQueryable<Photo> Photos { get; }

8. Place the mouse cursor at the end of the Photos property code block, press Enter, and then type the
following code.

IQueryable<Comment> Comments { get; }

9. Place the mouse cursor at the end of the Comments property code block, press Enter, and then type
the following code.

int SaveChanges();

10. Place the mouse cursor at the end of the SaveChanges method code block, press Enter, and then
type the following code.

T Add<T>(T entity) where T : class;

11. Place the mouse cursor at the end of the Add method code block, press Enter, and then type the
following code:

Photo FindPhotoById (int ID) ;

12. Place the mouse cursor at the end of the FindPhotoById method code block, press Enter, and then
type the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L06-4 Developing ASP.NET MVC 4 Web Applications

Comment FindCommentById (int ID) ;

13. Place the mouse cursor at the end of the FindCommentById method code block, press Enter, and
then type the following code.

T Delete<T>(T entity) where T : class;

14. In the Solution Explorer pane, click PhotoSharingContext.cs.

15. In the PhotoSharingContext.cs code window, locate the following code.

public class PhotoSharingContext : DbContext

16. Append the following code to the existing line of code.

, IPhotoSharingContext

17. Place the mouse cursor at the end of the Comments DbSet property code block, press Enter twice,
and then type the following code.

IQueryable<Photo> IPhotoSharingContext.Photos
{
get { return Photos; }
}

18. Place the mouse cursor at the end of the Photos property code block, press Enter twice, and then
type the following code.

IQueryable<Comment> IPhotoSharingContext.Comments
{
get { return Comments; }
}

19. Place the mouse cursor at the end of the Comments property code block, press Enter twice, and then
type the following code.

int IPhotoSharingContext.SaveChanges ()
{
return SaveChanges();
}

20. Place the mouse cursor at the end of the SaveChanges method code block, press Enter twice, and
then type the following code.

T IPhotoSharingContext.Add<T>(T entity)
{
return Set<T>().Add(entity);
}

21. Place the mouse cursor at the end of the Add method code block, press Enter twice, and then type
the following code.

Photo IPhotoSharingContext.FindPhotoById(int ID)


{
return Set<Photo>().Find(ID);
}

22. Place the mouse cursor at the end of the FindPhotoById code block, press Enter twice, and then type
the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L06-5

Comment IPhotoSharingContext.FindCommentById(int ID)


{
return Set<Comment>().Find(ID);
}

23. Place the mouse cursor at the end of the FindCommentById method code block, press Enter twice,
and then type the following code.

T IPhotoSharingContext.Delete<T>(T entity)
{
return Set<T>().Remove(entity);
}

24. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

25. On the BUILD menu, click Build Solution.

 Task 4: Refactor the photo controller to use the repository.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand Controllers, and then click PhotoController.cs.

2. In the PhotoController.cs code window, locate the following code.

private PhotoSharingContext context = new PhotoSharingContext();

3. Replace the code with the following code.

private IPhotoSharingContext context;

4. Place the mouse cursor at the end of the context object code block, press Enter twice, and then type
the following code.

public PhotoController ()
{
context = new PhotoSharingContext();
}

5. Place the mouse cursor at the end of the PhotoController constructor code block, press Enter twice,
and then type the following code.

public PhotoController (IPhotoSharingContext Context)


{
context = Context;
}

6. In the PhotoController.cs code window, locate the Display action method, and then locate the
following code.

Photo photo = context.Photos.Find(id);

7. Replace the code with the following code.

Photo photo = context.FindPhotoById (id);

8. Locate the Create action method for the POST verb, and then locate the following code.

context.Photos.Add(photo);

9. Replace the code with the following code.


MCT USE ONLY. STUDENT USE PROHIBITED
L06-6 Developing ASP.NET MVC 4 Web Applications

context.Add<Photo>(photo);

10. Locate the Delete action method, and then locate the following code.

Photo photo = context.Photos.Find(id);

11. Replace the code with the following code.

Photo photo = context.FindPhotoById (id);

12. Locate the DeleteConfirmed action method, and then locate the following code.

Photo photo = context.Photos.Find(id);

13. Replace the code with the following code.

Photo photo = context.FindPhotoById (id);

14. In the DeleteConfirmed action method, locate the following code.

context.Photos.Remove(photo);

15. Replace the code with the following code.

context.Delete<Photo>(photo);

16. Locate the GetImage action method, and then locate the following code.

Photo photo = context.Photos.Find(id);

17. Replace the code with the following code.

Photo photo = context.FindPhotoById (id);

18. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

19. On the Welcome to Adventure Works Photo Sharing page, click the Display link corresponding to
any photo of your choice.

20. On the Sample Photo 5 page, note that the changes are displayed, and then, in the Windows Internet
Explorer window, click the Close button.

 Task 5: Refactor the tests to use a mock repository.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click PhotoSharingTests, click Add, and then click New Folder.

2. In the NewFolder1 box, type Doubles, and then press Enter.

3. In the Solution Explorer pane, right-click Doubles, click Add, and then click Existing Item.

4. In the Add Existing Item - PhotoSharingTests dialog box, navigate to Allfiles


(D):\Labfiles\Mod06\Fake Repository\ FakePhotoSharingContext.cs, and then click Add.
5. In the Solution Explorer pane, click PhotoControllerTests.cs.

6. In the PhotoControllerTests.cs code window, place the mouse cursor at the end of the
PhotoSharingApplication.Contollers namespace, press Enter, and then type the following code.

using System.Linq;
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L06-7

using PhotoSharingTests.Doubles;

7. Locate the Test_Index_Return_View test method, and then locate the following code.

PhotoController = new PhotoController();

8. Replace the code with the following code.

var context = new FakePhotoSharingContext();


var controller = new PhotoController(context);

9. Locate the Test_PhotoGallery_Model_Type test method, place the mouse cursor at the beginning of
the Test_PhotoGallery_Model_Type test method code block, press Enter, and then type the
following code.

var context = new FakePhotoSharingContext();


context.Photos = new [] {
new Photo(),
new Photo(),
new Photo(),
new Photo()
}.AsQueryable();
var controller = new PhotoController(context);

10. In the Test_PhotoGallery_Model_Type test method, locate the following code, select the located
code, and then press Delete.

var controller = new PhotoController();

11. Locate the Test_GetImage_Return_Type test method, place the mouse cursor at the beginning of
the Test_GetImage_Return_Type method, press Enter, and then type the following code.

var context = new FakePhotoSharingContext();

12. Place the mouse cursor at the end of the code you just typed, press Enter twice, and then type the
following code.

context.Photos = new [] {
new Photo{ PhotoID = 1, PhotoFile = new byte[1], ImageMimeType = "image/jpeg" },
new Photo{ PhotoID = 2, PhotoFile = new byte[1], ImageMimeType = "image/jpeg" },
new Photo{ PhotoID = 3, PhotoFile = new byte[1], ImageMimeType = "image/jpeg" },
new Photo{ PhotoID = 4, PhotoFile = new byte[1], ImageMimeType = "image/jpeg" }
}.AsQueryable();

13. In the Test_GetImage_Return_Type method code block, locate the following code.

var controller = new PhotoController();

14. Replace the code with the following code.

var controller = new PhotoController(context);

15. On the TEST menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Run,
and then click All Tests.

16. In the Test Explorer pane, note that all tests have passed.
MCT USE ONLY. STUDENT USE PROHIBITED
L06-8 Developing ASP.NET MVC 4 Web Applications

Note: All the tests passed because the FakePhotoSharingContext test double can return
test values without connecting to a database. A test double or mock object is an object used in
test projects that behaves like the corresponding object in the web application project.

17. In the Test Explorer pane, click the Close button.

 Task 6: Add further tests.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
click PhotoControllerTests.cs.

2. In the PhotoControllerTests.cs code window, place the mouse cursor at the end of the
PhotoControllerTests class, press Enter twice, and then type the following code.

[TestMethod]
public void Test_PhotoGallery_No_Parameter ()
{
}

3. In the Test_PhotoGallery_No_Parameter test method code block, type the following code, and then
press Enter twice.

var context = new FakePhotoSharingContext();


context.Photos = new [] {
new Photo(),
new Photo(),
new Photo(),
new Photo()
}.AsQueryable();
var controller = new PhotoController(context);

4. In the Test_PhotoGallery_No_Parameter test method code block, type the following code, and then
press Enter.

var result = controller._PhotoGallery() as PartialViewResult;

5. In the Test_PhotoGallery_No_Parameter test method code block, type the following code.

var modelPhotos = (IEnumerable<Photo>)result.Model;


Assert.AreEqual(4, modelPhotos.Count());

6. Select all the code in the Test_PhotoGallery_No_Parameter method, including the [TestMethod]
annotation.

7. On the Edit menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Copy.

8. Place the mouse cursor at the end of the Test_PhotoGallery_No_Parameter method.

9. On the Edit menu, click Paste.

10. Locate the following code in the code block you just pasted.

public void Test_PhotoGallery_No_Parameter()

11. Replace the code with the following line of code.

public void Test_PhotoGallery_Int_Parameter()

12. In the Test_PhotoGallery_Int_Parameter method, locate the following code.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L06-9

var result = controller._PhotoGallery() as PartialViewResult;

13. In the Test_PhotoGallery_Int_Parameter method, replace the located code with the following code.

var result = controller._PhotoGallery(3) as PartialViewResult;

14. In the Test_PhotoGallery_Int_Parameter, method, locate the following code.

var modelPhotos = (IEnumerable<Photo>)result.Model;


Assert.AreEqual(4, modelPhotos.Count());

15. In the Test_PhotoGallery_Int_Parameter, method, replace the located code with the following code.

var modelPhotos = (IEnumerable<Photo>)result.Model;


Assert.AreEqual(3, modelPhotos.Count());

16. On the TEST menu of the PhotoSharingApplication -Microsoft Visual studio window, click TEST,
point to Run, and then click All Tests.

17. In the Test Explorer pane, note that all tests passed.

18. In the Test Explorer pane, click the Close button.

Results: After completing this exercise, you will be able to add a set of PhotoController tests defined in
the PhotoSharingTests project to the Photo Sharing application.
MCT USE ONLY. STUDENT USE PROHIBITED
L06-10 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Configuring Exception Handling


 Task 1: Edit Web.config for exception handling.
1. In the Solution Explorer pane, under PhotoSharingApplication, click Web.config.

Note: Ensure that you click the Web.config file in the root folder, and not the Web.config
file in the Views folder.

2. In the Web.config code window, locate the following code.

<system.web>

3. Place the mouse cursor at the end of the code, press Enter, and then type the following code.

<customErrors mode="On" defaultRedirect="Error">


</customErrors>

4. In the <customErrors> element, type the following code.

<error statusCode="500" redirect="~/Error.html"/>

5. In the Solution Explorer pane, right-click PhotoSharingApplication, point to Add, and then click
New Item.

6. In the navigation pane of the Add New Item - PhotoSharingApplication dialog box, under Visual
C#, click Web, and then, in the result pane, click HTML Page.

7. In the Name box, type Error.html, and then click Add.

8. In the TITLE element of the Error.html code window, type the following code.

Error

9. In the BODY element, type the following code.

<h1>500 Error</h1>
<p>There has been an internal server error</p>

 Task 2: Create a custom error view.


1. In the Solution Explorer pane, collapse Controllers, collapse Models, and then expand Views.

2. In the Solution pane, under Views, right-click Shared, point to Add, and then click View.

3. In the View name box of the Add View dialog box, type Error.

4. In the Add View dialog box, ensure that the Create a strongly-typed view and the Create as a
partial view check boxes are clear.
5. In the Add View dialog box, clear the Use a layout or master page check box, and then click Add.

6. In the Error.cshtml code window, locate the following code.

<title>Error</title>

7. Replace the code with the following code.

<title>Custom Error</title>
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L06-11

8. In the Error.cshtml code window, place the mouse cursor at the beginning of the code window, press
Enter twice, and then type the following code.

@model System.Web.Mvc.HandleErrorInfo

9. In the DIV element, type the following code.

<h1>MVC Error</h1>

10. Place the mouse cursor at the end of the H1 element, press Enter, and then type the following code.

Controller: @Model.ControllerName <br />

11. Place the mouse cursor at the end of the <br /> tag corresponding to the ControllerName property,
press Enter, and then type the following code.

Action: @Model.ActionName <br />

12. Place the mouse cursor at the end of the <br /> tag corresponding to the ActionName property,
press Enter, and then type the following code.

Message: @Model.Exception.Message

13. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 3: Configure errors in the PhotoController class.


1. In the Solution Explorer pane, collapse Views, expand Controllers, and then click
PhotoController.cs.

2. In the PhotoController.cs code window, locate the following code.

[ValueReporter]
public class PhotoController : Controller

3. In the PhotoController.cs code window, place the mouse cursor at the beginning of the
[ValueReporter] annotation, press Enter, and then type the following code.

[HandleError(View = "Error")]

4. In the PhotoController class code block, place the mouse cursor at the end of the GetImage action,
press Enter, and then type the following code.

public ActionResult SlideShow()


{
}

5. In the SlideShow action code block, type the following code.

throw new NotImplementedException("The SlideShow action is not yet ready");

 Task 4: Raise errors.


1. On the DEBUG menu of the PhotoSharingApplication –Microsoft Visual Studio window, click Start
Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, click the Display link corresponding to
the “Sample Photo 5” image.
MCT USE ONLY. STUDENT USE PROHIBITED
L06-12 Developing ASP.NET MVC 4 Web Applications

3. In the Address bar of the Windows Internet Explorer window, replace the URL /Photo/Display/5 with
/Photo/Display/malformedID, and then click the Go button.

4. In the Windows Internet Explorer window, note that the custom error view displays the controller,
action, and error message.

5. In the Address bar of the Windows Internet Explorer window, replace the URL
/Photo/Display/malformedID with /Photo/SlideShow, and then click the Go button

6. The Microsoft Visual Studio application breaks on the throw statement.

7. In the NotImplementedException window, click the Close button.


8. In the lower-right part of the PhotoSharingApplication (Debugging) - Microsoft Visual Studio
window, click the IntelliTrace tab.

9. In the IntelliTrace pane, click the ASP.NET: GET "/photo/slideshow" message.

Note: Note the details of the HTTP call.

10. In the IntelliTrace pane, click the Exception: Thrown: “The SlideShow action is not yet ready”
(System.NotImplementedException) message.

Note: In the PhotoController.cs code window, note that the SlideShow action code block
is highlighted.

11. On the DEBUG menu of the PhotoSharingApplication (Intelligence Debugging) - Microsoft


Visual Studio window, click Stop Debugging.

12. In the PhotoSharingApplication – Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to:

Configure a custom error handling strategy for an MVC application.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L07-1

Module07: Structuring ASP.NET MVC 4 Web Applications


Lab: Structuring ASP.NET MVC 4 Web
Applications
Exercise 1: Using the Routing Engine
 Task 1: Test the routing configuration.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.
4. On the taskbar, click the File Explorer icon.

5. In the Libraries window, navigate to Allfiles


(D):\Labfiles\Mod07\Starter\PhotoSharingApplication, and then double-click
PhotoSharingApplication.sln.

6. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand Photo Sharing Tests, and then expand Doubles.
7. In the Solution Explorer pane, right-click Doubles, point to Add, and then click Existing Item.

8. In the Add Existing Item – PhotoSharingTests dialog box, navigate to Allfiles


(D):\Labfiles\Mod07\Fake Http Classes, click FakeHttpClasses.cs, and then click Add.
9. In the Solution Explorer pane, under PhotoSharingTests, right-click References, and then click Add
Reference.

10. In the navigation pane of the Reference Manager – PhotoSharingTests dialog box, under
Assemblies, click Framework.

11. In the list of assemblies, click System.Web, select the check box corresponding to System.Web, and
then click OK.

12. In the Solution Explorer pane, right-click PhotoSharingTests, point to Add, and then click Unit Test.

13. In the Solution Explorer pane, right-click UnitTest1.cs, click Rename, type RoutingTests, and then
press Enter.

14. In the Microsoft Visual Studio dialog box, click Yes.

15. In the RoutingTests.cs code window, place the mouse cursor at the end of the
Microsoft.VisualStudio.TestTools.UnitTesting namespace, press Enter, and then type the following
code.

using System.Web.Routing;
using System.Web.Mvc;
using PhotoSharingTests.Doubles;
using PhotoSharingApplication;

16. In the RoutingTests.cs code window, locate the following code, and then select the code.

public void TestMethod1()

17. Replace the selected code with the following code.


MCT USE ONLY. STUDENT USE PROHIBITED
L07-2 Developing ASP.NET MVC 4 Web Applications

public void Test_Default_Route_ControllerOnly()

18. In the Test_Default_Route_ControllerOnly test method code block, press Enter twice, and then type
the following code.

var context = new FakeHttpContextForRouting(requestUrl: "~/ControllerName");

19. In the RoutingTests.cs code window, place the mouse cursor at the end of the var context code
block, press Enter, and then type the following code.

var routes = new RouteCollection();


RouteConfig.RegisterRoutes(routes);

20. Place the mouse cursor at the end of the RouteConfig.RegisterRoutes() method code block, press
Enter, and then type the following code.

RouteData routeData = routes.GetRouteData(context);

21. Place the mouse cursor at the end of the routes.GetRouteData method code block, press Enter, and
then type the following code.

Assert.IsNotNull(routeData);
Assert.AreEqual("ControllerName", routeData.Values["controller"]);
Assert.AreEqual("Index", routeData.Values["action"]);

22. Place the mouse cursor after the Test_Default_Route_ControllerOnly() test method but inside the
RoutingTests class code block, press Enter, and then type the following code.

[TestMethod]
public void Test_Photo_Route_With_PhotoID()
{
}

23. In the Test_Photo_Route_With_PhotoID test method code block, type the following code.

var context = new FakeHttpContextForRouting(requestUrl: "~/photo/2");

24. Place the mouse cursor at the end of the var context code block that you just entered, press Enter,
and then type the following code.

var routes = new RouteCollection();


RouteConfig.RegisterRoutes(routes);

25. Place the mouse cursor at the end of the RouteConfig.RegisterRoutes() method code block, press
Enter, and then type the following code.

RouteData routeData = routes.GetRouteData(context);

26. Place the mouse cursor at the end of the routes.GetRouteData() method code block, press Enter,
and then type the following code.

Assert.IsNotNull(routeData);
Assert.AreEqual("Photo", routeData.Values["controller"]);
Assert.AreEqual("Display", routeData.Values["action"]);
Assert.AreEqual("2", routeData.Values["id"]);

27. Place the mouse cursor after the Test_Photo_Route_With_PhotoID test method but inside
RoutingTests class code block, and then type the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L07-3

[TestMethod]
public void Test_Photo_Title_Route ()
{
}

28. In the Test_Photo_Title_Route test method code block, type the following code.

var context = new FakeHttpContextForRouting(requestUrl: "~/photo/title/my%20title");

29. Place the mouse cursor at the end of the var context code block that you just entered, press Enter,
and then type the following code.

var routes = new RouteCollection();


RouteConfig.RegisterRoutes(routes);

30. Place the mouse cursor at the end of the RouteConfig.RegisterRoutes() method code block, press
Enter, and then type the following code.

RouteData routeData = routes.GetRouteData(context);

31. Place the mouse cursor at the end of the routes.GetRouteData() method code block, press Enter.,
and then type the following code.

Assert.IsNotNull(routeData);
Assert.AreEqual("Photo", routeData.Values["controller"]);
Assert.AreEqual("DisplayByTitle", routeData.Values["action"]);
Assert.AreEqual("my%20title", routeData.Values["title"]);

32. On the TEST menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Run,
and then click All Tests.

33. In the Test Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand Passed Tests, and then note that the Test_Default_Route_Controller_Only route test has
passed.

34. In the Test Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Failed Tests, note that the Test_Photo_Route_With_PhotoID and the Test_Photo
Title_Route route tests have failed.

35. In the Test Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, click
the Close button.

 Task 2: Add and test the Photo ID route.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand PhotoSharingApplication, expand App_Start, and then double-click RouteConfig.cs.

2. In the RouteConfig.cs code window, locate the following code.

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

3. Place the mouse cursor at the end of the code, press Enter twice, and then type the following code.

routes.MapRoute(
name: "PhotoRoute",
url: "photo/{id}",
defaults: new { controller = "Photo", action = "Display" },
constraints: new { id = "[0-9]+" }
);
MCT USE ONLY. STUDENT USE PROHIBITED
L07-4 Developing ASP.NET MVC 4 Web Applications

4. On the TEST menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Run, and
then click All Tests.

5. In the Test Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, under
Passed Tests, note that the Test_Photo_Route_With_PhotoID route test has passed.

6. In the Test Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, click
the Close button.

 Task 3: Add and test the Photo Title route.


1. In the RouteConfig.cs code window, place the mouse cursor after the PhotoRoute route but before
the Default route code block, and then type the following code.

routes.MapRoute(
name: "PhotoTitleRoute",
url: "photo/title/{title}",
defaults: new { controller = "Photo", action = "DisplayByTitle" }
);

2. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under PhotoSharingApplication, expand Controllers, and then double-click PhotoController.cs.

3. In the PhotoController.cs code window, place the mouse cursor after the Display action code block,
press Enter twice, and then type the following code.

public ActionResult DisplayByTitle(string title)


{
}

4. In the DisplayByTitle action method code block, type the following code.

Photo photo = context.FindPhotoByTitle(title);


if (photo == null)
{
return HttpNotFound();
}
return View("Display", photo);

5. On the TEST menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Run, and
then click All Tests.

6. In the Test Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Passed Tests, note that the Test_Photo_Title_Route route test has passed.

7. In the Test Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, click
the Close button.

 Task 4: Try out the new routes.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventure Works Photo Sharing! Use this site to share your adventures page,
right-click the Display link corresponding to any image, and then click Properties.

3. In the Properties dialog box, note that the Address property does not include the name of the
Display action method, and then click Cancel.

4. On the Welcome to Adventure Works Photo Sharing! Use this site to share your adventures page,
click the Display link corresponding to any image.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L07-5

5. In the Address bar of the Windows Internet Explorer window, note that the URL does not include the
name of the Display action method.

6. In the Address bar of the Windows Internet Explorer window, select /photo/<ID> in the URL, and
then press Delete.

7. In the Address bar, append the existing URL with /photo/title/sample photo 3, and then click the
Go to button.

Note: Note that the Sample Photo 3 is displayed.

8. In the Windows Internet Explorer window, click the Close button.

Results: After completing this exercise, you will be able to create a Photo Sharing application with three
configured routes that enable visitors to easily locate photos using logical URLs.
MCT USE ONLY. STUDENT USE PROHIBITED
L07-6 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Building Navigation Controls


 Task 1: Install the MVC site map provider.
1. On the PROJECT menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Manage NuGet Packages.

2. In the navigation pane of the PhotoSharingApplication - Manage NuGet Packages dialog box,
click Online.

3. In the Search Online box of the PhotoSharingApplication - Manage NuGet Packages dialog box,
type MvcSiteMapProvider, and then click the Search button

4. In the PhotoSharingApplication - Manage NuGet Packages dialog box, click Install corresponding
to the MVCSiteMapProvider package.

5. In the PhotoSharingApplication - Manage NuGet Packages dialog box, click Close.

 Task 2: Configure the MVC site map provider.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Global.asax, double-click Web.config.

Note: Make sure that you do not open the Web.config file in the Views folder.

2. In the <siteMap> element of the Web.config code window, locate the <add> element.

3. In the enableLocalization attribute, of the <add name="MvcSiteMapProvider"> element, change


the value of the attribute from True to False.
4. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Web.config.

5. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
double-click Mvc.sitemap.

6. In the Mvc.sitemap code window, select the following code, and then press Delete.

<mvcSiteMapNode title="About" controller="Home" action="About"/>

7. In the <mvcSiteMapNode> element, type the following code.

<mvcSiteMapNode title="All Photos" controller="Photo" action="Index" key="AllPhotos"


/>

8. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Mvc.sitemap.
9. On the BUILD menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Build Solution.

 Task 3: Render menus and breadcrumb trails.


1. In the Solution Explorer pane, expand Views, expand Home, and then double-click Index.csthml.
2. In the Index.cshtml code window, place the mouse cursor after the <div> tag, press Enter, type the
following code, and then press Enter.

Menu: @Html.MvcSiteMap().Menu(false, false, true)


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L07-7

3. In the Index.cshtml code window, place the mouse cursor at the end of the MvcSiteMap code block,
press Enter, and then type the following code.

Breadcrumb Trail: @Html.MvcSiteMap().SiteMapPath()

4. In the Solution Explorer pane, under Views, expand Photo, and then double-click Index.csthml.

5. In the Index.cshtml code window, place the mouse cursor after the <div> tag, press Enter, type the
following code, and then press Enter.

Menu: @Html.MvcSiteMap().Menu(false, false, true)

6. In the Index.cshtml code window, place the mouse cursor at the end of the MvcSiteMap code block,
press Enter, and then type the following code.

Breadcrumb Trail: @Html.MvcSiteMap().SiteMapPath()

 Task 4: Try out the menus.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventures Works Photo Sharing page, under Menu, click the All Photos link,
and then note that the All Photos page is displayed.

3. In the Breadcrumb Trail section of the All Photos page, click the Home link.

4. In the Windows Internet Explorer window, click the Close button.

5. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to create a Photo Sharing application with a
simple site map, menu, and breadcrumb control.
MCT USE ONLY. STUDENT USE PROHIBITED
L07-8 Developing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L08-1

Module08: Applying Styles to ASP.NET MVC 4 Web


Applications
Lab: Applying Styles to MVC 4 Web
Applications
Exercise 1: Creating and Applying Layouts
 Task 1: Open and browse through the Photo Sharing application.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the taskbar, click the File Explorer icon.

5. In the Libraries window, navigate to Allfiles (D):\Labfiles\Mod08\Starter\PhotoSharingApplication,


and then double-click PhotoSharingApplication.sln.

6. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

Note: The Welcome to Adventure Works Photo Sharing! Use this site to share your
adventures page is displayed. The menu and the breadcrumb trail for the site are displayed on
this page.

7. On the Welcome to Adventure Works Photo Sharing! Use this site to share your adventures page,
under Home, click the All Photos link.

Note: The All Photos page is displayed. The menu and the breadcrumb trail for the site are
not displayed on this page.

8. Under Sample Photo 1 of the All Photos page, click the Display link.

Note: The menu and the breadcrumb trail are not displayed on the Sample Photo 1 page.

9. In the Windows Internet Explorer window, click the Close button.

 Task 2: Create a new layout.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand PhotoSharingApplication, and then expand Views.

2. Under Views, right-click Shared, point to Add, and then click View.

3. In the View name box of the Add View dialog box, type _MainLayout.

4. Ensure that the Create a strongly-typed view check box is cleared.

5. Ensure that the Create as a partial view check box is cleared.


MCT USE ONLY. STUDENT USE PROHIBITED
L08-2 Developing ASP.NET MVC 4 Web Applications

6. Ensure that the Use a layout or master page check box is cleared, and then click Add.

7. In the _MainLayout.cshtml code window, locate the following code.

<title>_MainLayout</title>

8. Replace the code with the following code.

<title>@ViewBag.Title</title>

9. In the DIV element, type the following code.

<h1 class="site-page-title">Adventure Works Photo Sharing


</h1>

10. Place the mouse cursor at the end of the H1 element you just created, press Enter, and then type the
following code.

<div class="clear-floats" />

Note: The clear-floats class you just added to the page will be used with the style sheet for
the web application.

11. Place the mouse cursor at the end of the code you just typed, press Enter, type the following code,
and then press Enter.

<div id="topmenu">
@Html.MvcSiteMap().Menu(false, true, true)
</div>

12. Place the mouse cursor at the end of the code you just typed, press Enter, type the following code,
and then press Enter.

<div id="breadcrumb">
@Html.MvcSiteMap().SiteMapPath()
</div>

13. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

<div>
@RenderBody()
</div>

14. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Views\Shared\_MainLayout.cshtml.

 Task 3: Set the default layout for the application.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Views, point to Add, and then click View.

2. In the View name box of the Add View dialog box, type _ViewStart.

3. Ensure that the Create a strongly-typed view check box is cleared.

4. Ensure that the Create as a partial view check box is cleared.


5. Ensure that the Use a layout or master page check box is cleared, and then click Add.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L08-3

6. In the _ViewStart.cshtml code window, locate the following code.

Layout = null;

7. Replace the code with the following code.

Layout = "~/Views/Shared/_MainLayout.cshtml";

8. In the _ViewStart.cshtml code window, locate the following code, select the code and all the
subsequent lines of code, and then press Delete.

<!DOCTYPE html>

9. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Views/_ViewStart.cshtml.

 Task 4: Update the views to use the layout.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Views, expand Home, and then click Index.cshtml.

2. In the Index.cshtml code window, locate the following code.

Layout = null;

3. Replace the code with the following code.

ViewBag.Title = "Welcome to Adventure Works Photo Sharing";

4. In the Index.cshtml code window, locate the following code, select the code, and then press Delete.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Welcome to Adventure Works Photo Sharing</title>
</head>
<body>
<div>

5. In the Index.cshtml code window, locate the following code, select the code, and then press Delete.

Menu: @Html.MvcSiteMap().Menu(false, false, true)


Current Location: @Html.MvcSiteMap().SiteMapPath()

6. In the Index.cshtml code window, locate the following code, select the code, and then press Delete.

</div>
</body>
</html>

7. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Views\Home\Index.cshtml.

8. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Views, expand Photo, and then click Display.cshtml.

9. In the Display.cshtml code window, locate the following code.

Layout = null;
MCT USE ONLY. STUDENT USE PROHIBITED
L08-4 Developing ASP.NET MVC 4 Web Applications

10. Replace the code with the following code.

ViewBag.Title = Model.Title;

11. Locate the following code, select the code, and then press Delete.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@Model.Title</title>
</head>
<body>
<div>

12. Locate the following code, select the code, and then press Delete.

</div>
</body>
</html>

13. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Views\Photo\Display.cshtml.

14. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under DisplayTemplates, click Error.cshtml.

15. In the Error.cshtml code window, locate the following code.

Layout = null;

16. Replace the code with the following code.

ViewBag.Title = "Custom Error";

17. In the Error.cshtml code window, locate the following code, select the code, and then press Delete.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Custom Error</title>
</head>
<body>
<div>

18. In the Error.cshtml code window, locate the following code, select the code, and then press Delete.

</div>
</body>
</html>

19. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Views\Shared\Error.cshtml.

 Task 5: Browse through the web application.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L08-5

Note: On the Adventure Works Photo Sharing page, note that the menu and the
breadcrumb trail are displayed.

2. In the Menu of the Adventure Works Photo Sharing page, click the All Photos link.

Note: On the All Photos page, note that the menu and the breadcrumb trail are now
displayed.

3. Under Sample Photo 1 of the All Photos page, click the Display link.

Note: On the Adventure Works Photo Sharing page, note that the Sample Photo 1 is
displayed. The site title, menu, and breadcrumb trail are now displayed on this webpage.

4. In the Windows Internet Explorer window, click the Close button.

Results: After completing this exercise, you will be able to create an ASP.NET MVC 4 web application that
uses a single layout to display every page of the application.
MCT USE ONLY. STUDENT USE PROHIBITED
L08-6 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Applying Styles to an MVC Web Application


 Task 1: Examine the HTML mockup web application.
1. On the taskbar, click the File Explorer icon.

2. In the PhotoSharingApplication dialog box, navigate to Allfiles (D):\Labfiles\Mod08 \Expression


Web Mock Up, and then double-click default.html.

Note: On the Welcome to Adventure Works Photo Sharing page, note the colors, fonts,
and layout of the page.

3. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.

Note: On the All Photos page, note that the layout of the page is similar to the home
page, but more photos are displayed on the page.

4. On the All Photos page, click the Details link corresponding to any image.

Note: The photo, metadata, description, and comments are displayed on the Details page.

5. In the Windows Internet Explorer window, click the Close button.

 Task 2: Import the styles and graphics.


1. Switch to the PhotoSharingApplication - Microsoft Visual Studio window.

2. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click PhotoSharingApplication, point to Add, and then click New Folder.

3. In the Solution Explorer pane, rename NewFolder1 as Content, and then press Enter.

4. In the Solution Explorer pane, right-click Content, point to Add, and then click Existing Item.

5. In the Add Existing Item - PhotoSharingApplication dialog box, navigate to Allfiles


(D):\Labfiles\Mod08\Expression Web Mock Up\Content, click PhotoSharingStyles.css, and then click
Add.

6. In the Solution Explorer pane, right-click Content, point to Add, and then click Existing Item.

7. In the Add Existing Item - PhotoSharingApplication dialog box, click BackgroundGradient.jpg,


and then click Add.
8. In the Solution Explorer pane, under Shared, click _MainLayout.cshtml.

9. In the _MainLayout.cshtml code window, locate the following code.

<title>@ViewBag.Title</title>

10. Place the mouse cursor at the end of the TITLE element, press Enter, and then type the following
code.

<link type="text/css" rel="stylesheet" href="~/content/PhotoSharingStyles.css" />

11. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
Views\Shared\_MainLayout.cshtml.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L08-7

 Task 3: Update the element classes to use the styles.


1. In the Solution Explorer pane, under DisplayTemplates, click _PhotoGallery.cshtml.

2. In the _PhotoGallery.cshtml window, locate the following code.

@foreach (var item in Model)


{
<div>

3. In the code, replace the <div> tag with the following code.

<div class="photo-index-card">

4. In the DIV element of the _PhotoGallery.cshtml window, locate the following code.

<img width="200"

5. Replace the code with the following code.

<img class="photo-index-card-img"

6. In the _PhotoGallery.cshtml window, locate the <div> tag after the <img> tag.
7. Replace <div> tag with the following code.

<div class="photo-metadata">

8. In the _PhotoGallery.cshtml window, locate the following code.

<span>
Created By:
</span>

9. Replace the code with the following code.

<span class="display-label">
Created By:
</span>

10. In the _PhotoGallery.cshtml window, locate the following code.

<span>
@Html.DisplayFor(model => item.UserName)
</span>

11. Replace the code with the following code.

<span class="display-field">
@Html.DisplayFor(model => item.UserName)
</span>

12. In the _PhotoGallery.cshtml window, locate the following code.

<span>
Created On:
</span>

13. Replace the code with the following code.

<span class="display-label">
Created On:
MCT USE ONLY. STUDENT USE PROHIBITED
L08-8 Developing ASP.NET MVC 4 Web Applications

</span>

14. In the _PhotoGallery.cshtml window, locate the following code.

<span>
@Html.DisplayFor(model => item.CreatedDate)
</span>

15. Replace the code with the following code.

<span class="display-field">
@Html.DisplayFor(model => item.CreatedDate)
</span>

 Task 4: Browse the styled web application.


1. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Start Debugging.

Note: On the Welcome to Adventure Works page, examine the styles applied to the home
page. Note the colors, fonts, and background gradient image. Also, note the layout of the photo
thumbnail cards and the top menu and breadcrumb trail shortcuts.

2. On the Welcome to Adventure Works page, click the All Photos link.

Note: Note the new style applied to the photo gallery on the All Photos page.

3. On the All Photos page, click the Display link corresponding to a photo.

Note: Note the new style applied to the display view.

4. In the Windows Internet Explorer window, click the Close button.

Results: After completing this exercise, you will be able to create a Photo Sharing application with a
consistent look and feel.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L08-9

Exercise 3: Adapting Webpages for Mobile Browsers


 Task 1: Test the application as a mobile device.
1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Start
Debugging.

2. On the Tools menu of the Windows Internet Explorer window, click F12 developer tools.

Note: In the Windows Internet Explorer window, note that a developer window is
displayed.

3. On the Tools menu of the developer window, point to Resize, and then click Custom.

4. In the Width box of the Resize Browser dialog box, type 480.

5. In the Height box, type 700, and then click Add.


6. In the dimensions area of the Resize Browser dialog box, click 480x700, and then click Resize.

Note: The size of the Windows Internet Explorer window has reduced according to the
specified dimensions.

7. In the Resize Browser dialog box, click Close.

8. On the Tools menu of the developer window, point to Change user agent string, and then click IE9
for Windows Phone 7.

9. On the File menu of the developer window, click Exit.

10. In the Address bar of the Windows Internet Explorer window, click the Refresh (F5) button.

Note: The main heading, menu, and other elements of the home page do not display
correctly in the mobile view.

11. In the Windows Internet Explorer window, click the Close button.

 Task 2: Add a new mobile layout.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under DisplayTemplates, right-click _MainLayout.cshtml, and then click Copy.

2. In the Solution Explorer pane, right-click Shared, and then click Paste.

3. Under DisplayTemplates, right-click Copy of _MainLayout.cshtml, click Rename, type


_MainLayout.Mobile, and then press Enter.

4. In the _MainLayout.Mobile.cshtml code window, locate the following code.

<h1 class="site-page-title">Adventure Works Photo Sharing</h1>

5. Replace the code with the following code.

<h1 class="site-page-title">
Adventure Works <br />
Photo Sharing
</h1>
MCT USE ONLY. STUDENT USE PROHIBITED
L08-10 Developing ASP.NET MVC 4 Web Applications

6. Place the mouse cursor at the end of the H1 element, press Enter, and then type the following code.

<h2>Mobile Site</h2>

7. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
Views\Shared\_MainLayout.Mobile.cshtml.

 Task 3: Add a media query to the style sheet.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Content, click PhotoSharingStyles.css.

2. In the PhotoSharingStyles.css code window, place the mouse cursor at the end of the style sheet,
press Enter, and then type the following code.

@media only screen and (max-width: 500px) {


}

3. In the PhotoSharingStyles.css code window, locate the following code.

.topmenulink {
float: left;
background-color: #cccccc;
width: 200px;
height: 35px;
text-align: center;
line-height: 2em;
border-radius: 5px;
margin: 5px;
}

4. In the PhotoSharingStyles.css code window, place the mouse cursor in the media query you just
added, and then type the following code.

.topmenulink {
float: left;
background-color: #cccccc;
width: 200px;
height: 35px;
text-align: center;
line-height: 2em;
border-radius: 5px;
margin: 5px;
}

5. In the media query you just pasted, locate the following code.

width: 200px;

6. In the located code, replace 200px with 100px.

 Task 4: Retest the application as a mobile device.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Tools menu of the Windows Internet Explorer window, click F12 developer tools.

3. On the Cache menu of the developer window, click Clear browser cache.

4. In the Clear Browse Cache dialog box, click Yes.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L08-11

5. On the Tools menu of the developer tools window, point to Change user agent string, and then
click IE9 for Windows Phone 7.

6. On the File menu of the developer tools window, click Exit.

7. In the Address bar of the Windows Internet Explorer window, click the Refresh (F5) button, and then
note that the home page of the web application is displayed without problems in the mobile view.

Note: This indicates that the mobile view and media query have been successfully applied.

8. In the Windows Internet Explorer window, click the Close button.

9. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to create a Photo Sharing application that displays
well on mobile devices and devices with small screens.
MCT USE ONLY. STUDENT USE PROHIBITED
L08-12 Developing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L09-1

Module09: Building Responsive Pages in ASP.NET MVC 4


Web Applications
Lab: Building Responsive Pages in ASP.NET
MVC 4 Web Applications
Exercise 1: Using Partial Page Updates
 Task 1: Import the Comment controller and Delete view.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the taskbar, click the File Explorer icon.

5. In the Libraries window, navigate to Allfiles (D):\Labfiles\Mod09\Starter


\PhotoSharingApplication, and then double-click PhotoSharingApplication.sln

6. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under PhotoSharingApplication, right-click Views, point to Add, and then click New Folder.
7. In the Solution Explorer pane, rename New Folder1 as Comment, and then press Enter.

8. In the Solution Explorer pane, under Views, right-click Comment, point to Add, and then click
Existing Item.
9. In the Add Existing Item - PhotoSharingApplication dialog box, navigate to Allfiles
(D):\Labfiles\Mod09\Comment Components, click Delete.cshtml, and then click Add.

10. In the Solution Explorer pane, right-click Controllers, point to Add, and then click Existing Item.

11. In the Add Existing Item - PhotoSharingApplication dialog box, navigate to Allfiles
(D):\Labfiles\Mod09\Comment Components, click CommentController.cs, and then click Add.

 Task 2: Add the _CommentsForPhoto action and view.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, click
CommentController.cs.

2. In the CommentController.cs code window, locate the following code.

public CommentController (IPhotoSharingContext Context)


{
context = Context;
}

3. Place the mouse cursor after the located code, press Enter twice, and then type the following code.

[ChildActionOnly]
public PartialViewResult _CommentsForPhoto(int PhotoId)
{
}

4. In the _CommentsForPhoto action code block, type the following code.

var comments = from c in context.Comments


MCT USE ONLY. STUDENT USE PROHIBITED
L09-2 Developing ASP.NET MVC 4 Web Applications

where c.PhotoID == PhotoId


select c;

5. Place the mouse cursor at the end of the code block you just created, press Enter, and then type the
following code.

ViewBag.PhotoId = PhotoId;

6. Place the mouse cursor at the end of the code block you just created, press Enter, and then type the
following code.

return PartialView("_CommentsForPhoto", comments.ToList());

7. Right-click the following line of code, and then click Add View.

public PartialViewResult _CommentsForPhoto(int PhotoId)

8. In the Add View dialog box, ensure that the name in the View name box is _CommentsForPhoto.

9. Select the Create a strongly-typed view check box.


10. In the Model Class box, click Comment. (PhotoSharingApplication.Models).

11. Select the Create as a partial view check box, and then click Add.

12. In the Solution Explorer pane, under Views, drag the _CommentsForPhoto.cshtml file from the
Comment folder to the Shared folder.

13. In the _CommentsForPhoto.cshtml code window, locate the following code.

@model PhotoSharingApplication.Models.Comment

14. Replace the code with the following code.

@model IEnumerable<PhotoSharingApplication.Models.Comment>

15. Place the mouse cursor at the end of the code you just entered, press Enter twice, and then type the
following code.

<h3>Comments</h3>

16. Place the mouse cursor at the end of the H3 element you just created, press Enter, and then type the
following code.

<div id="comments-tool">
<div id="all-comments">
</div>
</div>

17. In the <div id="all-comments"> element, type the following code.

@foreach (var item in Model)


{
<div class="photo-comment">
</div>
}

18. In the <div class="photo-comment"> element, type the following code.

<div class="photo-comment-from">
From:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L09-3

@Html.DisplayFor(modelItem => item.UserName)


</div>

19. Place the mouse cursor at the end of the DIV element you just created, press Enter, and then type the
following code.

<div class="photo-comment-subject">
Subject:
@Html.DisplayFor(modelItem => item.Subject)
</div>

20. Place the mouse cursor at the end of the DIV element you just created, press Enter, and then type the
following code.

<div class="photo-comment-body">
@Html.DisplayFor(modelItem => item.Body)
</div>

21. Place the mouse cursor at the end of the DIV element you just created, press Enter, and then type the
following code.

@Html.ActionLink("Delete This Comment", "Delete", new { id = item.CommentID})

22. In the Solution Explorer pane, under Views, expand Photo, and then click Display.cshtml.

23. In the Display.cshtml code window, locate the following code.

<p>
@Html.ActionLink("Back To List", "Index")
@Html.ActionLink("Delete This Photo", "Delete", new { id = Model.PhotoID })
</p>

24. Immediately before the located code, press Enter twice, and then type the following code.

@Html.Action("_CommentsForPhoto", "Comment", new { PhotoId = Model.PhotoID })

25. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

26. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.

27. Under Sample Photo 1, click the Display link.

28. On the Welcome to Adventure Works Photo Sharing page, scroll to the end of the page, and then
note that the two comments are displayed.

29. In the Windows Internet Explorer window, click the Close button.

 Task 3: Add the _Create Action and the _CreateAComment views.


1. In the Solution Explorer pane, under Controllers, click CommentController.cs.

2. In the CommentController.cs code window, place the mouse cursor within the CommentController
class but outside any method code block, and type the following code.

public PartialViewResult _Create(int PhotoId)


{
}

3. In the _Create action code block, type the following code.

Comment newComment = new Comment();


MCT USE ONLY. STUDENT USE PROHIBITED
L09-4 Developing ASP.NET MVC 4 Web Applications

newComment.PhotoID = PhotoId;

4. In the _Create action code block, place the mouse cursor at the end of the Comment object you just
created, press Enter, and then type the following code.

ViewBag.PhotoID = PhotoId;

5. Place the mouse cursor at the end of the ViewBag attribute you just created, press Enter, and then
type the following code:

return PartialView("_CreateAComment");

6. In the Solution Explorer pane, under Views, right-click Shared, point to Add, and then click View.

7. In the View name box of the Add View dialog box, type _CreateAComment.

8. Ensure that the Create a strongly-typed view check box is selected.

9. In the Model Class box, ensure that the value is Comment (PhotoSharingApplication.Models).
10. Select the Create as a partial view check box, and then click Add.

11. In the _CreateAComment.cshtml code window, place the mouse cursor at the end of the @model
statement, press Enter twice, and then type the following code.

@Html.ValidationSummary(true)

12. Place the mouse cursor at the end of the validation messages you just created, press Enter, and then
type the following code.

<div class="add-comment-tool">
</div>

13. In the <div class="add-comment-tool"> element you just created, type the following code.

<div>
</div>

14. In the DIV element you just created, type the following code.

<span class="editor-label">
Subject:
</span>

15. Place the mouse cursor at the end of the SPAN element you just created, press Enter, and then type
the following code.

<span class="editor-field">
@Html.EditorFor(model => model.Subject)
</span>

16. In the <div class="add-comment-tool"> element, place the mouse cursor after the end tag of the
DIV element, press Enter, and then type the following code.

<div>
</div>

17. In the DIV element you just created, type the following code.

<span class="editor-label">
Body:
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L09-5

</span>

18. Place the mouse cursor at the end of the SPAN element you just created, press Enter, and then type
the following code.

<span class="editor-field">
@Html.EditorFor(model => model.Body)
</span>

19. In the <div class="add-comment-tool"> element, place the mouse cursor at the end of the second
DIV element, press Enter, and then type the following code.

<input type="submit" value="Create" />

20. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 4: Add the _CommentsForPhoto POST action.


1. In the Solution Explorer pane, under Controllers, click CommentController.cs.

2. In the CommentController.cs code window, place the cursor within the CommentController class but
outside any method code block, and then type the following code.

[HttpPost]
public PartialViewResult _CommentsForPhoto(Comment comment, int PhotoId)
{
}

3. In the _CommentsforPhoto action code block you just created, type the following code.

context.Add<Comment>(comment);
context.SaveChanges();

4. In the _CommentsforPhoto action code block, place the mouse cursor at the end of the comment
object you just created, press Enter, and then type the following code.

var comments = from c in context.Comments


where c.PhotoID == PhotoId
select c;

5. In the _CommentsforPhoto action code block, place the mouse cursor at the end of the query you
just created, press Enter, and then type the following code.

ViewBag.PhotoId = PhotoId;

6. In the _CommentsforPhoto action code block, place the mouse cursor at the end of the ViewBag
attribute you just created, press Enter, and then type the following code.

return PartialView("_CommentsForPhoto", comments.ToList());

7. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 5: Complete the _CommentsForPhoto view.


1. In the Solution Explorer pane, under Shared, click _CommentsForPhoto.cshtml.

2. In the _CommentsForPhoto.cshtml code window, locate the following code.


MCT USE ONLY. STUDENT USE PROHIBITED
L09-6 Developing ASP.NET MVC 4 Web Applications

<h3>Comments</h3>

3. Place the mouse cursor at the end of the located code, press Enter twice, and then type the following
code.

@using (Ajax.BeginForm("_CommentsForPhoto",
new { PhotoId = ViewBag.PhotoId },
new AjaxOptions { UpdateTargetId = "comments-tool" }))
{

4. In the _CommentsForPhoto.cshtml code window, locate the last </div> tag, press Enter, and then
add the following code.

5. In the _CommentsForPhoto.cshtml code window, locate the last </div> tag.

6. Before the located tag, type the following code.

<div id="add-comment" class="add-comment-box">


</div>

7. In the DIV element you just created, type the following code.

@Html.Action("_Create", "Comment", new { PhotoId = ViewBag.PhotoId })

8. In the Solution Explorer pane, under Shared, click _MainLayout.cshtml.

9. In the _MainLayout.cshtml code window, locate the following code.

</head>

10. Just before the located code, insert the following code.

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js"
type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.js"
type="text/javascript"></script>

11. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Start Debugging.

12. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.
13. Under Sample Photo 1, click the Display link, and then on the Welcome to Adventure Works Photo
Sharing page, note that the two comments are displayed.

14. In the Subject box, type Test Comment.

15. In the Body box, type This comment is to test AJAX-based partial page updates., and then click
Create.

Note: Note that the new comment is displayed without a page reload.

16. In the Windows Internet Explorer window, click the Close button.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L09-7

Results: At the end of this exercise, you will have ensured that new comments can be added and
displayed on the pages of the application without a complete page reload. You will create a Photo
Sharing application with a comments tool, implemented by using partial page updates.
MCT USE ONLY. STUDENT USE PROHIBITED
L09-8 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Configuring the ASP.NET Caches


 Task 1: Test the All Photos page with no caching.
1. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Start
Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, click the Tools button, and then click F12
developer tools.

3. On the Cache menu of the developer window, click Always refresh from server.

4. On the Network tab of the developer window, click Start capturing.


5. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.

6. When the page is fully loaded, in the developer window, click Stop capturing.

7. In the URL column of the developer window, click http://localhost:<portnumber>/Photo, and then
click Go to detailed view.

8. On the Timings tab, click the Request entry, and then, in the Duration column, note that the time
taken.
9. On the Network tab of the developer window, click Clear, and then click Start capturing.

10. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.

11. When the page is fully loaded, in the developer window, click Stop capturing.

12. In the URL column of the developer window, click http://localhost:<portnumber>/Photo, and then
click Go to detailed view.

13. On the Timings tab, click the Request entry, and then, in the Duration column, note that the time
taken is reduced.

14. In the Windows Internet Explorer window, click the Close button.

 Task 2: Configure caching for the Index action.


1. In the Solution Explorer pane, under Controllers, click PhotoController.cs.
2. In the PhotoController.cs code window, locate the following code.

using System.Web.Mvc;

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using System.Web.UI;

4. In the PhotoController.cs code window, locate the following code.

public ActionResult Index()

5. Place the mouse cursor before the located code, type the following code, and then press Enter.

[OutputCache(Duration=600, Location=OutputCacheLocation.Server, VaryByParam="none")]

6. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L09-9

 Task 3: Retest the All Photos page with Index caching.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Start
Debugging.

2. On the Cache menu of the developer window, click Always refresh from server.

3. On the Network tab, click Start capturing.

4. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.
5. When the page is fully loaded, in the developer window, click Stop capturing.

6. In the URL column of the developer window, click http://localhost:<portnumber>/Photo, and then
click Go to detailed view.

7. On the Timings tab, click the Request entry, and then, in the Duration column, note that the time
taken.

8. On the Network tab, click Clear, and then click Start capturing.

9. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.

10. When the page is fully loaded, in the developer window, click Stop capturing.

11. In the URL column of the developer window, click http://localhost:<portnumber>/Photo, and then
click Go to detailed view.

12. On the Timings tab, click the Request entry, and then, in the Duration column, note that the time
taken is reduced.

13. On the Network tab of the developer window, click Back to summary view.

14. In the URL column, click /Photo/GetImage/1, and then click Go to detailed view.

15. Click the Request entry, and then, in the Duration column, note that the duration.

16. In the Windows Internet Explorer window, click the Close button.

 Task 4: Configure caching for the GetImage action.


1. In the PhotoController.cs code window, locate the following code.

public FileContentResult GetImage (int id)

2. Place the mouse cursor immediately before the located code, type the following code, and then press
Enter.

[OutputCache(Duration=600, Location=OutputCacheLocation.Server, VaryByParam="id")]

3. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 5: Retest the All Photo page with GetImage caching.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Cache menu of the developer window, click Always refresh from server.

3. On the Network tab, click Start capturing.

4. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.

5. When the page is fully loaded, in the developer window, click Stop capturing.
MCT USE ONLY. STUDENT USE PROHIBITED
L09-10 Developing ASP.NET MVC 4 Web Applications

6. In the URL column of the developer window, click /Photo/GetImage/1, and then click Go to
detailed view.

7. On the Timings tab, click the Request entry, and then, in the Duration column note that the
duration.

8. Click Clear, and then click Start capturing.

9. On the Welcome to Adventure Works Photo Sharing page, click the All Photos link.

10. When the page is fully loaded, in the developer window, click Stop Capturing.

11. In the URL column of the developer window, click /Photo/GetImage/1, and then click Go to
detailed view.

12. On the Timings tab, click the Request entry, and then, in the Duration column, note that the time
taken has reduced.

13. On the File menu of the developer window, click Exit.

14. In the Windows Internet Explorer window, click the Close button.

15. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

Results: At the end of this exercise, you will create a Photo Sharing application with the Output Cache
configured for caching photos.
MCT USE ONLY. STUDENT USE PROHIBITED
L10-1

Module10: Using JavaScript and jQuery for Responsive MVC


4 Web Applications
Lab: Using JavaScript and jQuery for
Responsive MVC 4 Web Applications
Exercise 1: Creating and Animating the Slideshow View
 Task 1: Import and test the slideshow view.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the taskbar, click the File Explorer icon.

5. In the Libraries window, navigate to Allfiles (D):\Labfiles\Mod10\Starter


\PhotoSharingApplication, and then double-click PhotoSharingApplication.sln.

6. In the Solution Explorer pane of the, PhotoSharingApplication - Microsoft Visual Studio window,
under PhotoSharingApplication, expand Views, right-click Photo, point to Add, and then click
Existing Item.

7. In the Add Existing Item – PhotoSharingApplication dialog box, navigate to Allfiles


(D):\Labfiles\Mod10\Slide Show View, click SlideShow.cshtml, and then click Add.
8. In the Solution Explorer pane, expand Controllers, and then click PhotoControllers.cs.

9. In the PhotoControllers.cs code window, locate the following code.

throw new NotImplementedException("The Slideshow action is not yet ready");

10. Replace the code with the following code.

return View("SlideShow", context.Photos.ToList());

11. In the Solution Explorer pane, click Mvc.sitemap.

12. In the Mvc.sitemap code window, locate the following code.

<mvcSiteMapNode title="Add a Photo" visibility="*" controller="Photo" action="Create"


/>

13. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

<mvcSiteMapNode title="Slideshow" visibility="*" controller="Photo"


action="SlideShow" />

14. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Start
Debugging.

15. On the Adventure Works Photo Sharing page, click the Tools button, and then click F12 developer
tools.
MCT USE ONLY. STUDENT USE PROHIBITED
L10-2 Developing ASP.NET MVC 4 Web Applications

16. On the Cache menu of the developer window, click Clear browser cache.

17. A message stating “Are you sure you want to clear the browser cache?” is displayed. Click Yes to clear
the browser cache.

18. On the File menu of the developer window, click Exit.

19. On the Adventure Works Photo Sharing page, click the Slideshow link.

Note: Note that the Slide Show view displays all the photos in a large size, one below the
other.

20. In the Windows Internet Explorer window, click the Close button.

 Task 2: Modify the style sheet.


1. In the Solution Explorer pane, expand Content, and then click PhotoSharingStyles.css.

2. In the PhotoSharingStyles.css code window, locate the following code.

#slide-show DIV.slide-show-card {

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

position: absolute;
top: 0;
left: 0;
z-index: 8;

4. In the PhotoSharingStyles.css code window, locate the following code.

#slideshow-progress-bar-container {

5. Place the mouse cursor immediately before the located code, type the following code, and then press
Enter.

#slide-show DIV.active-card {
z-index: 10;
}

6. Place the mouse cursor at the end of the code you just typed, press Enter twice, and then type the
following code.

#slide-show DIV.last-active-card {
z-index: 9;
}

7. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Start
Debugging.

8. On the Adventure Works Photo Sharing page, click the Tools button, and then click F12 developer
tools.

9. On the Cache menu of the developer window, click Clear browser cache.

10. A message stating “Are you sure you want to clear the browser cache?” is displayed. Click Yes to clear
the browser cache.

11. On the File menu of the developer window, click Exit.


MCT USE ONLY. STUDENT USE PROHIBITED
L10-3

12. On the Adventure Works Photo Sharing page, click the Slideshow link.

Note: Note that only Sample Photo 13 is displayed. The other photos are rendered in the
same absolute position behind Sample Photo 13. Your script will use the Z-order of different
style classes to move each photo to the top of the stack.

13. In the Windows Internet Explorer window, click the Close button.

 Task 3: Animate the photo cards in the slideshow.


1. In the Solution Explorer pane, right - click PhotoSharingApplication, point to Add, and then click
New Folder.

2. In the NewFolder1 box, type Scripts, and then press Enter.

3. In the Solution Explorer pane, right-click Scripts, point to Add, and then click New Item.
4. In the Add New Item – PhotoSharingApplication dialog box, click JavaScript File.

5. In the Name box, type SlideShow.js, and then click Add.

6. In the SlideShow.js code window, type the following code.

var percentIncrement;
var percentCurrent = 100;

7. Place the mouse cursor at the end of the variable you just created, press Enter twice, and then type
the following code.

function slideSwitch () {
}

8. In the slideSwitch() function code block, type the following code.

var $activeCard = $('#slide-show DIV.active-card');

9. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

if ($activeCard.length == 0) {
$activeCard = $('#slide-show DIV.slide-show-card:last');
}

10. Place the mouse cursor at the end of the if statement you just created, press Enter, and then type the
following code.

var $nextCard = $activeCard.next();

11. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

if ($nextCard.length == 0) {
$nextCard = $('#slide-show DIV.slide-show-card:first');
}

12. Place the mouse cursor at the end of the if statement you just created, press Enter, and then type the
following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L10-4 Developing ASP.NET MVC 4 Web Applications

$activeCard.addClass('last-active-card');

Note: Note that this last-active-card class applies the z-order value 9, from the style
sheet.

13. Place the mouse cursor at the end of the last-active-card class you just added, press Enter, and then
type the following code.

$nextCard.css({ opacity: 0.0 });

14. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

$nextCard.addClass('active-card');

Note: Note that this active-card class applies the z-order value 10, from the style sheet.

15. Place the mouse cursor at the end of the active-card class you just added, press Enter, and then type
the following code.

$nextCard.animate({ opacity: 1.0 }, 1000, function () {


$activeCard.removeClass('active-card last-active-card');
});

16. Place the mouse cursor at the end of the slideSwitch() function, press Enter twice, and then type the
following code.

$(document).ready(function() {
});

17. In the anonymous function you just created, type the following code.

setInterval("slideSwitch()", 5000);

18. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save All.

 Task 4: Link to the script and test the animation.


1. In the Solution Explorer pane, under Photo, click SlideShow.cshtml.

2. In the SlideShow.cshtml code window, locate the following code.

<h2>Slideshow</h2>

3. Place the mouse cursor immediately before the located code, type the following code, and then press
Enter twice.

<script type="text/javascript" src="@Url.Content("~/Scripts/SlideShow.js")"></script>

4. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Start
Debugging.

5. On the Adventure Works Photo Sharing page, click the Slideshow link.
MCT USE ONLY. STUDENT USE PROHIBITED
L10-5

Note: Note that your script fades between different photos in the application.

6. In the Windows Internet Explorer window, click the Close button.

Results: At the end of this exercise, you will have created a Photo Sharing application with a slideshow
page that displays all the photos in the application, sequentially.
MCT USE ONLY. STUDENT USE PROHIBITED
L10-6 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Adding a jQueryUI ProgressBar Widget


 Task 1: Complete the slideshow view and template view.
1. In the Solution Explorer pane, under Photo, click SlideShow.cshtml.

2. In the SlideShow.cshtml code window, locate the following code.

<div id="slideshow-progress-bar-container">

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

<div id="slideshow-progress-bar"></div>

4. In the Solution Explorer pane, under Views, expand Shared, and then click _MainLayout.cshtml.

5. In the _MainLayout.cshtml code window, locate the following code:

</head>

6. Place the mouse cursor immediately before the located code, type the following code, and then press
Enter.

<script type="text/javascript"
src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.0/jquery-ui.min.js"></script>

7. In the _MainLayout.cshtml code window, locate the following code.

<link type="text/css" rel="stylesheet" href="~/Content/PhotoSharingStyles.css">

8. Place the mouse cursor immediately before the located code, type the following code, and then press
Enter.

<link type="text/css" rel="stylesheet"


href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />

9. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save All.

 Task 2: Modify the slideshow script.


1. In the Solution Explorer pane, under Scripts, click SlideShow.js.

2. In the SlideShow.js code window, locate the following code.

$(document).ready(function() {

3. Place the mouse cursor immediately before the located code, press Enter twice, and then type the
following code.

function calculateIncrement() {
}

4. Place the cursor within the calculateIncrement() function you just created, and type the following
code.

var cardCount = $('#slide-show DIV.slide-show-card').length;

5. Place the mouse cursor at the end of the variable you just created, press Enter, and then type the
following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L10-7

percentIncrement = 100 / cardCount;

6. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

$('#slideshow-progress-bar').progressbar({
value: 100
});

Note: Note that the jQueryUI progressbar() function displays a progress bar widget.

7. In the SlideShow.js code window, locate the following code.

setInterval("slideSwitch()", 5000);

8. Place the mouse cursor immediately before the located code, type the following code, and then press
Enter.

calculateIncrement();

9. In the SlideShow.js code window, locate the following code.

var $activeCard = $('#slide-show DIV.active-card');

10. Place the mouse cursor immediately before the located code, type the following code, and then press
Enter.

percentCurrent += percentIncrement;

11. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

if (percentCurrent > 100) {


percentCurrent = percentIncrement;
}

12. Place the mouse cursor at the end of the if statement you just added, press Enter, and then type the
following code.

$('#slideshow-progress-bar').progressbar({
value: percentCurrent
});

13. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save All.

 Task 3: Test the slideshow view.


1. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Start
Debugging.

2. On the Adventure Works Photo Sharing page, click the Tools button, and then click F12 developer
tools.

3. On the Cache menu of the developer window, click Clear browser cache.

4. A message stating “Are you sure you want to clear the browser cache?” is displayed. Click Yes to clear
the browser cache.
MCT USE ONLY. STUDENT USE PROHIBITED
L10-8 Developing ASP.NET MVC 4 Web Applications

5. On the File menu of the developer window, click Exit.

6. On the Adventure Works Photo Sharing page, click the Slideshow link.

Note: On the Slideshow tab of the Adventure Works Photo Sharing page, note that all the
photos in the application are displayed, with a fade animation and a progress bar.

7. In the Windows Internet Explorer window, click the Close button.

8. In the PhotoSharingApplication – Microsoft Visual Studio window, click the Close button.

Results: At the end of this exercise, you will have created a slideshow page with a progress bar that
displays the position of the current photo in the list of photos.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-1

Module11: Controlling Access to ASP.NET MVC 4 Web


Applications
Lab: Controlling Access to ASP.NET MVC 4
Web Applications
Exercise 1: Configuring Authentication and Membership Providers
 Task 1: Configure a new Windows Azure SQL database.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the Internet Explorer icon.


6. In the Address bar of the Internet Explorer window, type http://www.windowsazure.com/ and then
press Enter.

7. On the Windows Azure page, click PORTAL.

8. In the someone@example.com box, type <username>.

9. In the Password box, type <password>, and then click Sign in.

10. In the left pane of the Windows Azure page, click SQL DATABASES, and then click NEW.
11. In the New dialog box, click SQL DATABASE, and then click CUSTOM CREATE.

12. In the Name box of the NEW SQL DATABASE - CUSTOM CREATE dialog box, type
PhotoAppServices.
13. In the SERVER box, click New SQL Database Server, and then click the Next button.

14. In the LOGIN NAME box, type <your Windows Azure account ID>.

15. In the LOGIN PASSWORD box, type Pa$$w0rd.

16. In the LOGIN PASSWORD CONFIRMATION box, type Pa$$w0rd.

17. In the REGION box, click <a region close to you>, and then click the Complete button.

Note: In the sql databases result pane, note that a new database, PhotoAppServices, is
created.

18. In the Name column of the sql databases page, click PhotoAppServices, and then click the
DASHBOARD tab.

19. In the quick glance section, click the Manage allowed IP addresses link.

20. In the RULE NAME box of the result window, type First Address Range.

21. In the START IP ADDRESS box, type <first address in range>.

22. In the END IP ADDRESS box, type <last address in range>.


MCT USE ONLY. STUDENT USE PROHIBITED
L11-2 Developing ASP.NET MVC 4 Web Applications

23. In the lower pane of the SQL Databases – Windows Azure window, click Save.

Note: To add more IP address ranges, you can repeat the steps 18 – 23.

 Task 2: Install Universal Providers.


1. On the taskbar, click the File Explorer icon.

2. In the Libraries window, navigate to Allfiles (D):\Labfiles\Mod11\Starter


\PhotoSharingApplication, and then double-click PhotoSharingApplication.sln.

3. On the PROJECT menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Manage NuGet Packages.

4. In the navigation pane of the PhotoSharingApplication – Manage NuGet Packages dialog box,
click Online.

5. In the Search Online box of the PhotoSharingApplication – Manage NuGet Packages dialog box,
type Microsoft.aspnet.providers.

6. On the PhotoSharingApplication – Manage NuGet Packages dialog box, click Microsoft ASP.NET
Universal Providers, and then click Install.

7. In the License Acceptance dialog box, click I Accept.


8. When the installation is complete, in the PhotoSharingApplication – Manage NuGet Packages
dialog box, click Close.

 Task 3: Configure Providers in Web.config.


1. In the Solution Explorer pane of the PhotoSharingApplication – Microsoft Visual Studio window,
underPhotoSharingApplication, click Web.config.

2. In the <connectionStrings> element of the Web.config code window, locate the following code, and
then press Delete.

<add name="DefaultConnection" providerName="System.Data.SQLClient"


connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-
PhotoSharingApplication-<id>;Integrated Security=SSPI" />

3. Switch to Internet Explorer.

4. In the NAME column of the sql databases result pane, click PhotoAppServices.

5. In the quick glance section of the photoappservices page, click the Show connection strings link.

6. In the ADO.NET box of the Connection Strings dialog box, select the given text, right-click the
selected text, and then click Copy.

7. On the taskbar, click the PhotoSharingApplication - Microsoft Visual Studio icon.

8. In the Web.config code window, place the mouse cursor before the </connectionStrings> tag, press
Enter, and then type the following code.

<add name="PhotoAppServices"
connectionString = ""
providerName =
"System.Data.SqlClient" />

9. In the connectionStrings code block you just typed, place the mouse cursor within the quotes after
connectionString =, right-click, and then click Paste.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-3

10. In the content you just pasted, locate the text {your_password_here}, and then replace the text with
Pa$$w0rd.

11. In the Web.config code window, locate the following code.

<system.web>

12. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

<authentication mode="Forms">
</authentication>

13. Place the cursor within the <authentication> element you just created, and then type the following
code.

<forms loginUrl="~/Account/Login" timeout="2880" />

14. In the Web.config code window, locate the following code.

<add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider,


System.Web.Providers, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"
applicationName="/"/>

15. Within the located code, select the following text.

DefaultConnection

16. Replace the selected text with the following text.

PhotoAppServices

17. In the Web.config code window, locate the following code.

<add name="DefaultMembershipProvider"
type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="false"
requiresUniqueEmail="false" maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10" applicationName="/" />

18. Within the located code, select the following text.

DefaultConnection

19. Replace the selected text with the following text.

PhotoAppServices

20. In the Web.config code window, locate the following code.

<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider,


System.Web.Providers, Version=1.0.0.0 Culture=neutral,
PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection
applicationName="/"/>

21. Within the located code, select the following text.


MCT USE ONLY. STUDENT USE PROHIBITED
L11-4 Developing ASP.NET MVC 4 Web Applications

DefaultConnection

22. Replace the selected text with the following text.

PhotoAppServices

23. In the Web.config code window, locate the following code.

<add name="DefaultSessionProvider"
type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers,
Version=1.0.0.0 Culture=neutral, PublicKeyToken=31bf3856ad364e35"
connectionStringName="DefaultConnection"/>

24. Within the located code, select the following text.

DefaultConnection

25. Replace the selected text with the following text.

PhotoAppServices

26. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

Results: At the end of this exercise, you will have created a Photo Sharing application that is configured to
use Windows Azure SQL database for user accounts and membership information. In subsequent
exercises, you will add model classes, actions, and views to implement authentication for this database.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-5

Exercise 2: Building the Logon and Register Views


 Task 1: Add account model classes.
1. In the Solution Explorer pane, right-click Models, point to Add, and then click Class.

2. In the Name box of the Add New Item – PhotoSharingApplication dialog box, type
AccountModelClasses.cs, and then click Add.

3. In the AccountModelClasses.cs code window, locate the following code.

using System.Web;

4. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

5. In the AccountModelClasses.cs code window, locate the following code.

public class AccountModelClasses

6. Replace the located code with the following code.

public class UsersContext : DbContext

7. Place the mouse cursor within the UsersContext class code block you just created, and then type the
following code.

public UsersContext ()
: base("PhotoAppServices")
{
}

8. Place the mouse cursor after the UsersContext class code block, press Enter twice, and then type the
following code.

public class Login


{
}

9. Place the mouse cursor within the Login class code block you just created, and then type the
following code.

[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

10. Place the mouse cursor at the end of the UserName property code block you just created, press
Enter twice, and then type the following code.

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }

11. Place the mouse cursor at the end of the Password property code block you just created, press Enter
twice, and then type the following code.

[Display(Name = "Remember me?")]


public bool RememberMe { get; set; }
MCT USE ONLY. STUDENT USE PROHIBITED
L11-6 Developing ASP.NET MVC 4 Web Applications

12. Place the mouse cursor after the Login class code block, press Enter twice, and then type the
following code.

public class Register


{
}

13. Place the mouse cursor within the Register class code block you just created, and then type the
following code.

[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

14. Place the mouse cursor at the end of the UserName property code block you just created, press
Enter twice, and then type the following code.

[Required]
[DataType(DataType.Password)]
public string Password { get; set; }

15. Place the mouse cursor at the end of the Password property code block you just created, press Enter
twice, and then type the following code.

[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not
match.")]
public string ConfirmPassword { get; set; }

16. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 2: Add an account controller.


1. In the Solution Explorer pane, right-click Controllers, point to Add, and then click Controller.

2. In the Controller name box of the Add Controller dialog box, type AccountController.

3. In the Template box, ensure that the Empty MVC controller template is selected, and then click
Add.

4. In the AccountController.cs code window, locate the following code, and then press Delete.

//
//GET: /Account/
public ActionResult Index ()
{
return View();
}

5. Locate the following code.

using System.Web.Mvc;

6. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using System.Web.Security;
using PhotoSharingApplication.Models;

7. Place the mouse cursor within the AccountController class code block, and then type the following
code.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-7

public ActionResult Login(string returnUrl)


{
}

8. Place the mouse cursor within the Login action method code block, and then type the following
code.

ViewBag.ReturnUrl = returnUrl;
return View();

9. Place the mouse cursor within the AccountController class but outside any action method code
block, and then type the following code.

[HttpPost]
public ActionResult Login(Login model, string returnUrl)
{
}

10. Place the mouse cursor within the Login action method code block for the POST HTTP verb, and
then type the following code.

if (ModelState.IsValid)
{
}

11. Place the mouse cursor within the if code block you just added, and then type the following code.

if (Membership.ValidateUser(model.UserName, model.Password))
{
}
else
{
}

12. Place the cursor within the if statement code block you just added, and then type the following code.

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

13. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}

14. Place the mouse cursor within the empty else clause you created in the first instance, and then type
the following code.

ModelState.AddModelError("", "The username or password is incorrect");

15. Place the mouse cursor at the end of the Login action method for the POST HTTP verb, outside any
of the if statements, and then type the following code.

return View(model);
MCT USE ONLY. STUDENT USE PROHIBITED
L11-8 Developing ASP.NET MVC 4 Web Applications

16. Place the mouse cursor within the AccountController class but outside any action method, and then
type the following code.

public ActionResult LogOff()


{
}

17. Place the mouse cursor within the LogOff action code block you just created, and then type the
following code.

FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");

18. Place the cursor within the AccountController class but outside any action method, and then type
the following code.

public ActionResult Register()


{
}

19. Place the mouse cursor within the Register action code block you just created, and then type the
following code.

return View();

20. Place the mouse cursor within the AccountController class but outside any action method code
block, and then type the following code.

[HttpPost]
public ActionResult Register(Register model)
{
}

21. Place the mouse cursor within the Register action method code block for the POST HTTP verb, and
then type the following code.

if (ModelState.IsValid)
{
}

22. Place the mouse cursor within the if code block you just created, and then type the following code.

try
{
}
catch (MembershipCreateUserException e)
{
}

23. Place the mouse cursor within the try code block you just created, and then type the following code.

MembershipUser NewUser = Membership.CreateUser(model.UserName, model.Password);

24. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");

25. Place the mouse cursor within the catch code block you created, and then type the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-9

ModelState.AddModelError("Registration Error", "Registration error: " +


e.StatusCode.ToString());

26. Place the mouse cursor at the end of the Register action method for the POST HTTP verb, outside
any of the if statements, and then type the following code.

return View(model);

27. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 3: Import Logon and Register views.


1. In the Solution Explorer pane, right-click Views, point to Add, and then click New Folder.

2. In the NewFolder1 box, type Account, and then press Enter.

3. Under Views, right-click Account, point to Add, and then click Existing Item.
4. In the Add Existing Item – PhotoSharingApplication dialog box, navigate to Allfiles
(D):\Labfiles\Mod11\Account Views, click Login.cshtml, and then click Add.

5. In the Solution Explorer pane, right-click Account, point to Add, and then click Existing Item.
6. In the Add Existing Item – PhotoSharingApplication dialog box, navigate to Allfiles
(D):\Labfiles\Mod11\Account Views, click Register.cshtml, and then click Add.

 Task 4: Add authentication controls to the Template view.


1. In the Solution Explorer pane, under Views, expand Shared, and then click _MainLayout.cshtml.
2. In the _MainLayout.cshtml code window, locate the following code.

<div class="clear-floats" />

3. Place the mouse cursor immediately before the located code, press Enter, and then type the following
code.

<div class="login-controls">
</div>

4. Place the mouse cursor within the DIV element you just created, and then type the following code.

@if (Request.IsAuthenticated)
{
}
else
{
}

5. Place the mouse cursor within the @if statement code block you just created, and then type the
following code.

@:Hello, @User.Identity.Name

6. Place the mouse cursor at the end of the greeting message you just created, press Enter, and then
type the following code.

@Html.ActionLink("Log Off", "LogOff", "Account")

7. Place the mouse cursor within the else code block you created, and then type the following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L11-10 Developing ASP.NET MVC 4 Web Applications

@Html.ActionLink("Log In", "Login", "Account")

8. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

@Html.ActionLink("Register", "Register", "Account")

9. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 5: Test registration, log on, and log off.


1. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, click Register.

3. In the User name box of the Register section, type David Johnson.
4. In the Password box, type Pa$$w0rd.

5. In the Confirm password box, type Pa$$w0rd, and then click Register.

Note: At the upper-right corner of the Welcome to Adventure Works Photo Sharing page,
note that the authentication controls show your first name to indicate that you are logged on.

6. On the Welcome to Adventure Works Photo Sharing page, click the Log Off link.

Note: Before clicking the Log In link, click any other link or menus to ensure that you
cannot log on to the web application.

7. On the Welcome to Adventure Works Photo Sharing page, click the Log In link

8. In the User name box of the Login section, type David Johnson.

9. In the Password box, type Pa$$w0rd, and then click Log in.

Note: At the upper-right corner of the Welcome to Adventure Works Photo Sharing page,
if you see your first name in the authentication controls, it implies that the model classes,
controller, and views are working as expected.

10. In the Windows Internet Explorer window, click the Close button.

Results: At the end of this exercise, you will create a Photo Sharing application in which users can register
for an account, log on, and log off.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-11

Exercise 3: Authorizing Access to Resources


 Task 1: Restrict access to Photo actions.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Controllers, click PhotoController.cs.

2. In the PhotoController.cs code window, locate the following code.

public ActionResult Create()

3. Place the mouse cursor before the located code, type the following code, and then press Enter.

[Authorize]

4. In the PhotoController.cs code window, locate the following code.

public ActionResult Create(Photo photo, HttpPostedFileBase image)

5. Place the mouse cursor before the located code, type the following code, and then press Enter.

[Authorize]

6. In the PhotoController.cs code window, locate the following code.

public ActionResult Delete(int id)

7. Place the mouse cursor before the located code, type the following code, and then press Enter.

[Authorize]

8. In the PhotoController.cs code window, locate the following code.

public ActionResult DeleteConfirmed(int id)

9. Place the mouse cursor before the located code, type the following code, and then press Enter.

[Authorize]

10. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 2: Restrict access to the Comment actions.


1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Controllers, click CommentController.cs.

2. In the CommentController.cs code window, locate the following code.

public PartialViewResult _Create(int PhotoId)

3. Place the mouse cursor before the located code, type the following code, and then press Enter.

[Authorize]

4. In the CommentController.cs code window, locate the following code.

public ActionResult Delete(int id = 0)

5. Place the mouse cursor before the located code, type the following code, and then press Enter.
MCT USE ONLY. STUDENT USE PROHIBITED
L11-12 Developing ASP.NET MVC 4 Web Applications

[Authorize]

6. In the CommentController.cs code window, locate the following code.

public ActionResult DeleteConfirmed(int id)

7. Place the mouse cursor before the located code, type the following code, and then press Enter.

[Authorize]

8. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 3: Restrict access to the Account actions.


1. In the Solution Explorer pane, under Controllers, click AccountController.cs.

2. In the AccountController.cs code window, locate the following code.

public class AccountController : Controller

3. Place the mouse cursor before the located code, type the following code, and then press Enter.

[Authorize]

4. In the AccountController.cs code window, locate the following code.

public ActionResult Login(string returnUrl)

5. Place the mouse cursor before the located code, type the following code, and then press Enter.

[AllowAnonymous]

6. In the AccountController.cs code window, locate the following code.

public ActionResult Login(Login model, string returnUrl)

7. Place the mouse cursor before the located code, type the following code, and then press Enter.

[AllowAnonymous]

8. In the AccountController.cs code window, locate the following code.

public ActionResult Register()

9. Place the mouse cursor before the located code, type the following code, and then press Enter.

[AllowAnonymous]

10. In the AccountController.cs code window, locate the following code.

public ActionResult Register(Register model)

11. Place the mouse cursor before the located code, type the following code, and then press Enter.

[AllowAnonymous]

12. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-13

 Task 4: Check authentication status in a view.


1. In the Solution Explorer pane, under Views, expand Shared, and then click
_CommentsForPhoto.cshtml.

2. In the _CommentsForPhoto.cshtml code window, locate the following code.

@Html.Action("_Create", "Comment", new { PhotoID = ViewBag.PhotoId })

3. Place the mouse cursor before the located code, type the following code, and then press Enter.

@if (Request.IsAuthenticated)
{

4. Place the mouse cursor after the located code, press Enter, and then type the following code.

5. In the _CommentsForPhoto.cshtml code window, at the end of the code you just typed, press Enter,
and then type the following code.

else
{
@Html.ActionLink("To comment you must log in", "Login", "Account");
}

6. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 5: Test authorization.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, click Add a Photo.

Note: ASP.NET forwards the browser to the Login action of the Accounts controller
because the request is unauthenticated.

3. On the Login page, click All Photos.

4. On the All Photos page, click the Display link corresponding to a photo of your choice.

5. On the result page, view the comments for the photo.

Note: A link to the Login action is displayed at the lower end of the page, instead of the
comment creation form.

6. On the result page, click the To comment you must log in link.
7. On the Login page, in the User name box, type David Johnson, in the Password box, type
Pa$$w0rd, and then click Log in.

8. On the Welcome to Adventure Works Photo Sharing page, click the Display link corresponding to a
photo of your choice.

9. On the result page, view the comments for the photo.


MCT USE ONLY. STUDENT USE PROHIBITED
L11-14 Developing ASP.NET MVC 4 Web Applications

Note: The create comment form is displayed because the request is authenticated.

10. In the Comments section of the result page, in the Subject box, type Authenticated Test Comment.

11. In the Body box, type <a comment of your choice>, and then click Create.

12. In the Windows Internet Explorer window, click the Close button.

Results: At the end of this exercise, you will have authorized anonymous and authenticated users to
access resources in your web application.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-15

Exercise 4: Building a Password Reset View


 Task 1: Add a Local Password Model class.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
under Models, click AccountModelClasses.cs.

2. In the AccountModelClasses.cs code window, place the mouse cursor in the


PhotoSharingApplication.Models namespace code block, but outside any class code block, and
then type the following code.

public class LocalPassword


{
}

3. In the LocalPassword class code block, type the following code.

[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }

4. Place the mouse cursor at the end of the OldPassword property code block that you just created,
press Enter twice, and then type the following code.

[Required]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }

5. Place the mouse cursor at the end of the NewPassword property code block you just created, press
Enter twice, and then type the following code.

[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The password and confirmation password do not
match.")]
public string ConfirmPassword { get; set; }

6. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 2: Add reset password actions.


1. In the Solution Explorer pane, under Controllers, click AccountControllers.cs.

2. In the AccountControllers.cs code window, place the mouse cursor in the AccountController class
code block, but outside any action method code block, press Enter, and then type the following code.

public enum ManageMessageId


{
ChangePasswordSuccess,
SetPasswordSuccess,
}

3. Place the mouse cursor at the end of the ManageMessageId code block, press Enter, and then type
the following code.

public ActionResult ResetPassword (ManageMessageId? message)


{
}
MCT USE ONLY. STUDENT USE PROHIBITED
L11-16 Developing ASP.NET MVC 4 Web Applications

4. In the ResetPassword action code block, type the following code.

if (message != null)
{
ViewBag.StatusMessage = "Your password has been changed";
}

5. In the ResetPassword action code block, press Enter, and then type the following code.

ViewBag.ReturnUrl = Url.Action("ResetPassword");

6. In the ResetPassword action code block, press Enter, and then type the following code.

return View();

7. In the AccountControllers.cs code window, place the mouse cursor within the AccountController
class, but outside any action method code block, press Enter, and then type the following code.

[HttpPost]
public ActionResult ResetPassword (LocalPassword model)
{
}

8. In the ResetPassword action code block for the HTTP POST verb, type the following code.

ViewBag.ReturnUrl = Url.Action("ResetPassword");

9. In the ResetPassword action code block for the HTTP POST verb, press Enter, and then type the
following code.

if (ModelState.IsValid)
{
}

10. In the if statement code block, type the following code.

bool changePasswordSucceeded;
try
{
}
catch (Exception)
{
}

11. In the try code block, type the following code.

changePasswordSucceeded = Membership.Provider.ChangePassword(User.Identity.Name,
model.OldPassword, model.NewPassword);

12. In the catch block, type the following code.

changePasswordSucceeded = false;

13. Place the cursor at the end of the try…catch code block, press Enter, and then type the following
code.

if (changePasswordSucceeded)
{
return RedirectToAction("ResetPassword", new { message =
ManageMessageId.ChangePasswordSuccess });
}
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L11-17

14. Place the mouse cursor at the end of the if statement code block, press Enter, and then type the
following code.

else
{
ModelState.AddModelError("", "The current password is incorrect or the new
password is invalid");
}

15. Place the mouse cursor at the end of the ResetPassword action code block for the POST HTTP verb,
but outside the if statements, press Enter, and then type the following code.

return View(model);

16. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 3: Import the reset password view.


1. In the Solution Explorer pane, under Views, right-click Account, point to Add, and then click Existing
Item.

2. In the Add Existing Item - PhotoSharingApplication dialog box, navigate to Allfiles


(D):\Labfiles\Mod11\Account Views, click ResetPassword.cshtml, and then click Add.

Note: In the Solution Explorer pane, note that the ResetPassword.cshtml file is added to
the Account folder.

 Task 4: Add a link to the reset password view.


1. In the Solution Explorer pane, under Shared, click _MainLayout.cshtml.

2. In the _MainLayout.cshtml code window, locate the following code.

@Html.ActionLink("Log Off", "LogOff", "Account")

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

@Html.ActionLink("Reset", "ResetPassword", "Account")

4. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 5: Test password reset.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, click the Log In link.

3. On the logon page, in the User name box, type David johnson, in the Password box, type
Pa$$w0rd, and then click Log in.

4. On the Welcome to Adventure Works Photo Sharing page, click the Reset link.

5. On the ResetPassword page, in the Current password box, type Pa$$w0rd.

6. In the New password box, type Pa$$w0rd2, in the Confirm new password box, type Pa$$w0rd2,
and then click Change Password.
MCT USE ONLY. STUDENT USE PROHIBITED
L11-18 Developing ASP.NET MVC 4 Web Applications

Note: The message, “Your password has been changed” is displayed.

7. In the Windows Internet Explorer window, click the Close button.

8. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

9. In the SQL Databases – Windows Azure window, click the Close button.

Results: At the end of this exercise, you will build a Photo Sharing application in which registered users
can reset their own password.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L12-1

Module12: Building a Resilient ASP.NET MVC 4 Web


Application
Lab: Building a Resilient ASP.NET MVC 4
Web Application
Exercise 1: Creating Favorites Controller Actions
 Task 1: Create the Favorites Slideshow action.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the taskbar, click the File Explorer icon.

5. In the Libraries window, navigate to Allfiles (D:)\Labfiles\Mod12\Starter


\PhotoSharingApplication, and then double-click PhotoSharingApplication.sln.

6. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand PhotoSharingApplication, expand Controllers, and then click PhotoController.cs.
7. In the PhotoController.cs code window, locate the following code.

public ActionResult SlideShow()


{
return View("SlideShow", context.Photos.ToList());
}

8. Place the mouse cursor at the end of the located code, press Enter twice, and then type the following
code.

public ActionResult FavoritesSlideShow()


{
}

9. Place the cursor within the FavoritesSlideShow action code block you just created, and then type the
following code.

List<Photo> favPhotos = new List<Photo>();

10. Place the mouse cursor at the end of the favPhotos list code block you just created, press Enter, and
then type the following code.

List<int> favoriteIds = Session["Favorites"] as List<int>;

11. Place the mouse cursor at the end of the favoriteIds list code block you just created, press Enter, and
then type the following code.

if (favoriteIds == null)
{
favoriteIds = new List<int>();
}
MCT USE ONLY. STUDENT USE PROHIBITED
L12-2 Developing ASP.NET MVC 4 Web Applications

12. Place the mouse cursor at the end of the if statement code block you just created, press Enter, and
then type the following code.

Photo currentPhoto;

13. Place the mouse cursor at the of the currentPhoto object code block you just created, press Enter,
and then type the following code.

foreach (int currentId in favoriteIds)


{
}

14. Place the mouse cursor within the foreach code block you just created, and then type the following
code.

currentPhoto = context.FindPhotoById(currentId);

15. Place the mouse cursor at the end of the code you just created, press Enter, and then type the
following code.

if (currentPhoto != null)
{
favPhotos.Add(currentPhoto);
}

16. Place the mouse cursor at the end of the foreach code block you created earlier, press Enter, and
then type the following code.

return View("SlideShow", favPhotos);

Note: You do not need to create a new view for the FavoritesSlideShow action. Instead,
you can re-use the SlideShow.cshtml view by passing a different list of Photo objects.

17. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 2: Create the Add Favorite action.


1. Place the mouse cursor immediately after the FavoritesSlideShow action code block, but inside the
PhotoController class code block, and then type the following code.

public ContentResult AddFavorite(int PhotoId)


{
}

2. Place the cursor within the AddFavorite action code block you just created, and then type the
following code.

List<int> favoriteIds = Session["Favorites"] as List<int>;

3. Place the mouse cursor at the end of the favoriteIds list code block you just created, press Enter, and
then type the following code.

if (favoriteIds == null)
{
favoriteIds = new List<int>();
}
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L12-3

4. Place the mouse cursor at the end of the code block you just created, press Enter, and then type the
following code.

favoriteIds.Add(PhotoId);

5. Place the mouse cursor at the end of the code you just created, press Enter, and then type the
following code.

Session["Favorites"] = favoriteIds;

6. Place the mouse cursor at the end of the code you just created, press Enter, and then type the
following code.

return Content("The picture has been added to your favorites", "text/plain",


System.Text.Encoding.Default);

7. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

Results: After completing this exercise, you will be able to create controller actions that store values in the
session state of the web application, and retrieve values from the same session state.
MCT USE ONLY. STUDENT USE PROHIBITED
L12-4 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Implementing Favorites in Views


 Task 1: Add an AJAX action link in the Photo Display view.
1. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand Views, expand Photo, and then click Display.cshtml.

2. In the Display.cshtml code window, locate the following code.

<div>
<span class="display-label">
@Html.DisplayNameFor(model => model.CreatedDate):
</span>
<span class="display-field">
@Html.DisplayFor(model => model.CreatedDate)
</span>
</div>

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

<div id="addtofavorites">
</div>

4. In the DIV element you just created, type the following code.

@Ajax.ActionLink("Add this photo to your favorites",


"AddFavorite",
"Photo",
new { PhotoId = Model.PhotoID },
new AjaxOptions {
UpdateTargetId = "addtofavorites",
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace
})

5. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 2: Add a link and update the site map.


1. In the Solution Explorer pane, under Views, expand Shared, and then click _MainLayout.cshtml.
2. In the _MainLayout.cshtml code window, locate the following code.

<div id="breadcrumb">
Current Location: @Html.MvcSiteMap().SiteMapPath()
</div>

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

@if (Session["Favorites"] != null) {


}

4. In the @if statement code block, type the following code.

<div id="favorites-link">
</div>

5. In the DIV element you just created, type the following code.

@Html.ActionLink("Favorite Photos", "FavoritesSlideShow", "Photo")


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L12-5

6. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

7. In the Solution Explorer pane, click Mvc.sitemap.

8. In the Mvc.sitemap code window, locate the following code.

<mvcSiteMapNode title="Slideshow" visibility="*" controller="Photo"


action="SlideShow" />

9. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

<mvcSiteMapNode title="Favorites" visibility="SiteMapPathHelper,!*"


controller="Photo" action="FavoritesSlideShow" />

10. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 3: Test favorites.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

Note: On the Welcome to Adventure Works Photo Sharing page, note that there is no link
to the FavoritesSlideShow action.

2. On the Welcome to Adventure Works Photo Sharing page, click All Photos.

3. On the All Photos page, click the Display link corresponding to any photo of your choice.

Note: Ensure that the photo you have selected is not already added to the favorites list.

4. On the result page, click the Add this photo to your favorites link.

Note: The message, “The picture has been added to your favorites” is displayed. This
indicates that the AJAX action displays a confirmation message without a full-page reload.

5. On the result page, click the Back to List link.

6. On the All Photos page, click the Display link corresponding to any photo of your choice.

Note: Ensure that the photo you have selected is not already added to the favorites list.

7. On the result page, click the Add this photo to your favorites link, and then click the Back to List
link.

8. On the All Photos page, click the Display link corresponding to any photo of your choice.

Note: Ensure that the photo you selected is not already added to the favorites list.

9. On the result page, click the Add this photo to your favorites link, and then click the Back to List
link.
MCT USE ONLY. STUDENT USE PROHIBITED
L12-6 Developing ASP.NET MVC 4 Web Applications

10. On the All Photos page, click Home, and then click the Favorite Photos link.

Note: The slideshow displays the photos that were marked as favorites.
On the Home page, if you are not able to view the Favorite Photos link, click the Refresh
button.

11. In the Windows Internet Explorer window, click the Close button.

12. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to :


Create the user interface components for the favorite photos functionality.

Test the functionality of the user interface components.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L13-1

Module13: Using Windows Azure Web Services in ASP.NET


MVC 4 Web Applications
Lab: Using Windows Azure Web Services in
ASP.NET MVC 4 Web Applications
Exercise 1: Accessing Windows Azure and Bing Maps
 Task 1: Install the Windows Azure SDK.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the Internet Explorer icon.


6. In the Address bar of the Internet Explorer window, type
http://www.microsoft.com/web/downloads/platform.aspx, and then click the Go to button.

7. On the Download the Microsoft Web Platform page, click Free Download.

8. In the Navigation bar, click Run.

9. If the User Account Control dialog box appears, click Yes.

10. In the Search box of the Web Platform Installer 4.5 window, type Windows Azure SDK, and then
press Enter.

11. In the Web Platform Installer 4.5 window, click Windows Azure SDK for .NET (VS 2012) <latest
version>, click Add, and then click Install.
12. In the PREREQUISITES page of the Web Platform Installer 4.5 wizard, click I Accept.

13. On the FINISH page, click Finish.

14. In the Web Platform Installer 4.5 window, click Exit.

 Task 2: Create a Bing Maps Developer Account.


1. On the taskbar, click the Internet Explorer icon.

2. In the Address bar of the Internet Explorer window, type https://www.bingmapsportal.com, and
then click the Go to button.

3. On the Home – Bing Maps Account Center page, click the Sign In link.

4. On the Sign in to your Microsoft account page, in the someone@example.com box, type <Your
Windows Live account name>, and then, in the Password box, type Pa$$w0rd.
5. On the Sign in to your Microsoft account page, select the Keep me signed in check box, and then
click Sign in.

6. On the Sign Up – Bing Maps Account Center page, if the message “Do you want to use the account
<Your Windows Live account name>@hotmail.com for your Bing Maps account?” is displayed, click
Continue.
MCT USE ONLY. STUDENT USE PROHIBITED
L13-2 Developing ASP.NET MVC 4 Web Applications

7. In the Account Name box, type <Your Windows Live account name>, and then, in the Email Address
box, ensure that the text, <Your Windows Live account name> appears.

8. Select the I Agree check box, and then click Save.

 Task 3: Create a Bing Maps key.


1. In the My Account section of the Announcements – Bing Maps Account Center page, click Create or
view keys.

2. In the Application Name box of the Create key - Bing Maps Account Center page, type Photo
Sharing Application, in the Key Type box, ensure that the value is Trial, and then, in the
Application type box, ensure that the value is Public website.

3. In the Enter the characters you see box, type <the characters seen in the image>, and then click
Submit.

Note: On the Create Key - Bing Maps Account Center page, the message, “Key created
successfully” is displayed.

Results: After completing this exercise, you will be able to register an application to access the Bing Maps
APIs.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L13-3

Exercise 2: Creating a WCF Service for Windows Azure


 Task 1: Add a new Windows Azure Cloud Service project to the web application.
1. On the taskbar, click the File Explorer icon.

2. In the Libraries window, navigate to Allfiles


(D):\Labfiles\Mod13\Starter\PhotoSharingApplication, and then double-click
PhotoSharingApplication.sln.

3. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, point to
Add, and then click New Project.

4. In the navigation pane of the Add New Project dialog box, under Visual C#, and then click Cloud.

5. In the Add New Project dialog box, click Windows Azure Cloud Service, in the Name box, type
LocationChecker, and then click OK.
6. In the .NET Framework 4.5 roles box of the New Windows Azure Cloud Service dialog box, click
WCF Service Web Role, click the Right Arrow button, and then click OK.

7. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click the WCFServiceWebRole1 project, and then click Rename.

8. In the Solution Explorer pane, rename WCFServiceWebRole1 as LocationCheckerWebRole, and


then press Enter.

9. In the Solution Explorer pane, right-click IService1.cs, and then click Rename.

10. In the Solution Explorer pane, rename IService1.cs as ILocationCheckerService.cs, and then press
Enter.
11. In the Microsoft Visual Studio dialog box, click Yes.

12. In the Solution Explorer pane, right-click Service1.svc, and then click Rename.

13. In the Solution Explorer pane, rename Service1.svc as LocationCheckerService.svc, and then press
Enter.

14. In the Solution Explorer pane, under LocationCheckerService.svc, click


LocationCheckerService.svc.cs.
15. In the LocationCheckerService.svc.cs code window, locate the following code.

namespace WCFServiceWebRole1

16. In the LocationCheckerService.svc.cs code window, select the text, WCFServiceWebRole1, right-click
the text, point to Refactor, and then click Rename.

17. In the New name box of the Rename dialog box, type LocationCheckerWebRole, ensure that the
Preview reference changes check box is selected, and then click OK.

18. In the Preview Changes – Rename dialog box, click Apply.

19. In the Microsoft Visual Studio dialog box, click Yes.

20. In the LocationCheckerService.svc.cs code window, locate the following code.

public class Service1 : ILocationCheckerService

21. In the located code, select the text Service1, right-click Service1, point to Refactor, and then click
Rename.
MCT USE ONLY. STUDENT USE PROHIBITED
L13-4 Developing ASP.NET MVC 4 Web Applications

22. In the New name box of the Rename dialog box, type LocationCheckerService, ensure that the
Preview reference changes check box is selected, and then click OK

23. In the Preview Changes – Rename dialog box, click Apply.

24. In the Solution Explorer pane, under Roles, click ServiceDefinition.csdef.

25. In the ServiceDefinition.csdef code window, locate the following code.

<WebRole name="LocationCheckerWebRole" vmsize="Small">

26. In the ServiceDefinition.csdef code window, replace the located code with the following code.

<WebRole name="LocationCheckerWebRole" vmsize="ExtraSmall">

27. On the FILE menu of the PhotoSharingApplication - Micorosft Visual Studio window, click Save
All.

 Task 2: Create the Location Checker Service interface.


1. In the Solution Explorer pane, under LocationCheckerWebRole, click ILocationCheckerService.cs

2. In the ILocationCheckerService.cs code window, locate the following code, select the code, and then
press Delete.

[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

3. Place the mouse cursor in the ILocationCheckerService interface code block, and then type the
following code.

[OperationContract]
string GetLocation(string address);

4. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 3: Add a service reference to the Bing Maps Geocode service.


1. In the Solution Explorer pane, under LocationCheckerWebRole, right-click References, and then
click Add Service Reference.

2. In the Address box of the Add Service Reference dialog box, type
http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl, and then
click Go.

3. In the Namespace box of the Add Service Reference dialog box, type GeocodeService, and then
click OK.

4. In the Solution Explorer pane, under LocationCheckerService.svc, click


LocationCheckerService.svc.cs.

5. In the LocationCheckerService.svc.cs code window, locate the following code.

using System.Text;

6. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using LocationCheckerWebRole.GeocodeService;
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L13-5

7. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 4: Write the Location Checker service.


1. In the LocationCheckerService.svc.cs code window, locate the following code, select the code, and
then press Delete.

public string GetData(int value)


{
return string.Format("You entered: {0}", value);
}

2. In the LocationCheckerService.svc.cs code window, locate the following code, select the code, and
then press Delete.

public CompositeType GetDataUsingDataContract(CompositeType composite)


{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}

3. Place the mouse cursor in the LocationCheckService class code block, and then type the following
code.

public string GetLocation(string address)


{
}

4. In the GetLocation method you just created, type the following code.

string results = "";

5. On the taskbar, click the Internet Explorer icon.

6. In the Key/URL section of the Create Key - Bing Maps Account Center page, select the key
corresponding to Photo Sharing Application, right-click the key, and then click Copy.

7. Switch to the PhotoSharingApplication - Microsoft Visual Studio window.

8. Place the mouse cursor at the end of the results variable, press Enter, and then type the following
code.

string key = "";

9. Place the mouse cursor within the key variable, and then, on the EDIT menu of the
PhotoSharingApplication - Microsoft Visual Studio window, click Paste.

10. On the taskbar, click the File Explorer icon.

11. In the PhotoSharingApplication window, navigate to Allfiles (D):\Labfiles\Mod13\Service Code,


and then double-click Geocoding Code.txt.

12. On the Edit menu of the Geocoding Code.txt - Notepad window, click Select All.

13. On the Edit menu of the Geocoding Code.txt- Notepad window, click Copy.
MCT USE ONLY. STUDENT USE PROHIBITED
L13-6 Developing ASP.NET MVC 4 Web Applications

14. Switch to the PhotoSharingApplication - Microsoft Visual Studio window.

15. Place the mouse cursor at the end of the key variable, and then press Enter.

16. On the EDIT menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Paste.

17. Place the mouse cursor at the end of the code you just pasted, press Enter, and then type the
following code.

return results;

18. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 5: Publish the Service in Windows Azure.


1. In the Solution Explorer pane, collapse LocationCheckerWebRole, right-click LocationChecker, and
then click Package.

2. In the Service Configuration box of the Package Windows Azure Application dialog box, ensure
that Cloud is selected, and then, in the Build Configuration box, click Debug, and then click
Package.

Note: Visual Studio creates the .cspkg file and the .cscfg file for the service and displays
them in the File Explorer window.

3. On the taskbar, click the Internet Explorer icon.

4. In the Internet Explorer window, click the New Tab button.


5. In the Address bar of the Internet Explorer window, type www.windowsazure.com, and then click
Go to.

6. On the Home page of the Windows Azure: Microsoft’s Cloud Platform | Cloud Hosting | Cloud
Services window, click the Portal link.

7. In the left pane of the Windows Azure window, click CLOUD SERVICES.

8. In the lower pane of the Cloud services – Windows Azure window, click NEW, point to CLOUD
SERVICE, and then click CUSTOM CREATE.

9. In the URL box of the Create a cloud service dialog box, type <Your Windows Live Account
Name>LocationService, in the REGION/AFFINITY GROUP box, click <Choose a location near you>,
and then click Complete.

10. In the NAME column of the Cloud services – Windows Azure window, click <Your Live Account
Name>LocationService.

11. On the <Your Live Account Name>locationservice page, click DASHBOARD, click STAGING, and
then click UPLOAD A NEW STAGING DEPLOYMENT.

12. In the DEPLOYMENT NAME box of the Upload a package. dialog box, type LocationChecker, and
then, in the PACKAGE box, click FROM LOCAL.

13. In the Choose File to Upload dialog box, navigate to Allfiles


(D):\Labfiles\Mod13\Starter\PhotoSharingApplication\LocationChecker\bin\Debug\app.publi
sh, click LocationChecker.cspkg, and then click Open.

14. In the CONFIGURATION box of the Upload a package. dialog box, click FROM LOCAL.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L13-7

15. In the Choose File to Upload dialog box, click ServiceConfiguration.Cloud.cscfg, and then click
Open.

16. In the Upload a package. dialog box, select the Deploy even if one or more roles contain a single
instance check box, and then click Complete.

Note: It may take several minutes to fully create and deploy the service. Ensure the process
is complete before you proceed to Exercise 3.

Results: After completing this exercise, you will be able to create a WCF cloud service. You will also be
able to publish the WCF cloud service in Windows Azure.
MCT USE ONLY. STUDENT USE PROHIBITED
L13-8 Developing ASP.NET MVC 4 Web Applications

Exercise 3: Calling a Web Service from Controller Action


 Task 1: Add a service reference to the Photo Sharing application.
1. On the taskbar, click the Microsoft Visual Studio icon.

2. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
collapse LocationChecker, and then under PhotoSharingApplication, right-click References, and
then click Add Service Reference.

3. On the taskbar, point to the Internet Explorer icon, and then click the Cloud services – Windows
Azure window.

4. In the SITE URL section of the Cloud services – Windows Azure window, click the link provided.

5. In the result window, click the LocationCheckerService.svc link.

6. In the result window, select the URL corresponding to svcutil.exe, right-click the URL, and then click
Copy.

7. Switch to Microsoft Visual Studio.

8. In the Address box of the Add Service Reference dialog box, right-click anywhere, and then click
Paste.

9. In the Add Service Reference dialog box, click Go.

10. In the Namespace box of the Add Service Reference dialog box, type GeocodeService, and then
click OK.

11. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand Controllers, and then click PhotoController.cs.
12. In the PhotoController.cs code window, locate the following code.

using PhotoSharingApplication.Models;

13. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using PhotoSharingApplication.GeocodeService;

14. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 2: Call the WCF service from the photo create action.
1. In the PhotoController.cs code window, place the mouse cursor at the end of the PhotoController
class but outside any action code block, and then type the following code.

private string CheckLocation(string location)


{
}

2. In the CheckLocation method code block, type the following code.

LocationCheckerServiceClient client = null;

3. Place the mouse cursor at the end of the client variable code block, press Enter, and then type the
following code.

string response = "";


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L13-9

4. Place the mouse cursor at the end of the response variable code block, press Enter, and then type the
following code.

try
{
}
catch (Exception e)
{
}

5. In the try code block, type the following code.

client = new LocationCheckerServiceClient();

6. Place the mouse cursor at the end of the client variable you just entered, press Enter, and then type
the following code.

response = client.GetLocation(location);

7. In the catch code block, type the following code.

response = "Error: " + e.Message;

8. Place the mouse cursor after the catch code block but in the CheckLocation method, press Enter,
and then type the following code.

return response;

9. In the PhotoController.cs code window, locate the following code.

[Authorize]
[HttpPost]
public ActionResult Create(Photo photo, HttpPostedFileBase image)
{

10. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

if (photo.Location != "")
{
}

11. In the if code block, type the following code.

string stringLongLat = CheckLocation(photo.Location);

12. Place the mouse cursor at the end of the code you just entered, press Enter, and then type the
following code.

if (stringLongLat.StartsWith("Success"))
{
}

13. In the if statement code block you just created, type the following code.

char[] splitChars = {':'};

14. Place the mouse cursor at the end of the code you just entered, press Enter, and then type the
following code.
MCT USE ONLY. STUDENT USE PROHIBITED
L13-10 Developing ASP.NET MVC 4 Web Applications

string[] coordinates = stringLongLat.Split(splitChars);

15. Place the mouse cursor at the end of the code you just entered, press Enter, and then type the
following code.

photo.Latitude = coordinates[1];
photo.Longitude = coordinates[2];

Note: Remember that the array is zero-based.

16. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 3: Configure the Services Database Connection String


1. On the taskbar, click the Internet Explorer icon.

2. In the Windows Azure Portal, on the left, click SQL Databases.


3. In the list of databases, click PhotoSharingAppServices.

4. Under quick glance, click Show connection strings.

5. Select all the text in the ADO.NET textbox.


6. Right-click the selected code, and then click Copy.

7. On the taskbar, click the PhotoSharingApplication - Microsoft Visual Studio icon.

8. In the Solution Explorer pane, click web.config.


9. In the Web.config code window, select the following code, which is the connection string value for
the AzureAppServices connection string.

Server=tcp:{Your Azure Services DB URL},1433;Database=PhotoSharingAppServices;User


ID={Your ID Here};Password=Pa$$w0rd;Trusted_Connection=False;Encrypt=True;Connection
Timeout=30;

10. Right-click the selected code, and then click Paste.

11. In the pasted content, locate the text {your_password_here} and then replace it with Pa$$w0rd.
12. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 4: Test the Location Checker service.


1. In the Solution Explorer pane, right-click PhotoSharingApplication, and then click Set as StartUp
Project.

2. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Start Debugging.

Note: If you get error while debugging, reopen the PhotoSharingApplication.sln file, and
run the web application in debugging mode.

3. On the Welcome to Adventure Works Photo Sharing page, click Log In.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L13-11

4. In the User name box of the Login page, type David Johnson, in the Password box, type
Pa$$w0rd2, and then click Log In.

5. On the Welcome to Adventure Works Photo Sharing page, click Add a Photo.

6. In the Title box of the Create New Photo page, type Testing Locations, and then, in the Picture box,
click Browse.

7. In the Choose File to Upload dialog box, navigate to Allfiles (D):\Labfiles\Mod13\Sample Photos,
click Beach.jpg, and then click Open.

8. In the Location box of the Create New Photo page, type Florence, OR, and then click Create.
9. On the All Photos page, click the Display link corresponding to the "Testing Locations" photo.

10. On the Testing Locations page, note that the Latitude and Longitude values are displayed.

Note: The latitude and longitude values are taken from the Location Checker WCF service.

11. In the Internet Explorer window, click the Close button.

12. In the PhotoSharingApplication – Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to add a service reference for a Windows Azure
WCF service to a .NET Framework application. You will also be able to call a WCF service from an MVC
controller action.
MCT USE ONLY. STUDENT USE PROHIBITED
L13-12 Developing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L14-1

Module14: Implementing Web APIs in ASP.NET MVC 4 Web


Applications
Lab: Implementing APIs in ASP.NET MVC 4
Web Applications
Exercise 1: Adding a Web API to the Photo Sharing Application
 Task 1: Add a Photo API controller.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the File Explorer icon.


6. In the Libraries window, navigate to Allfiles
(D):\Labfiles\Mod14\Starter\PhotoSharingApplication, and then double-click
PhotoSharingApplication.sln.
7. In the Solution Explorer pane of the PhotoSharingApplication – Microsoft Visual Studio window,
expand PhotoSharingApplication, right-click Controllers, point to Add, and then click Controller.

8. In the Controller name box of the Add Controller dialog box, type PhotoApiController.
9. In the Template list, click Empty API controller, and then click Add.

10. In the PhotoApiController.cs code window, locate the following code.

using System.Web.Http;

11. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using PhotoSharingApplication.Models;

12. Place the mouse cursor within the PhotoApiController class code block, and then type the following
code.

private IPhotoSharingContext context = new PhotoSharingContext();

13. Place the mouse cursor at the end of the code you just added, press Enter twice, and then type the
following code.

public IEnumerable<Photo> GetAllPhotos()


{
}

14. Place the mouse cursor within the GetAllPhotos action code block you just created, and then type
the following code.

return context.Photos.AsEnumerable();
MCT USE ONLY. STUDENT USE PROHIBITED
L14-2 Developing ASP.NET MVC 4 Web Applications

15. Place the mouse cursor in the PhotoApiController class, but outside any action code block, and then
type the following code.

public Photo GetPhotoById(int id)


{
}

16. Place the mouse cursor within the GetPhotoById action code block you just created, and then type
the following code.

Photo photo = context.FindPhotoById(id);

17. Place the mouse cursor at the end of the code you just added, press Enter, and then type the
following code.

if (photo == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}

18. Place the mouse cursor at the end of the if statement code block you just created, press Enter, and
then type the following code.

return photo;

19. Place the mouse cursor in the PhotoApiController class, but outside any action code block, and then
type the following code.

public Photo GetPhotoByTitle(string title)


{
}

20. Place the mouse cursor within the GetPhotoByTitle action code block you just created, and then
type the following code.

Photo photo = context.FindPhotoByTitle(title);

21. Place the mouse cursor at the end of the code you just added, press Enter, and then type the
following code.

if (photo == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}

22. Place the mouse cursor at the end of the if statement code block you just created, press Enter, and
then type the following code.

return photo;

23. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 2: Configure API routes.


1. In the Solution Explorer pane, expand App_Start, and then click WebApiConfig.cs.

2. In the WebApiConfig.cs code window, locate the following code, select the code, and then press
Delete.
MCT USE ONLY. STUDENT USE PROHIBITED
L14-3

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

3. Place the cursor within the Register method code block, and then type the following code.

config.Routes.MapHttpRoute(
name: "PhotoApi",
routeTemplate: "api/photos/{id}",
defaults: new { controller = "PhotoApi", action = "GetPhotoById" },
constraints: new { id = "[0-9]+" }
);

4. Place the mouse cursor at the end of the PhotoApi route code block you just added, press Enter
twice, and then type the following code.

config.Routes.MapHttpRoute(
name: "PhotoTitleApi",
routeTemplate: "api/photos/{title}",
defaults: new { controller = "PhotoApi", action = "GetPhotoByTitle" }
);

5. Place the mouse cursor at the end of the PhotoTitleApi route code block you just added, press Enter
twice, and then type the following code.

config.Routes.MapHttpRoute(
name: "PhotosApi",
routeTemplate: "api/photos",
defaults: new { controller = "PhotoApi", action = "GetAllPhotos" }
);

6. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 3: Configure media-type formatters.


1. In the WebApiConfig.cs code window, place the mouse cursor at the end of the PhotosApi route
code block, press Enter twice, and then type the following code.

var json = config.Formatters.JsonFormatter;

2. Place the mouse cursor at the end of the json variable code block you just added, press Enter, and
then type the following code.

json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;

3. Place the mouse cursor at the end the code you just added, press Enter, and then type the following
code.

config.Formatters.Remove(config.Formatters.XmlFormatter);

4. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 4: Test the web API with Internet Explorer.


1. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Start Debugging.
MCT USE ONLY. STUDENT USE PROHIBITED
L14-4 Developing ASP.NET MVC 4 Web Applications

Note: The Welcome to Adventure Works Photo Sharing page is displayed.

2. In the Address bar of the Internet Explorer window, type


http://localhost:<yourportnumber>/api/photos/4, and then press Enter.

3. In the lower-end of the Welcome to Adventure Works Photo Sharing page, a message stating “Do
you want to open or save 4.json (712 KB) from localhost?” is displayed. Click Open to view the file.

4. In the 4.json code window, note that value for the Title property is Sample Photo 4.

5. In the 4.json – Microsoft Visual Studio window, click the Close button.

6. In the Address bar of the Internet Explorer window, type


http://localhost:<yourportnumber>/api/photos/sample photo 5, and then press Enter.

7. In the lower-end of the Welcome to Adventure Works Photo Sharing page, a message stating “Do
you want to open or save sample photo 5.json (750 KB) from localhost?” is displayed. Click Open to
view the file.

8. In the sample photo 5.json code window, note that the Title property is Sample Photo 5.

9. In the sample photo 5.json – Microsoft Visual Studio window, click the Close button.
10. In the Address bar of the Internet Explorer window, type
http://localhost:<yourportnumber>/api/photos, and then press Enter.

11. In the lower-end of the Welcome to Adventure Works Photo Sharing page, a message stating “Do
you want to open or save photos.json (15.0 MB) from localhost?” is displayed. Click Open to view the
file.

12. On the EDIT menu of the photos.json – Microsoft Visual Studio window, point to Find and
Replace, and then click Quick Find.

13. In the Search Term box of the Quick Find dialog box, type Sample Photo 9, click the Find Next
button..

14. In the Microsoft Visual Studio dialog box, click OK.

15. In the Quick Find dialog box, click the Close button.

Note: In the photos.json code window, note that Visual Studio locates the text Sample
Photo 9. This implies that the API has included this photo in the returned photos.

16. On the EDIT menu of the photos.json – Microsoft Visual Studio window, click Find and Replace,
and then click Quick Find.

17. In the Search Term box of the Quick Find dialog box, type Sample Photo 13, click the Find Next
button.
18. In the Microsoft Visual Studio dialog box, click OK.

19. In the Quick Find dialog box, click the Close button

Note: In the photos.json code window, note that Visual Studio locates the text Sample
Photo 13. This implies that the API has included this photo in the returned photos.

20. In the photos.json – Microsoft Visual Studio window, click the Close button.

21. In the Internet Explorer window, click the Close button.


MCT USE ONLY. STUDENT USE PROHIBITED
L14-5

Results: At the end of this exercise, you will be able to create a simple web API for an ASP.NET MVC 4
web application.
MCT USE ONLY. STUDENT USE PROHIBITED
L14-6 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Using the Web API for a Bing Maps Display


 Task 1: Create a new template view.
1. In the Solution Explorer pane, expand Views, and then expand Shared.

2. In the Solution Explorer pane, under Shared, right-click _MainLayout.cshtml, and then click Copy.

3. In the Solution Explorer pane, right-click Shared, and then click Paste.
4. In the Solution Explorer pane, under Shared, right-click Copy of _MainLayout.cshtml, and then click
Rename.

5. In the Solution Explorer pane, rename Copy of _MainLayout.cshtml as _MapLayout.cshtml, and


then press Enter.

6. In the Solution Explorer pane, click _MapLayout.cshtml.

7. In the _MapLayout.cshtml code window, locate the following code.

<!DOCTYPE html>

8. Replace the located code with the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

9. In the _MapLayout.cshtml code window, locate the following code.

<body>

10. Replace the located code with the following code.

<body onload="GetMap();">

11. In the _MapLayout.cshtml code window, locate the following code, select the code, and then press
Delete.

<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.0/jquery-ui.min.js"
type="text/javascript"></script>

12. In the _MapLayout.cshtml code window, locate the following code, select the code, and then press
Delete.

<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.js"
type="text/javascript"></script>

13. In the _MapLayout.cshtml code window, locate the following code.

</head>

14. Place the mouse cursor before the located code, type the following code, and then press Enter.

<script charset="UTF-8" type="text/javascript"


src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>

15. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 2: Create a map action, view, and script file.


1. In the Solution Explorer pane, under Controllers, click PhotoController.cs.
MCT USE ONLY. STUDENT USE PROHIBITED
L14-7

2. In the PhotoController.cs code window, place the mouse cursor in the PhotoController class code
block, but outside any action code block, press Enter, and then type the following code.

public ViewResult Map()


{
}

3. In the Map action code block you just created, type the following code.

return View("Map");

4. In the PhotoController.cs code window, locate the following code, right-click the code, and then click
Add View.

public ViewResult Map()

5. In the View name box of the Add View dialog box, ensure that the text is Map, and then ensure that
the Create a Strongly-Typed View check box is cleared.

6. In the Add View dialog box, ensure that the Use a layout or master page check box is selected, and
then click the Browse button.

7. In the Contents of the folder box of the Select a Layout Page dialog box, click
_MapLayout.cshtml, and then click OK.

8. In the Add View dialog box, click Add.


9. In the Map.cshtml code window, locate the following code, select the code, and then press Delete.

<h2>Map</h2>

10. In the Map.cshtml code window, place the mouse cursor outside the Razor code block, press Enter,
and then type the following code.

<script type="text/javascript">
</script>

11. Place the mouse cursor in the SCRIPT element, and then type the following code.

var webApiUrl = '@Url.Content("~/api/photos")';

12. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

var pictureUrl = '@Url.Action("GetImage", "Photo")/';

13. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

var displayUrl = '@Url.Action("Display", "Photo")/';

14. Place the mouse cursor at the end of the </script> tag, press Enter twice, and then type the
following code.

<script src="" type="text/javascript"></script>

15. Place the mouse cursor in the empty src attribute you just created, and then type the following code.

@Url.Content("~/Scripts/MapDisplay.js")
MCT USE ONLY. STUDENT USE PROHIBITED
L14-8 Developing ASP.NET MVC 4 Web Applications

16. Place the mouse cursor at the end of the JavaScript code you just created, press Enter twice, and then
type the following code.

<div id="mapDiv" style="position: absolute; width: 650px; height: 400px;"></div>

17. In the Solution Explorer pane, right-click Scripts, point to Add, and then click Existing Item.

18. In the Add Existing Item - PhotoSharingApplication dialog box, navigate to Allfiles
(D):\Labfiles\Mod14\Bing Maps Script, click MapDisplay.js and then click Add.

19. In the Solution Explorer pane, click Mvc.sitemap.

20. In the Mvc.sitemap code window, locate the following code.

<mvcSiteMapNode title="Add a Photo" visibility="*" controller="Photo" action="Create"


/>

21. Place the mouse cursor before the located code, type the following code, and then press Enter.

<mvcSiteMapNode title="Map" visibility="*" controller="Photo" action="Map" />

22. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

23. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

24. On the Welcome to Adventure Works Photo Sharing page, click Map.

Note: On the Map page, the Bing Map AJAX control is displayed. However, there are no
push pins on the map.

25. On the Internet Explorer window, click the Close button.

 Task 3: Obtain and display photos.


1. In the Solution Explorer pane, under Scripts, click MapDisplay.js.

2. In the MapDisplay.js code window, place the mouse cursor at the end of the GetMap function code
block, press Enter twice, and then type the following code.

function GetPhotos(serviceUrl) {
}

3. In the GetPhotos function code block, type the following code.

$.support.cors = true;

4. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

$.ajax({
url: serviceUrl,
type: 'GET',
dataType: 'json',
success: DisplayPics,
error: OnError
});
MCT USE ONLY. STUDENT USE PROHIBITED
L14-9

5. Place the mouse cursor at the end of the GetPhotos function code block, press Enter twice, and then
type the following code.

function DisplayPics(response) {
}

6. In the DisplayPics function code block, type the following code.

var location;
var pin;

7. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

$.each(response, function(index, photo) {


});

8. In the $.each function code block, type the following code.

location = new Microsoft.Maps.Location(photo.Latitude, photo.Longitude);

9. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

pin = new Microsoft.Maps.PushPin(location);

10. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

pin.Title = photo.Title;
pin.ID = photo.PhotoID;

11. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

Microsoft.Maps.Events.addHandler(pin, 'click', DisplayInfoBox);

12. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

dataLayer.push(pin);

13. Place the mouse cursor at the end of the DisplayInfoBox function code block, press Enter twice, and
then type the following code.

function OnError(response) {
}

14. In the OnError function code block, type the following code.

alert("Could not obtain the picture coordinates");

15. In the GetMap function code block, locate the following code.

infoboxLayer.push(infoBox);

16. Place the mouse cursor at the end of the located code, press Enter twice, and then type the following
code.
MCT USE ONLY. STUDENT USE PROHIBITED
L14-10 Developing ASP.NET MVC 4 Web Applications

GetPhotos(webApiUrl);

17. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 4: Test the Bing Maps control.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, click Map.

Note: On the Map of All Photos page, if the message The specified credentials are
invalid. You can sign up for a free developer account at http://www.bingmapsportal.com
is displayed, then click the http://www.bingmapsportal.com link, and then log on to the page
by using your Windows Live credentials. Then, in the PhotoSharingApplication - Microsoft
Visual Studio window, you should re-start the web application in debugging mode and navigate
to the Map page.
Note: On the Map of All Photos page, the Bing Map AJAX control is displayed along with
push pins on the map.

3. On the Map page, click a pin of your choice.

Note: The info box appears, displaying the photo as a thumbnail with the title.

4. In the info box of the Map page, click the thumbnail.

Note: The web application redirects you to the Display view, which displays the full size
photo.

5. In the Internet Explorer window, click the Close button.


6. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to create a template view to display a Bing Map
AJAX control, and create a view and script file to display a Bing Map. You will also use jQuery to call a web
API and obtain details of photos. You will then mash up the data from a web API with Bing Maps data.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L15-1

Module15: Handling Requests in ASP.NET MVC 4 Web


Applications
Lab: Handling Requests in ASP.NET MVC 4
Web Applications
Exercise 1: Creating a SignalR Hub
 Task 1: Install SignalR.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.

4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the File Explorer icon.


6. In the Libraries window, navigate to Allfiles
(D):\Labfiles\Mod15\Starter\PhotoSharingApplication, and then double-click
PhotoSharingApplication.sln.
7. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
right-click Solution ‘PhotoSharingApplication’, and then click Manage NuGet Packages for
Solutions.
8. In the navigation pane of the PhotoSharingApplication.sln - Manage NuGet Packages dialog box,
under Online, click NuGet official package source.

9. In the PhotoSharingApplication.sln - Manage NuGet Packages dialog box, click inside the Stable
Only box, and then click Include Prerelease.

10. In the Search Online box of the PhotoSharingApplication.sln - Manage NuGet Packages dialog
box, type Microsoft.aspnet.signalr, and then press Enter.

11. In the list of packages, click Microsoft ASP.NET SignalR, and then click Install.

12. If the Select Projects dialog box appears, clear all projects except PhotoSharingApplication, and
then click OK.

13. In the License Acceptance dialog, click I Accept.

When the installation is complete, click Close.

14. In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window,
expand PhotoSharingApplication, and then expand References.

Note: The NuGet package has added three Microsoft.aspnet.SignalR namespaces to the
project references.

15. In the Solution Explorer pane, under PhotoSharingApplication, expand Scripts.


MCT USE ONLY. STUDENT USE PROHIBITED
L15-2 Developing ASP.NET MVC 4 Web Applications

Note: The NuGet package has added five jquery scripts, including two signalR scripts, to
the Scripts folder.

 Task 2: Create a Hub class.


1. In the Solution Explorer pane, right-click PhotoSharingApplication, point to Add, and then click
Class.

2. In the Name box of the Add New Item - PhotoSharingApplication dialog box, type ChatHub.cs,
and then click Add.

3. In the ChatHub.cs code window, locate the following code, select the code, and then press Delete.

using System.Collections.Generic;
using System.Linq;

4. In the ChatHub.cs code window, locate the following code.

using System.Web;

5. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

6. In the ChatHub.cs code window, locate the following code.

public class ChatHub

7. In the ChatHub.cs code window, replace the located code with the following code.

public class ChatHub : Hub

8. In the ChatHub class code block, type the following code.

public Task Join(int photoId)


{
}

9. In the Join method code block, type the following code.

return Groups.Add(Context.ConnectionId, "Photo" + photoId);

10. Place the mouse cursor at the end of the Join method code block, press Enter twice, and then type
the following code.

public Task Send(string username, int photoId, string message)


{
}

11. In the Send method code block, type the following code.

string groupName = "Photo" + photoId;

12. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

return Clients.Group(groupName).addMessage(username, message);


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L15-3

13. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 3: Configure SignalR routes.


1. In the Solution Explorer pane, click Global.asax.

2. In the Global.asax code window, locate the following code.

using System.Data.Entity;

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

using Microsoft.AspNet.SignalR;

4. In the Global.asax code window, locate the following code.

AreaRegistration.RegisterAllAreas();

5. Place the mouse cursor at the end of the located code, press Enter twice, and then type the following
code.

RouteTable.Routes.MapHubs();

6. On the FILE menu of the PhotSharingApplication - Microsoft Visual Studio window, click Save
All.

Results: After completing this exercise, you will be able to install SignalR in an MVC 4 web application,
configure routes for SignalR, and create a hub to receive and forward simple text messages.
MCT USE ONLY. STUDENT USE PROHIBITED
L15-4 Developing ASP.NET MVC 4 Web Applications

Exercise 2: Creating a Photo Chat View


 Task 1: Create a chat action and view.
1. In the Solution Explore pane, expand Controllers, and then click PhotoController.cs.

2. In the PhotoController.cs code window, place the mouse cursor in the PhotoController class, but
outside any action method code block, and then type the following code.

[Authorize]
public ActionResult Chat(int id)
{
}

3. Place the mouse cursor within the Chat action code block, type the following code, and then press
Enter.

Photo photo = context.FindPhotoById(id);

4. In the Chat action code block, place the mouse cursor at the end of the code you just added, type the
following code, and then press Enter.

if (photo == null) {
return HttpNotFound();
}

5. In the Chat action code block, place the mouse cursor at the end of the code you just added, type the
following code, and then press Enter.

return View("Chat", photo);

6. In the Solution Explorer pane, expand Views, right-click Photo, point to Add, and then click Existing
Item.

7. In the Add Existing Item – PhotoSharingApplication dialog box, navigate to Allfiles


(D):\labfiles\Mod15\Chat View\Chat.cshtml, click Chat.cshtml, and then click Add.

8. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save
All.

 Task 2: Link to the chat view.


1. In the Solution Explorer pane, under Photo, click Display.cshtml.

2. In the Display.cshtml code window, locate the following code.

<div id="addtofavorites">
@Ajax.ActionLink("Add this photo to your favorites",
"AddFavorite",
"Photo",
new { PhotoID = Model.PhotoID },
new AjaxOptions {
UpdateTargetId = "addtofavorites",
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace
})
</div>

3. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

<div id="chataboutthisphoto">
</div>
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L15-5

4. Place the mouse cursor within the DIV element code block you just added, and then type the
following code.

@Html.ActionLink("Chat about this photo", "Chat", new { id = Model.PhotoID })

5. On the DEBUG menu of the PhotoSharingApplication – Microsoft Visual Studio window, click
Start Debugging.

6. On the Welcome to Adventure Works Photo Sharing page, in the Latest Photos section, click the
Display link corresponding to the image of your choice.

7. On the Sample Photo page, click the Chat about this photo link, and then note that the login page
appears.

8. In the User name box of the Login page, type David Johnson.

9. In the Password box, type Pa$$w0rd2, and then click Log in.

Note: If an exception appears when you log on, you need to replace the connection string
in the web.config file from the PhotoAppServices project. Also, in the connection string, you need
to replace {your_passwor_here} as Pa$$w0rd.

10. On the Chat page, in the message box, type Test message, and then click Send.

Note: The message is not sent because the SignalR scripts are not yet written.

11. In the Internet Explorer window, click the Close button.

 Task 3: Link to JScript files.


1. In the Solution Explorer pane, under Photo, click Chat.cshtml.

2. In the Chat.cshtml code window, place the mouse cursor at the end of the view file, press Enter, and
then type the following code.

<script type="text/javascript">
</script>

3. Place the mouse cursor in the SCRIPT element code block you just added, type the following code,
and then press Enter.

var username = '@User.Identity.Name';

4. In the SCRIPT element, place the mouse cursor at the end of the username variable, press Enter, and
then type the following code.

var photoid = @Model.PhotoID

5. Place the mouse cursor at the end of the SCRIPT element code block, press Enter, and then type the
following code.

<script type="text/javascript" src=""></script>

6. Place the mouse cursor within the src attribute, and then type the following code.

@Url.Content("~/Scripts/jquery.signalR-1.0.0.js")
MCT USE ONLY. STUDENT USE PROHIBITED
L15-6 Developing ASP.NET MVC 4 Web Applications

7. Place the mouse cursor at the end of the SCRIPT element code block that you just added, press Enter,
and then type the following code.

<script type="text/javascript" src=""></script>

8. Place the mouse cursor within the src attribute, and then type the following code.

@Url.Content("~/signalr/hubs")

9. Place the mouse cursor at the end of the SCRIPT element code block that you just added, press Enter,
and then type the following code.

<script type="text/javascript" src=""></script>

10. Place the mouse cursor within the src attribute, and then type the following code.

@Url.Content("~/Scripts/ChatRoom.js")

11. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 4: Script SignalR connections.


1. In the Solution Explorer pane, right-click Scripts, point to Add, and then click New Item.

2. In the Search Installed Templates box of the Add New Item – PhotoSharingApplication dialog
box, type JavaScript, and then press Enter.
3. In the Add New Item – PhotoSharingApplication dialog box, click JavaScript File and the click
Add.

4. In the Solution Explorer pane, under Scripts, right-click JavaScript1.js, and then click Rename.
5. In the Solution Explorer pane, under Scripts, rename JavaScript1.js with ChatRoom, and then press
Enter.

6. In the ChatRoom.js code window, type the following code.

$(function() {
});

7. Place the mouse cursor within the anonymous function code block you just added, and then type the
following code.

var chat = $.connection.chatHub;

8. Place the mouse cursor at the end of the chat variable code block, press Enter, and then type the
following code.

$('#chat-message').focus();

9. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

$.connection.hub.start().done(function(){
});

10. Place the mouse cursor within the anonymous function you just created, and then type the following
code.
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L15-7

chat.server.join(photoid).done(function() {
});

11. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.

 Task 5: Script SignalR messages.


1. In the ChatRoom.js code window, locate the following code.

var chat = $.connection.chatHub;

2. Place the mouse cursor at the end of the located code, press Enter, and then type the following code.

chat.client.addMessage = function (name, message) {


};

3. Place the mouse cursor within the anonymous function code block you just created, and then type
the following code.

var encodedName = $('<div />').text(name).html();

4. Place the mouse cursor at the end of the encodedName variable code block, press Enter, and then
type the following code.

var encodedMessage = $('<div />').text(message).html();

5. Place the mouse cursor at the end of the encodedMessage variable code block, press Enter, and
then type the following code.

var listItem = '<li>' + encodedName + ': ' + encodedMessage + '</li>';

6. Place the mouse cursor at the end of the listItem variable code block, press Enter, and then type the
following code.

$('#discussion').append(listItem);

7. In the ChatRoom.js code window, locate the following code.

chat.server.join(photoid).done(function() {
});

8. Place the mouse cursor within the located code, and then type the following code.

$('#sendmessage').click(function() {
});

9. Place the mouse cursor within the anonymous function you just created, and then type the following
code.

chat.server.send(username, photoid, $('#chat-message').val());

10. Place the mouse cursor at the end of the code you just typed, press Enter, and then type the
following code.

$('#chat-message').val('').focus();

11. On the FILE menu of the PhotoSharingApplication - Microsoft Visual Studio window, click Save
All.
MCT USE ONLY. STUDENT USE PROHIBITED
L15-8 Developing ASP.NET MVC 4 Web Applications

 Task 6: Test the chat room.


1. On the DEBUG menu of the PhotoSharingApplication - Microsoft Visual Studio window, click
Start Debugging.

2. On the Welcome to Adventure Works Photo Sharing page, click the Log In link.

3. In the User name box of the Login page, type David Johnson.

4. In the Password box, type Pa$$w0rd2, and then click Log in.
5. On the Welcome to Adventure Works Photo Sharing page, click the Display link corresponding to
Sample Photo 1

6. On the Sample Photo 1 page, click the Chat about this photo link.

7. On the Chat page, in the message box, type Test message, and then click Send.

Note: The script sends the message to the hub that returns the message to all browsers,
currently connected to the group of this photo.

8. On the taskbar, right-click the Internet Explorer icon, and then click Internet Explorer.

9. In the Address bar of the Internet Explorer window, type http://localhost:<portnumber>/, and then
press Enter.

10. On the Welcome to Adventure Works Photo Sharing page, click the Log off link.

11. On the Welcome to Adventure Works Photo Sharing page, click the Register link.
12. In the User name box of the Register page, type Mark Steele, in the Password box, type Pa$$w0rd.

13. In the Confirm password box, type Pa$$w0rd, and then click Register.

14. On the Welcome to Adventure Works Photo Sharing page, click the Display link corresponding to
Sample Photo 1.

15. On the Sample Photo 1 page, click the Chat about this photo link.

16. On the Chat page, in the message box, type Second test message, and then click Send.

Note: On the Welcome to Adventure Works Photo Sharing page, if the user name shown is
David Johnson and not Mark Steele, click the Refresh button before proceeding with the steps.

17. On the taskbar, point to Internet Explorer icon, and then click the David Johnson Chat – Windows
Internet Explorer icon.

Note: The new message from Mark Steele is displayed because both users joined the chat
for Sample Photo 1.

18. On the Chat page, in the message box, type Third test message, and then click Send.
19. On the taskbar, point to Internet Explorer icon, and then click the Mark Steele Chat - Windows
Internet Explorer.

Note: The new message is displayed in Internet Explorer.


MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L15-9

20. In the Internet Explorer windows, click the Close button.

21. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to create MVC controller actions and views to
display a user interface for SignalR functionality, link to the JScript libraries that SignalR requires, and use
JScript to connect to a SignalR hub and send messages.
MCT USE ONLY. STUDENT USE PROHIBITED
L15-10 Developing ASP.NET MVC 4 Web Applications
MCT USE ONLY. STUDENT USE PROHIBITED
Developing ASP.NET MVC 4 Web Applications L16-1

Module16: Deploying ASP.NET MVC 4 Web Applications


Lab: Deploying ASP.NET MVC 4 Web
Applications
Exercise 1: Deploying a Web Application to Windows Azure
 Task 1: Prepare the Photo Sharing application project for deployment.
1. In the result pane of the Hyper-V Manager console, in the Name list of the Virtual Machines area,
right-click 20486A-SEA-DEV11, and then click Connect.

2. To log on to 20486A-SEA-DEV11, click the Ctrl+Alt+Delete button.

3. In the User name box, type Admin, in the Password box, type Pa$$w0rd, and then click the
Forward button.
4. On the Windows 8 Start screen, click Desktop.

5. On the taskbar, click the File Explorer icon.

6. In the Libraries window, navigate to Allfiles


(D):\Labfiles\Mod16\Starter\PhotoSharingApplication, and then double-click
PhotoSharingApplication.sln

7. In the Solution Explorer pane of the PhotoSharingApplication – Microsoft Visual Studio window,
right-click PhotoSharingApplication, and then click Properties.

8. In the PhotoSharingApplication code window, ensure that the Build tab is selected.

9. In the Configuration box, click Release.

10. On the taskbar, click the Internet Explorer icon.

11. In the Address bar of the Internet Explorer window, type http://www.windowsazure.com, and click
the Go to button.

12. In the upper-right corner of the Windows Azure: Microsoft’s Cloud Platform | Cloud Hosting | Cloud
Services page, click PORTAL.

13. In the Microsoft account box of the Sign in to your Microsoft account page, type <Your Windows
Live account name>, in the Password box, type <your password>, and then click Sign in.

14. In the left pane of the Windows Azure page, click SQL Databases.

15. In the Name column, click PhotoSharingDB.

16. In the quick glance section of the photosharingdb page, click Show connection strings.

17. In the ADO.NET box of the Connection Strings dialog box, select all the text, right-click the selected
text, and then click Copy.

18. On the taskbar, click the PhotoSharingApplication – Microsoft Visual Studio icon.

19. In the Solution Explorer pane, expand PhotoSharingApplication, click Web.config.

20. In the Web.config code window, locate the following code.

<add name="PhotoSharingDB" connectionString="Server=tcp:{Your Azure Database


URL},1433;Database=PhotoSharingDB;User ID={Your ID
Here};Password=Pa$$w0rd;Trusted_Connection=False;Encrypt=True;Connection
Timeout=30;PersistSecurityInfo=true" providerName="System.Data.SqlClient" />
MCT USE ONLY. STUDENT USE PROHIBITED
L16-2 Developing ASP.NET MVC 4 Web Applications

21. In the located code, select the contents of the connectionString attribute, right-click the selected
text, and then click Paste.

22. Within the text you just pasted, select the following text.

{your_password_here}

23. Replace the selected text with the word, Pa$$w0rd.

24. On the taskbar, click the SQL Databases – Windows Azure – Windows Internet Explorer icon.

25. In the Connection Strings dialog box, click the Close button.

26. In the left pane of the Windows Azure page, click PhotoAppServices.

27. In the glance section of the photoappservices page, click Show connection strings.

28. In the ADO.NET box of the Connection Strings dialog box, select all the text, right-click the selected
text, and then click Copy.

29. On the taskbar, click the PhotoSharingApplication – Microsoft Visual Studio icon.

30. In the Web.config code window, locate the following code.

<add name="AzureAppServices" providerName="System.Data.SqlClient"


connectionString="Server=tcp:{Your Azure App Services
URL},1433;Database=PhotoSharingAppServices;User ID={Your ID
Here};Password=Pa$$w0rd;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
/>

31. In the located code, select the contents of the connectionString attribute, right-click the selected
text, and then click Paste.

32. Within the text you just pasted, select the following text.

{your_password_here}

33. Replace the selected text with the word, Pa$$w0rd.


34. On the taskbar, click the SQL Databases – Windows Azure – Windows Internet Explorer icon.

35. In the Internet Explorer window, click the New Tab button.

36. In the Address bar of the Internet Explorer window, type https://www.bingmapsportal.com, and
then click the Go to button.

37. On the Home – Bing Maps Account Center page, click the Sign In link.

38. If the Sign in to your Microsoft account page appears, in the Microsoft account box, type <Your
Windows Live account name>, in the Password box, type Pa$$w0rd, and then click Sign in.

39. In the left pane of the Create Key - Bing Maps Account Center page, under My Account, click the
Create or view keys link.
40. In the Key/URL section of the Create Key - Bing Maps Account Center page, select the key
corresponding to Photo Sharing Application, right-click the key, and then click Copy.

41. On the taskbar, click the PhotoSharingApplication – Microsoft Visual Studio icon.

42. In the Solution Explorer pane of the PhotoSharingApplication – Microsoft Visual Studio window,
under Scripts, click MapDisplay.js.

43. In the MapDisplay.js code window, locate the following code, right-click the selected code, and then
click Paste.
MCT USE ONLY. STUDENT USE PROHIBITED
L16-3

{Your Bing Key}

44. On the FILE menu of the PhotoSharingApplication – Microsoft Visual Studio window, click Save All.

 Task 2: Create a new website in Windows Azure.


1. On the taskbar, point to the Internet Explorer icon, and then click the SQL Databases – Windows
Azure – Windows Internet Explorer icon.

2. In the Connection Strings dialog box, click Close.

3. In the left pane of the Windows Azure page, click the Web Sites icon.

4. In the lower-left pane of the Windows Azure page, click New, and then click Custom Create.

5. In the URL box of the Create Web Site dialog box, type <your username>PhotoSharing.

6. In the REGION box, click <a region near you>.

7. In the DATABASE box, click PhotoSharingDB.

8. In the DB CONNECTION STRING NAME box, type PhotoSharingDB.

9. In the DB USER box, type <your first name>.


10. In the PASSWORD box, type Pa$$w0rd, and then click the Complete icon.

Note: Windows Azure creates the new web application. You need to upload the Photo
Sharing application project to this web application.

 Task 3: Deploy the Photo Sharing application.


1. On the Windows Azure page, in the Name column, click <your username>PhotoSharing, and then
click DASHBOARD.

2. In the quick glance section of the <your username>PhotoSharing page, click Download publish
profile.

3. In the Navigation bar, click Save, and then click the Close button.
4. On the taskbar, click the PhotoSharingApplication – Microsoft Visual Studio icon.

5. In the Solution Explorer pane, right-click PhotoSharingApplication, and then click Publish.

6. On the Profile page of the Publish Web wizard, in the Select or import a publish profile box, click
Import.

7. In the Import Publish Settings dialog box, click <your


username>PhotoSharing.azurewebsites.net.PublishSettings, and then click Open.

8. On the Connection page, click Validate Connection, and then click Next.

9. If the Certification Error dialog box appears, click Accept.

10. On the Settings page, in the PhotoSharingContext box, click the following option.
Data Source=tcp:{Your Azure Database URL},1433;Initial Catalog=PhotoSharingDB;
Integrated Security=False;User ID={Your ID Here};Password=Pa$$w0rd; Connection
Timeout=30; Encrypt=True

11. In the PhotoSharingDB box, click the following option.


MCT USE ONLY. STUDENT USE PROHIBITED
L16-4 Developing ASP.NET MVC 4 Web Applications

Data Source=tcp:{Your Azure Database URL},1433;Initial Catalog=PhotoSharingDB;


Integrated Security=False;User ID={Your ID Here};Password=Pa$$w0rd;Connection
Timeout=30; Encrypt=True

12. In the UsersContext (AzureAppServices) box, click the following option.

Data Source=tcp:{Your Azure Database URL},1433;Initial Catalog=PhotoSharingDB;


Integrated Security=False;User ID={Your ID Here};Password=Pa$$w0rd; Connection
Timeout=30; Encrypt=True

13. On the Settings page, click Next.

14. On the Preview page, click Start Preview.

Note: The files that require changes are displayed.

15. On the Publish page, click Publish.

Note: Visual Studio publishes the Photo Sharing web application to Windows Azure. This
process may take several minutes. When the process is complete, a new tab in Windows Internet
Explorer displays the Photo Sharing home page.

Results: After completing this exercise, you will be able to prepare an ASP.NET MVC web application for
production deployment, create a web application in Windows Azure, and deploy an MVC web application
to Windows Azure.
MCT USE ONLY. STUDENT USE PROHIBITED
L16-5

Exercise 2: Testing the Completed Application


 Task 1: Add a photo and a comment.
1. On the Welcome to Adventures Works Photo Sharing page, click the Log In link.

2. In the User name box of the Login page, type David Johnson, in the Password box, type
Pa$$w0rd2, and then click Log in.

3. On the Create New Photo page, click Add a Photo.

4. In the Title box, type Test New Photo, and then click Browse.

5. In the Choose File to Upload dialog box, navigate to Allfiles (D):\Labfiles\Mod16\Sample Photos,
click track.JPG and then click Open.

6. In the Description box, type This is the first photo added to the deployed web application.

7. In the Location box, type Devon, UK, and then click Create.

8. On the All Photos page, click the Display link corresponding to Test New Photo.

9. On the Test News Photo page, note that the Location Checker WCF service has resolved the location
you entered and added the correct latitude and longitude values.

10. In the Subject box of the Comments section, type Functionality Check.

11. In the Body box, type The location checker service has correctly resolved the coordinates, and
then click Create.

Note: The new comment added is displayed on the website.

 Task 2: Use the slideshow and favorites options.


1. On the Test New Photo page, click Slideshow, and then view a slideshow of the images.

Note: The slideshow displays all the photos in the web application and includes the faded
transition between photos and the jQueryUI progress bar widget.

2. On the Test New Photo page, click All Photos.

3. On the All Photos page, click the Display link corresponding to Sample Photo 1.

4. On the Sample Photo 1 page, click the Add this photo to your favorites link.

5. In the Internet Explorer window, click the Back button.

6. On the All Photos page, click the Display link corresponding to Sample Photo 2.

7. On the Sample Photo 2 page, click the Add this photo to your favorites link.

8. In the Internet Explorer window, click the Back button.


9. On the All Photos page, click the Display link corresponding to Sample Photo 3.

10. On the Sample Photo 3 page, click the Add this photo to your favorites link.

11. On the Sample Photo 3 page, click the Favorite Photos link, and then view a slideshow of the images
that you added to the favorites list.

 Task 3: Test the chat room.


1. On the SlideShow page, click All Photos.
MCT USE ONLY. STUDENT USE PROHIBITED
L16-6 Developing ASP.NET MVC 4 Web Applications

2. On the All Photos page, click the Display link corresponding to Sample Photo 1.

3. On the Sample Photo 1 page, click the Chat about this photo link.

4. On the Chat page, in the message box, type A test message for Sample Photo 1, and then click
Send.

Note: Your message is displayed in the conversation.

5. On the taskbar, right-click the Internet Explorer icon, and then click Internet Explorer.

6. In the Address bar, type http://<yourusername>PhotoSharing.azurewebsites.net, and then click


the Go to button.
7. On the Welcome to Adventure Works Photo Sharing page, click the Log Off link, and then click the
Refresh button.

8. On the Welcome to Adventure Works Photo Sharing page, click the Log In link.
9. In the User name box of the Login page, type Mark Steele.

10. In the Password box, type Pa$$w0rd, and then click Log In.

11. On the Welcome to Adventure Works Photo Sharing page, click All Photos.
12. On the All Photos page, click the Display link corresponding to Sample Photo 1.

13. On the Sample Photo 1 page, click the Chat about this photo link.

14. On the Chat page, in the message box, type This message is from Mark Steele, and then click Send.

15. On the taskbar, point to the Internet Explorer icon, and then click the Chat window for David
Johnson.

Note: The second message is displayed successfully

16. On the Chat page, in the message box, type This is the third test message, and then click Send.

17. On the taskbar, point to the Internet Explorer icon, and then click the Chat window for Mark Steele.

Note: The third message is displayed successfully.

18. Close all the Internet Explorer windows.

19. In the PhotoSharingApplication - Microsoft Visual Studio window, click the Close button.

Results: After completing this exercise, you will be able to confirm that all the functionalities that you
built in the Photo Sharing application run correctly in the Windows Azure deployment.

Das könnte Ihnen auch gefallen