Table Of ContentHands on With
ASP.NET
MVC
Hands on With
ASP.NET MVC
Rahul Sahay
Quills Ink Publishing
Copyright © 2014 Rahul Sahay
Hardcover ISBN : 978-93-84318-52-9
Softcover ISBN : 978-93-84318-53-6
Ebook ISBN : 978-93-84318-54-3
Design and Setting By
Quills Ink Publishing
www.quillsink.com
All rights reserved. No part of this publication may be reproduced,
distributed, or transmitted in any form or by means, including
photocopying, recording, or other electronic or mechanical methods,
without the prior written permission of the publisher.
The views and characters expressed in the book are of the author and
his/her imagination and do not represent the views of the Publisher.
Contents
Chapter 1: Getting Started
WHAT DO you find in this CHAPTER?
Introduction
Understanding ASP.NET MVC
The MVC Pattern
Brief History of MVC Pattern
MVC Life Cycle
Software Requirements
Application Glimpse
Creating a MVC application
How MVC applications are structured
Summary
Chapter 2: Understanding Controllers
WHAT DO you find in this CHAPTER?
Introduction
Controllers
Working with your 1st controller
Action Results
Action Selectors
Action Filters
Summary
Chapter 3: Views
WHAT DO you find in this CHAPTER?
Introduction
Views
Syntax of Razor Views
How to Prevent Cross Site Scripting Attack
More on Razor Syntax
Layout Views
HTML Helpers
HTML.ValidationSummary
Some more HTML Helpers
Partial Views
Viewbag and Viewdata
Summary
Chapter 4: Getting Started with Data modeling
WHAT DO you find in this CHAPTER?
Introduction
Using Entity Framework
Entities Creation
Database Migrations
LINQ
Search Query
Summary
Chapter 5: Data modeling-Part 2
WHAT DO you find in this CHAPTER?
Introduction
Organizing Movie Controller
Reviews Listing
Create and Edit Facility of Review
Validation Annotations
Summary
Chapter 6: JavaScript and AJAX in MVC
WHAT DO you find in this CHAPTER
Introduction
Managing Scripts
Ajax Helpers
Async Search
Paging
Summary
Chapter 7: Security with ASP.Net MVC
WHAT DO you find in this CHAPTER?
Introduction
Authentication
Membership and Forms Authentication
Authorize
Seeding Membership
Cross Site Vulnerability
OpenID and OAuth
Summary
Chapter 8: Caching - ASP.Net Terminology
WHAT DO you find in this CHAPTER?
Introduction
Caching
Cache Profiles
Localization
Resource Files
Diagnostics
ELMAH
Summary
Chapter 9: Unit Testing
WHAT DO you find in this CHAPTER?
Introduction
TDD
Home Controller Tests
Web Tests
Load Tests
Code Coverage
Summary
Chapter 10: Deployment
WHAT DO you find in this CHAPTER?
Introduction
Configuration Files
Hosting ASP.Net MVC Application
Deployment Preparation
Deployment on IIS
Deployment to Azure
Summary
Chapter 11: Routing Basics
WHAT DO you find in this CHAPTER?
Introduction
Route Definition
Areas
Ignore Routes
Debugging Routes
Constructing URLs from Route
Route Data
Route Constraints
Summary
Chapter 12: Web API and Dependency Injection
WHAT DO you find in this CHAPTER?
Introduction
Why Web API
Creating a new empty Project
Adding Web API to an ASP.Net Project
Creating your 1st controller
Dependency Injection
Implementing POST
Implementing DELETE
Implementing PUT
Summary
Chapter 13: Making a Mobile Site Using Jquery Mobile
WHAT DO you find in this CHAPTER?
Introduction
Setup
Viewports, Widgets and Attributes
Themes and Swatches
Media Query
Layouts and Pages
Collapsibles
Grids and Buttons
Forms
Summary
Chapter 14: ASP.NET MVC
WHAT DO you find in this CHAPTER?
Introduction
Scaffolding
ASP.Net Identity
One ASP.Net
Bootstrap
Attribute Routing
Filter Overrides
Summary
Chapter 15: ASP.NET MVC
WHAT DO you find in this CHAPTER
Introduction
Features
Prerequisites
Setting up VM
Getting Started with Visual Studio 14
Creating your 1st Visual Studio 14 MVC Project
Configurations in MVC Project
project.json
Startup class
Adding a Class Library Project
Deployment to Azure
Bin
Self Hosting
Summary
Index
Introduction
Hands-on with ASP.NET MVC book give you a fine Launchpad to get started on
MVC. I have written the book in such a fashion that you will solve one case
study while studying the book. By the time you will finish the book, you would
have completed web version and mobile version of development using MVC.
Now, one important point to understand here that ASP.NET MVC is a major
shift for all Microsoft developers. ASP.NET MVC emphasizes clean
architecture, design patterns and testability. At every part of the book you will
get practical example of all the theoretical concepts with complete demo.
ASP.NET MVC is basically a web development framework from Microsoft
which combines the features of MVC (Model-View-Controller) architecture.
Now, ASP.NET MVC is not something which is built from the ground zero. It
built on the top of ASP.NET, so developers enjoy almost all the ASP.NET
features while building the MVC application. ASP.NET MVC is an alternative
approach to an existing Web Forms approach.
This book is basically comprised of 15 chapters covering all the parts of MVC
development. 1st chapter starts with the introduction of ASP.NET MVC where in
you will see how MVC gained maturity in different releases. 1st chapter will also
showcase that what you are going to build by giving the application glimpse in
its finished form. Then, you will start creating your MVC project right from the
scratch. In 2nd Chapter you will get the complete idea of controllers. Here, after
understanding the basics of controllers, you will be writing your 1st controller.
2nd chapter also covers different types of action results, filters and many more
things. 3rd chapter will have view introduction with syntactic sugar. This section
also explains some of the core concepts of data — (dash) attributes that how
views behind the scenes emit these tags. Apart from HTML helpers, you will
also work with different types of view in it. Chapter 4 and 5 will be all about
data modeling. In this chapter you will come to know how to get started with
entity framework, also how to setup the entities and migrations. Here you will
also see different types of validations in action.
Chapter 6 is all about Client side scripts. Its main focus would be on how to
manage scripts, how to work with Ajax helpers and how to write an Async
Search. Chapter 7 is covering all the security related features like Membership,
Authentication, Roles, Open Authentication and many more things. Now,
chapter 8 is all about infrastructure topic like Caching and different Diagnostics
techniques. This topic stresses how to improve the Application performance by
implementing caching. 9th chapter is all about testing. It start with unit testing
where in you can test your application logic, then web testing and load testing
where in you can test your website performance in different test scenarios. Then
we have 10th chapter which is all about deployment. In this case you will 1st
deploy the application to IIS, then on internet on AZURE. Chapter 11 is all about
routing concepts. This chapter deals with different routing rules. Then, chapter
12 comprise of Web API and Dependency Injection. Both of these topics are
really important and interesting as well. You will learn how to implement both
with a complete application demo. In chapter 13, you will be optimizing your
application in such a way that it will support mobile browsers as well. Then,
Chapter 14 is dedicated to the new features of ASP.NET MVC 5 with complete
demo. Last but not the least which is the bonus part of this book MVC 6; here, in
chapter 15 you will start from the features discussion, then prerequisites required
for MVC 6. MVC 6 is a major breakthrough from Microsoft side towards being
Open Source Compliant. So, stay tuned for a stunning journey.
Acknowledgments This book would not
have been possible without the patient
and loving support of my wife Nivi, who
had to take over much of the household
responsibility apart from her teaching
activities, so I could whisper to myself in
the corner and do my activities with my
laptop. Many thanks also go to my
publisher, Rohan Vij, who took this
responsibility, to publish the book in a
precise and correct way.
Appreciation also goes out to my Mom and Dad for believing in me and always
keeps on encouraging me to complete the book in a best possible way. Also, I
need to acknowledge you, the reader, for your desire to continue your own
growth not only as developer but a smart developer. Obviously, your efforts to
improve your skills make me all work to improve myself to keep up the good
work. Kudos to you, and keep setting the pace at your workplace!
I would also like to Thank Sanjay for writing the FOREWORD for me. He took
his time from his day to day busy schedule, read the complete book; gave his
suggestions for improvement and finally after implementing his suggestions he
wrote foreword for me.