Professional ASP.NET MVC 5
Buy Rights Online Buy Rights

Rights Contact Login For More Details

More About This Title Professional ASP.NET MVC 5

English

ASP.NET MVC insiders cover the latest updates to the technology in this popular Wrox reference

MVC 5 is the newest update to the popular Microsoft technology that enables you to build dynamic, data-driven websites. Like previous versions, this guide shows you step-by-step techniques on using MVC to best advantage, with plenty of practical tutorials to illustrate the concepts. It covers controllers, views, and models; forms and HTML helpers; data annotation and validation; membership, authorization, and security.

  • MVC 5, the latest version of MVC, adds sophisticated features such as single page applications, mobile optimization, and adaptive rendering
  • A team of top Microsoft MVP experts, along with visionaries in the field, provide practical advice on basic and advanced MVC topics
  • Covers controllers, views, models, forms, data annotations, authorization and security, Ajax, routing, ASP.NET web API, dependency injection, unit testing, real-world application, and much more

Professional ASP.NET MVC 5 is the comprehensive resource you need to make the best use of the updated Model-View-Controller technology.

English

Jon Galloway works at Microsoft as a Technical Evangelist focused on ASP.NET MVC.

Brad Wilson has been a professional software developer for more than 20 years, with 10 years in web development.

K. Scott Allen is the founder of OdeToCode LLC, which provides custom development, consulting, and mentoring services for clients worldwide.

David Matson is a software developer at Microsoft. He is part of the MVC 5 and Web API 2 product team.

English

FOREWORD xxvii

INTRODUCTION xxix

CHAPTER 1: GETTING STARTED 1

A Quick Introduction to ASP.NET MVC 1

How ASP.NET MVC Fits in with ASP.NET 2

The MVC Pattern 2

MVC as Applied to Web Frameworks 3

The Road to MVC 5 3

MVC 4 Overview 6

Open-Source Release 10

ASP.NET MVC 5 Overview 11

One ASP.NET 11

New Web Project Experience 12

ASP.NET Identity 12

Bootstrap Templates 13

Attribute Routing 14

ASP.NET Scaffolding 14

Authentication Filters 15

Filter Overrides 15

Installing MVC 5 and Creating Applications 16

Software Requirements for ASP.NET MVC 5 16

Installing ASP.NET MVC 5 16

Creating an ASP.NET MVC 5 Application 17

The New ASP.NET Project Dialog 18

The MVC Application Structure 24

ASP.NET MVC and Conventions 27

Convention over Configuration 28

Conventions Simplify Communication 29

Summary 29

CHAPTER 2: CONTROLLERS 31

The Controller’s Role 31

A Sample Application: The MVC Music Store 34

Controller Basics 38

A Simple Example: The Home Controller 39

Writing Your First Controller 42

Parameters in Controller Actions 45

Summary 47

CHAPTER 3: VIEWS 49

The Purpose of Views 50

View Basics 50

Understanding View Conventions 54

Strongly Typed Views 55

How ViewBag Falls Short 55

Understanding ViewBag, ViewData, and ViewDataDictionary 57

View Models 58

Adding a View 60

The Razor View Engine 63

What Is Razor? 63

Code Expressions 64

HTML Encoding 66

Code Blocks 68

Razor Syntax Samples 68

Layouts 70

ViewStart 72

Specifying a Partial View 73

Summary 74

CHAPTER 4: MODELS 75

Modeling the Music Store 76

Scaffolding a Store Manager 80

What Is Scaffolding? 80

Scaffolding and the Entity Framework 82

Executing the Scaffolding Template 85

Executing the Scaffolded Code 92

Editing an Album 97

Building a Resource to Edit an Album 97

Responding to the Edit POST Request 101

Model Binding 103

The DefaultModelBinder 104

Explicit Model Binding 105

Summary 107

CHAPTER 5: FORMS AND HTML HELPERS 109

Using Forms 110

The Action and the Method 110

To GET or to POST? 111

HTML Helpers 114

Automatic Encoding 115

Making Helpers Do Your Bidding 115

Inside HTML Helpers 116

Setting Up the Album Edit Form 117

Adding Inputs 118

Helpers, Models, and View Data 124

Strongly Typed Helpers 126

Helpers and Model Metadata 127

Templated Helpers 127

Helpers and ModelState 128

Other Input Helpers 129

Html.Hidden 129

Html.Password 129

Html.RadioButton 129

Html.CheckBox 130

Rendering Helpers 130

Html.ActionLink and Html.RouteLink 131

URL Helpers 132

Html.Partial and Html.RenderPartial 133

Html.Action and Html.RenderAction 133

Summary 135

CHAPTER 6: DATA ANNOTATIONS AND VALIDATION 137

Annotating Orders for Validation 138

Using Validation Annotations 141

Custom Error Messages and Localization 146

Looking Behind the Annotation Curtain 147

Controller Actions and Validation Errors 148

Custom Validation Logic 150

Custom Annotations 150

IValidatableObject 154

Display and Edit Annotations 155

Display 155

ScaffoldColumn 156

DisplayFormat 156

ReadOnly 157

DataType 157

UIHint 158

HiddenInput 158

Summary 158

CHAPTER 7: MEMBERSHIP, AUTHORIZATION, AND SECURITY 159

Security: Not fun, But Incredibly Important 159

Using the Authorize Attribute to Require Login 162

Securing Controller Actions 162

How AuthorizeAttribute Works with Forms Authentication and the AccountController 167

Windows Authentication 169

Using AuthorizeAttribute to Require Role Membership 172

Extending User Identity 174

Storing additional user profile data 174

Persistance control 174

Managing users and roles 175

External Login via OAuth and OpenID 175

Registering External Login Providers 176

Configuring OpenID Providers 178

Configuring OAuth Providers 180

Security Implications of External Logins 181

Understanding the Security Vectors in a Web Application 182

Threat: Cross-Site Scripting 183

Threat: Cross-Site Request Forgery 193

Threat: Cookie Stealing 197

Threat: Over-Posting 200

Threat: Open Redirection 202

Proper Error Reporting and the Stack Trace 207

Using Configuration Transforms 208

Using Retail Deployment Configuration in Production 209

Using a Dedicated Error Logging System 209

Security Recap and Helpful Resources 209

Summary 211

CHAPTER 8: AJAX 213

jQuery 214

jQuery Features 214

Unobtrusive JavaScript 218

Using jQuery 219

Ajax Helpers 225

Adding the Unobtrusive Ajax Script to Your Project 225

Ajax ActionLinks 226

HTML 5 Attributes 230

Ajax Forms 230

Client Validation 233

jQuery Validation 233

Custom Validation 236

Beyond Helpers 241

jQuery UI 242

Autocomplete with jQuery UI 243

JSON and Client-Side Templates 246

Bootstrap Plugins 251

Improving Ajax Performance 253

Using Content Delivery Networks 253

Script Optimizations 253

Bundling and Minification 254

Summary 255

CHAPTER 9: ROUTING 257

Uniform Resource Locators 258

Introduction to Routing 259

Comparing Routing to URL Rewriting 259

Routing Approaches 260

Defining Attribute Routes 260

Defining Traditional Routes 271

Choosing Attribute Routes or Traditional Routes 280

Named Routes 280

MVC Areas 282

Catch-All Parameter 284

Multiple Route Parameters in a Segment 285

StopRoutingHandler and IgnoreRoute 286

Debugging Routes 286

Inside Routing: How Routes Generate URLs 288

High-Level View of URL Generation 288

A Detailed Look at URL Generation 289

Ambient Route Values 291

More Examples of URL Generation with the Route Class 293

Inside Routing: How Routes Tie Your URL to an Action 294

The High-Level Request Routing Pipeline 294

RouteData 295

Custom Route Constraints 295

Using Routing with Web Forms 296

Summary 297

CHAPTER 10: NUGET 299

Introduction to NuGet 299

Adding a Library as a Package 301

Finding Packages 301

Installing a Package 303

Updating a Package 308

Package Restore 308

Using the Package Manager Console 309

Creating Packages 312

Packaging a Project 313

Packaging a Folder 313

Configuration File and Source Code Transformations 314

NuSpec File 315

Metadata 316

Dependencies 317

Specifying Files to Include 318

Tools 319

Framework and Profile Targeting 322

Prerelease Packages 324

Publishing Packages 325

Publishing to NuGet.org 325

Using NuGet.exe 327

Using the Package Explorer 330

Summary 332

CHAPTER 11: ASP.NET WEB API 333

Defining ASP.NET Web API 334

Getting Started with Web API 335

Writing an API Controller 335

Examining the Sample ValuesController 335

Async by Design: IHttpController 336

Incoming Action Parameters 340

Action Return Values, Errors, and Asynchrony 340

Configuring Web API 342

Configuration in Web-Hosted Web API 343

Configuration in Self-Hosted Web API 343

Adding Routes to Your Web API 346

Binding Parameters 347

Filtering Requests 349

Enabling Dependency Injection 350

Exploring APIs Programmatically 350

Tracing the Application 352

Web API Example: ProductsController 352

Summary 354

CHAPTER 12: SINGLE PAGE APPLICATIONS WITH ANGULARJS 355

Understanding and Setting Up AngularJS 356

What’s AngularJS? 356

Your Goal in This Chapter 356

Getting Started 357

Adding AngularJS to the Site 359

Setting Up the Database 361

Building the Web API 363

Building Applications and Modules 364

Creating Controllers, Models, and Views 365

Services 368

Routing 371

Details View 373

A Custom MovieService 375

Deleting Movies 377

Editing and Creating Movies 379

Summary 384

CHAPTER 13: DEPENDENCY INJECTION 385

Software Design Patterns 385

Design Pattern: Inversion of Control 386

Design Pattern: Service Locator 388

Design Pattern: Dependency Injection 392

Dependency Resolution in MVC 395

Singly Registered Services in MVC 397

Multiply Registered Services in MVC 397

Arbitrary Objects in MVC 399

Dependency Resolution in Web API 402

Singly Registered Services in Web API 402

Multiply Registered Services in Web API 403

Arbitrary Objects in Web API 405

Dependency Resolvers in MVC vs. Web API 405

Summary 405

CHAPTER 14: UNIT TESTING 407

Understanding Unit Testing and Test-Driven Development 408

Defining Unit Testing 408

Defining Test-Driven Development 410

Building a Unit Test Project 412

Examining the Default Unit Tests 413

Test Only the Code You Write 415

Advice for Unit Testing Your ASP.NET MVC

and ASP.NET Web API Applications 415

Testing Controllers 416

Testing Routes 420

Testing Validators 423

Summary 427

CHAPTER 15: EXTENDING MVC 429

Extending Models 430

Turning Request Data into Models 430

Describing Models with Metadata 436

Validating Models 438

Extending Views 442

Customizing View Engines 442

Writing HTML Helpers 444

Writing Razor Helpers 445

Extending Controllers 446

Selecting Actions 446

Filters 447

Providing Custom Results 457

Summary 458

CHAPTER 16: ADVANCED TOPICS 461

Mobile Support 461

Adaptive Rendering 462

Display Modes 470

Advanced Razor 473

Templated Razor Delegates 473

Advanced View Engines 476

Configuring a View Engine 477

Finding a View 478

The View Itself 479

Alternative View Engines 480

New View Engine or New ActionResult? 482

Advanced Scaffolding 482

Introducing ASP.NET Scaffolding 482

Customizing Scaffold Templates 483

Custom Scaffolders 485

Advanced Routing 486

RouteMagic 486

Editable Routes 487

Advanced Templates 492

The Default Templates 492

Custom Templates 496

Advanced Controllers 498

Defining the Controller: The IController Interface 498

The ControllerBase Abstract Base Class 499

The Controller Class and Actions 500

Action Methods 502

The ActionResult 502

Action Invoker 511

Using Asynchronous Controller Actions 515

Summary 520

CHAPTER 17: REAL-WORLD ASP.NET MVC: BUILDING THE NUGET.ORG WEBSITE 521

May the Source Be with You 522

WebActivator 526

ASP.NET Dynamic Data 527

Exception Logging 530

Profiling 532

Data Access 535

EF Code–Based Migrations 536

Deployments with Octopus Deploy 539

Automated Browser Testing with Fluent Automation 540

Other Useful NuGet Packages 541

WebBackgrounder 541

Lucene.NET 542

AnglicanGeek.MarkdownMailer 543

Ninject 543

Summary 544

APPENDIX: ASP.NET MVC 5.1 545

ASP.NET MVC 5.1 Release Description 545

Getting MVC 5.1 546

Upgrading MVC 5 Projects from MVC 5.1 546

Upgrading an MVC 5 Application to 5.1 547

Enum Support in ASP.NET MVC Views 549

Attribute Routing with Custom Constraints 553

Route Constraints in Attribute Routing 554

ASP.NET MVC 5.1 Example: Adding a Custom LocaleRoute 554

Bootstrap and JavaScript Enhancements 558

EditorFor Now Supports Passing HTML Attributes 558

Client-Side Validation for MinLength and MaxLength 561

Three Small but Useful Fixes to MVC Ajax Support 562

Summary 563

INDEX 565

loading