O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering ASP.NET Core 2.0

Book Description

Become an expert in the shiny new version of ASP.NET

About This Book

  • Grasp the advanced concepts of MVC Core and start making professional-grade applications
  • Migrate from previous versions of ASP.NET MVC to ASP.NET Core
  • This book will take you on a journey through all the aspects of ASP.NET Core 2.0

Who This Book Is For

If you are a developer who knows the basics of ASP.NET MVC and wants to build powerful applications, this book is for you. Basic C# experience and an understanding of HTML and CSS is assumed.

What You Will Learn

  • Get to know the new features of ASP.NET Core 2.0
  • Find out how to configure ASP.NET Core
  • Configure routes to access ASP.NET Core resources
  • Create controllers and action methods and see how to maintain the state
  • Create views to display contents
  • Implement and validate forms and retrieve information from them
  • Write reusable modules for ASP.NET Core
  • Deploy ASP.NET Core to other environments

In Detail

ASP.NET is an open source web framework that builds modern web apps and services. This book is your one-stop guide to the new features of ASP.NET Core 2.0, including web APIs and MVC. We begin with a brief overview of the basics, taking you through the MVC pattern, platforms, dependencies, and frameworks. We then move on to setting up and configuring the MVC environment before talking about routing and advanced routing options. Next, we'll look at model binding, controllers and actions, filters, user authentication, and testing.

Moving on, you'll learn about all the aspects of syntax and processes when working with Razor. You'll be introduced to client-side development and will get to know about the security aspects of ASP.NET Core. We will also look at microservices with ASP.NET Core. Finally, you'll find out how to deploy ASP.NET Core to new environments such as Azure, AWS, and Docker. By the end of the book, you will be well versed with development in ASP.NET Core and will have a deep understanding of how to interact with the framework and work cross-platform.

Style and approach

Find out how to design modern, cross-platform, business oriented web applications that serve the client needs in the age of emerging .NET framework

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Getting Started with ASP.NET Core
    1. Introducing ASP.NET Core
    2. .NET Core
    3. Platforms
    4. Dependencies and frameworks
      1. Targeting .NET Core or the full .NET framework
    5. Understanding the MVC pattern
    6. Getting one's context
    7. The OWIN pipeline
    8. Hosting
      1. Kestrel
      2. WebListener
      3. IIS
      4. Nginx
      5. Apache
      6. Configuration
      7. Features
      8. Launch configuration
    9. Inversion of control and dependency injection
    10. Knowing the environments
    11. A sample project
      1. Structure
    12. Summary
  3. Configuration
    1. Configuration in .NET Core
    2. Providers
      1. File-based providers
        1. JSON provider
        2. XML provider
        3. INI provider
      2. Other providers
        1. User secrets
        2. Azure Key Vault
        3. Command line
        4. Environment variables
        5. Memory
        6. Docker
      3. Creating a custom provider
    3. Using configuration values
      1. Getting and setting values explicitly
      2. Configuration sections
      3. Getting all values
      4. Binding to classes
      5. Injecting values
      6. Reloading and handling change notifications
    4. Putting it all together
    5. Summary
  4. Routing
    1. Routing tables
    2. Route templates
    3. Route parameter matching
    4. Route selection
      1. Special routes
    5. Route defaults
    6. Route handlers
      1. Routing to inline handlers
    7. Route constraints
      1. HTTP methods
      2. Default constraints
      3. Custom constraints
    8. Route data tokens
    9. Areas
    10. Routing attributes
      1. Routes
      2. Defaults
      3. Constraints
      4. Areas
      5. Action names
      6. Non actions
      7. Restrictions
      8. Route values
    11. Dealing with errors
      1. Catch-all route
      2. Status code pages middleware
        1. Specific status codes
        2. Any status code
    12. Putting it all together
    13. Summary
  5. Controllers and Actions
    1. Using controllers
      1. Controller base classes
      2. POCO controllers
        1. Adding context to POCO controllers
        2. Intercepting actions in POCO controllers
    2. Finding controllers
    3. Controller life cycle
    4. Actions
      1. Finding actions
      2. Synchronous and asynchronous actions
      3. Getting the context
      4. Action constraints
      5. Action parameters
      6. Model binding
        1. Body
        2. Form
        3. Header
        4. Query
        5. Route
        6. Dependency injection
        7. Custom binders
        8. Input formatters
        9. Explicit binding
        10. Cancelling requests
      7. Model validation
      8. Action results
    5. API actions
      1. Output formatters
      2. Content negotiation
      3. API versioning
        1. Header values
        2. URL
        3. Deprecation
        4. Default version
        5. Version mapping
        6. Invalid versions
      4. API documentation
      5. Response caching
    6. Maintaining state
      1. Request
      2. Form
      3. Query string
      4. Route
      5. Cookies
      6. Session
      7. Cache
        1. In-memory cache
        2. Distributed cache
          1. Redis
          2. SQL Server
      8. Temporary data
      9. Comparing state maintenance techniques
    7. Dependency injection
    8. Globalization and localization
    9. Putting it all together
    10. Summary
  6. Views
    1. View classes
    2. Locating views
      1. View location expanders
      2. View engines
      3. Logging and diagnostics
      4. View compilation
      5. Model
      6. ViewBag
      7. Temporary data
    3. View layouts
      1. Partial views
        1. Passing data to partial views
        2. Finding partial views
      2. Special files
      3. View options
    4. Areas
    5. Dependency injection
      1. Resources
      2. Translated views
    6. Razor Pages
      1. Page model
      2. Page handlers
      3. Model binding
      4. Model validation
      5. Maintaining state
      6. View layouts
      7. Partial views
      8. Special files
      9. Filters
      10. Dependency injection
      11. Configuring options
      12. Page routes
      13. Security
    7. Putting it all together
    8. Summary
  7. Using Forms and Models
    1. Form context
    2. Model metadata
      1. Models of anonymous types
    3. HTML helpers
      1. Forms
      2. Single line text boxes
      3. Multi-line text boxes
      4. Passwords
      5. Dropdowns
      6. List boxes
      7. Radio buttons
      8. Checkboxes
      9. Hidden values
      10. Links
      11. Labels
      12. Raw HTML
      13. IDs, names, and values
      14. Generic editor and display
      15. Utility methods and properties
      16. Validation messages
      17. Custom helpers
    4. Templates
    5. Model binding
    6. Model validation
      1. Server-side validation
        1. Configuration
        2. Data annotations validation
          1. Error messages
        3. Self-validation
        4. Custom validation
      2. Client-side model validation
        1. Configuration
        2. Custom validation
    7. AJAX
      1. Validation
      2. Restrictions
      3. Contents
    8. Uploading files
      1. Direct access
    9. Putting it all together
    10. Summary
  8. Security
    1. Authentication
      1. Windows authentication
      2. Custom authentication
        1. ASP.NET Core 1.x authentication
        2. ASP.NET Core 2.x authentication
        3. Identity
        4. Using JWT
        5. IdentityServer
          1. ASP.NET Core 1.x
          2. ASP.NET Core 2.x
        6. Azure AD
        7. Social logins
          1. Facebook
          2. Twitter
          3. Google
          4. Microsoft
      3. Binding security
      4. Cookie security
    2. Authorization
      1. Roles
      2. Policies
      3. Authorization handlers
      4. Resource-based authorization
      5. Anonymous access
    3. Anti-forgery
    4. HTML encoding
    5. HTTPS
      1. Hosting
        1. IIS Express
        2. Kestrel
          1. ASP.NET Core 1.x
          2. ASP.NET Core 2.x
        3. WebListener
      2. Restricting HTTP
      3. Redirecting to HTTPS
    6. CORS
    7. Data protection
    8. Static files
      1. Using an action to retrieve files
      2. Using middleware to enforce security
    9. Putting it all together
    10. Summary
  9. Reusable Components
    1. View components
      1. Discovering view components
      2. Using view components
      3. View component results
      4. Dependency injection
    2. Tag helpers
      1. Understanding the properties of a tag helper
      2. Restricting applicability
      3. Discovering tag helpers
      4. Dependency injection
      5. Included tag helpers
        1. The <a> tag
        2. The <cache> tag
        3. The <distributed-cache> tag
        4. The <environment> tag
        5. The <form> tag
        6. The <script> tag
        7. The <link> tag
        8. The <select> tag
        9. Validation message and summary
    3. Tag helper components
    4. Partial views
      1. Partial views versus view components
    5. Putting it all together
    6. Summary
  10. Filters
    1. Types of filters
      1. Synchronous versus asynchronous
      2. Filter scope
      3. Execution order
      4. Applying filters through attributes
      5. Factories and providers
      6. Dependency injection
      7. Context
    2. Authorization filters
      1. Authorization policies
    3. Resource filters
    4. Understanding action filters
    5. Result filters
    6. Exception filters
    7. Page filters
    8. Putting it all together
    9. Summary
  11. Logging, Tracing, and Diagnostics
    1. Common logging
      1. Using logging
      2. Logging providers
        1. ASP.NET Core 1.x
        2. ASP.NET Core 2.x
      3. Log filtering
        1. ASP.NET Core 1
        2. ASP.NET Core 2
      4. Custom providers
      5. Dependency injection
      6. Logging attributes
    2. Custom logging middleware
    3. Diagnostics
    4. Telemetry
      1. ASP.NET Core 1.x
      2. ASP.NET Core 2.x
      3. Custom events
    5. Health checking
    6. Putting it all together
    7. Summary
  12. Testing
    1. Unit testing
      1. xUnit
      2. Injecting dependencies
      3. Mocking
      4. Assertions
      5. User interface
      6. Using the command line
      7. Limitations of unit tests
    2. Integration testing
    3. Putting it all together
    4. Summary
  13. Client-Side Development
    1. Using Bower
    2. Using Node.js
      1. Calling Node from .NET Core
      2. Pre-compiling views
      3. Using WebPack
      4. SPA templates
    3. Using Gulp
    4. Using Grunt
    5. Using TypeScript
    6. Using Less
    7. Putting it all together
    8. Summary
  14. Improving the Performance and Scalability
    1. Profiling
      1. CoreProfiler
      2. MiniProfiler
      3. Stackify Prefix
    2. Hosting
      1. Choosing the best host
      2. Tuning
        1. Maximum number of simultaneous connections
        2. Limits
        3. Timeouts
    3. Bundling and minification
    4. Asynchronous actions
    5. Caching
      1. Caching data
        1. In-memory cache
        2. Distributed cache
      2. Caching action results
      3. Caching views
    6. Compressing responses
    7. Putting it all together
    8. Summary
  15. Real-Time Communication
    1. Setting up SignalR
    2. Hosting a hub
    3. Communication protocols
    4. SignalR context
    5. Message targets
    6. Communicating from the outside
    7. Summary
  16. Other Topics
    1. Areas
      1. Usage
      2. Tag and HTML helpers
    2. Working with static files and folders
      1. Directory browsing
      2. Static files
      3. Default documents
      4. Security
    3. Application lifetime events
    4. Embedded resources
    5. Hosting extensions
      1. Hosting startup
      2. Hosted services
    6. Custom conventions
    7. URL rewriting
      1. URL redirection
      2. URL rewrite
      3. Runtime evaluation
      4. HTTPS
      5. Platform-specific
    8. Putting it all together
    9. Summary
  17. Deployment
    1. Manual deployment
      1. Setting the target framework
      2. Self-contained deployments and runtimes
      3. Real-time rebuilding
    2. Visual Studio deployment
    3. IIS
    4. Nginx
    5. Azure
    6. AWS
    7. Docker
    8. Windows Service
    9. Putting it all together
    10. Summary