Download e-book : programming ASP.NET MVC 4 / PDF
Download e-book : programming ASP.NET MVC 4 / PDF
Part I. Up and Running
1. Fundamentals of ASP.NET MVC
Microsoft’s Web Development Platforms 3
Active Server Pages (ASP) 3
ASP.NET Web Forms 4
ASP.NET MVC 4
The Model-View-Controller Architecture 4
The Model 5
The View 6
The Controller 6
What’s New in ASP.NET MVC 4? 6
Introduction to EBuy 8
Installing ASP.NET MVC 9
Creating an ASP.NET MVC Application 9
Project Templates 10
Convention over Configuration 13
Running the Application 15
Routing 15
Configuring Routes 16
Controllers 18
Controller Actions 19
Action Results 19
Action Parameters 21
Action Filters 23
Views 24
Locating Views 24
Hello, Razor! 26
Differentiating Code and Markup
Layouts 28
Partial Views 30
Displaying Data 31
HTML and URL Helpers 33
Models 34
Putting It All Together 35
The Route 35
The Controller 35
The View 38
Authentication 41
The AccountController 42
Summary 44
2. ASP.NET MVC for Web Forms Developers . .
It’s All Just ASP.NET 45
Tools, Languages, and APIs 46
HTTP Handlers and Modules 46
Managing State 46
Deployment and Runtime 47
More Differences than Similarities 47
Separation of Application Logic and View Logic 48
URLs and Routing 48
State Management 49
Rendering HTML 50
Authoring ASP.NET MVC Views Using Web Forms Syntax 54
A Word of Caution 55
Summary 56
3. Working with Data
Building a Form 57
Handling Form Posts 59
Saving Data to a Database 59
Entity Framework Code First: Convention over Configuration 60
Creating a Data Access Layer with Entity Framework Code First 60
Validating Data 61
Specifying Business Rules with Data Annotations 63
Displaying Validation Errors 65
Summary 68
4. Client-Side Development
Working with JavaScript 69
Selectors 71
Responding to Events
DOM Manipulation 76
AJAX 77
Client-Side Validation 79
Summary 83
Part II. Going to the Next Level
5. Web Application Architecture .
The Model-View-Controller Pattern 87
Separation of Concerns 87
MVC and Web Frameworks 88
Architecting a Web Application 90
Logical Design 90
ASP.NET MVC Web Application Logical Design 90
Logical Design Best Practices 92
Physical Design 93
Project Namespace and Assembly Names 93
Deployment Options 94
Physical Design Best Practices 94
Design Principles 96
SOLID 96
Inversion of Control 102
Don’t Repeat Yourself 110
Summary 110
6. Enhancing Your Site with AJAX
Partial Rendering 111
Rendering Partial Views 112
JavaScript Rendering 117
Rendering JSON Data 118
Requesting JSON Data 119
Client-Side Templates 120
Reusing Logic Across AJAX and Non-AJAX Requests 123
Responding to AJAX Requests 124
Responding to JSON Requests 125
Applying the Same Logic Across Multiple Controller Actions 126
Sending Data to the Server 128
Posting Complex JSON Objects 129
Model Binder Selection 131
Sending and Receiving JSON Data Effectively 132
Cross-Domain AJAX 133
JSONP
Enabling Cross-Origin Resource Sharing 137
Summary 138
7. The ASP.NET Web API
Building a Data Service 139
Registering Web API Routes 141
Leaning on Convention over Configuration 142
Overriding Conventions 143
Hooking Up the API 143
Paging and Querying Data 146
Exception Handling 147
Media Formatters 149
Summary 152
8. Advanced Data
Data Access Patterns 153
Plain Old CLR Objects 153
Using the Repository Pattern 154
Object Relational Mappers 156
Entity Framework Overview 158
Choosing a Data Access Approach 159
Database Concurrency 160
Building a Data Access Layer 161
Using Entity Framework Code First 161
The EBuy Business Domain Model 163
Working with a Data Context 167
Sorting, Filtering, and Paging Data 168
Summary 174
9. Security .
Building Secure Web Applications 175
Defense in Depth 175
Never Trust Input 176
Enforce the Principle of Least Privilege 176
Assume External Systems Are Insecure 176
Reduce Surface Area 176
Disable Unnecessary Features 177
Securing an Application 177
Securing an Intranet Application 178
Forms Authentication 183
Guarding Against Attacks 192
SQL Injection 192
Cross-Site Scripting
Cross-Site Request Forgery 199
Summary 201
10. Mobile Web Development .
ASP.NET MVC 4 Mobile Features 203
Making Your Application Mobile Friendly 205
Creating the Auctions Mobile View 205
Getting Started with jQuery Mobile 207
Enhancing the View with jQuery Mobile 209
Avoiding Desktop Views in the Mobile Site 216
Improving Mobile Experience 216
Adaptive Rendering 217
The Viewport Tag 217
Mobile Feature Detection 218
CSS Media Queries 220
Browser-Specific Views 221
Creating a New Mobile Application from Scratch 224
The jQuery Mobile Paradigm Shift 224
The ASP.NET MVC 4 Mobile Template 224
Using the ASP.NET MVC 4 Mobile Application Template 226
Summary 229
Part III. Going Above and Beyond
11. Parallel, Asynchronous, and Real-Time Data Operations
Asynchronous Controllers 233
Creating an Asynchronous Controller 234
Choosing When to Use Asynchronous Controllers 236
Real-Time Asynchronous Communication 236
Comparing Application Models 237
HTTP Polling 237
HTTP Long Polling 238
Server-Sent Events 239
WebSockets 240
Empowering Real-Time Communication 241
Configuring and Tuning 245
Summary 246
12. Caching
Types of Caching 247
Server-Side Caching 248
Client-Side Caching
Server-Side Caching Techniques 248
Request-Scoped Caching 248
User-Scoped Caching 249
Application-Scoped Caching 250
The ASP.NET Cache 251
The Output Cache 252
Donut Caching 255
Donut Hole Caching 257
Distributed Caching 259
Client-Side Caching Techniques 264
Understanding the Browser Cache 264
App Cache 265
Local Storage 268
Summary 269
13. Client-Side Optimization Techniques
Anatomy of a Page 271
Anatomy of an HttpRequest 272
Best Practices 273
Make Fewer HTTP Requests 274
Use a Content Delivery Network 274
Add an Expires or a Cache-Control Header 276
GZip Components 278
Put Stylesheets at the Top 279
Put Scripts at the Bottom 279
Make Scripts and Styles External 281
Reduce DNS Lookups 282
Minify JavaScript and CSS 282
Avoid Redirects 283
Remove Duplicate Scripts 285
Configure ETags 285
Measuring Client-Side Performance 286
Putting ASP.NET MVC to Work 289
Bundling and Minification 289
Summary 293
14. Advanced Routing
Wayfinding 295
URLs and SEO 297
Building Routes 298
Default and Optional Route Parameters 299
Routing Order and Priority 301
Routing to Existing Files
Ignoring Routes 302
Catch-All Routes 302
Route Constraints 303
Peering into Routes Using Glimpse 305
Attribute-Based Routing 306
Extending Routing 310
The Routing Pipeline 310
Summary 315
15. Reusable UI Components
What ASP.NET MVC Offers out of the Box 317
Partial Views 317
HtmlHelper Extensions or Custom HtmlHelpers 317
Display and Editor Templates 318
Html.RenderAction() 318
Taking It a Step Further 319
The Razor Single File Generator 319
Creating Reusable ASP.NET MVC Views 321
Creating Reusable ASP.NET MVC Helpers 325
Unit Testing Razor Views 327
Summary 328
Part IV. Quality Control
16. Logging
Error Handling in ASP.NET MVC 331
Enabling Custom Errors 332
Handling Errors in Controller Actions 333
Defining Global Error Handlers 334
Logging and Tracing 336
Logging Errors 336
ASP.NET Health Monitoring 338
Summary 341
17. Automated Testing
The Semantics of Testing 343
Manual Testing 344
Automated Testing 345
Levels of Automated Testing 345
Unit Tests 345
Fast 347
Integration Tests
Acceptance Tests 349
What Is an Automated Test Project? 350
Creating a Visual Studio Test Project 350
Creating and Executing a Unit Test 352
Testing an ASP.NET MVC Application 354
Testing the Model 355
Test-Driven Development 358
Writing Clean Automated Tests 359
Testing Controllers 361
Refactoring to Unit Tests 364
Mocking Dependencies 365
Testing Views 370
Code Coverage 372
The Myth of 100% Code Coverage 374
Developing Testable Code 374
Summary 376
18. Build Automation
Creating Build Scripts 378
Visual Studio Projects Are Build Scripts! 378
Adding a Simple Build Task 378
Executing the Build 379
The Possibilities Are Endless! 380
Automating the Build 380
Types of Automated Builds 381
Creating the Automated Build 383
Continuous Integration 386
Discovering Issues 386
The Principles of Continuous Integration 386
Summary 391
Part V. Going Live
19. Deployment
What Needs to Be Deployed 395
Core Website Files 395
Static Content 398
What Not to Deploy 398
Databases and Other External Dependencies 399
What the EBuy Application Requires 400
Deploying to Internet Information Server 401
Prerequisites
Creating and Configuring an IIS Website 402
Publishing from Within Visual Studio 403
Deploying to Windows Azure 407
Creating a Windows Azure Account 408
Creating a New Windows Azure Website 408
Publishing a Windows Azure Website via Source Control 409
Summary
0 commentaires: