ASP.NET Core Essentials - Shahed Chowdhuri - E-Book

ASP.NET Core Essentials E-Book

Shahed Chowdhuri

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Develop and deploy modern cross-platform web applications with ASP.NET Core

About This Book

  • Develop modern web applications from scratch
  • Integrate server-side and client-side code with modern tools
  • Be prepared for the real world with unit tests and cloud deployment

Who This Book Is For

This book is for software developers who have experience in .NET, preferably with C# or some other Object-Oriented Programming Language, which is required in order to build ASP.NET Core web applications. A basic understanding of web application development is also essential.

What You Will Learn

  • Develop ASP.NET web applications from scratch
  • Use MVC to build all parts of a web application
  • Use Web API to support browsers and mobile apps
  • Simplify development with client-side tools
  • Use Entity Framework to integrate with your database
  • Implement Dependency Injection to build testable software
  • Build web solutions with performance and security in mind
  • Deploy to the cloud and scale with ease

In Detail

ASP.NET Core is the latest collection of Microsoft's web application development technologies. When you're trying to reach a broad spectrum of users with a robust web application, ASP.NET Core is there to help you build that application. With the ability to cater to users on desktop, tablet, or smartphone platforms, you can put together a solution that works well anywhere.

This book is what you need to get started developing ASP.NET Core applications was quickly as possible; starting by introducing the software and how it can be used in today's modern world of web applications and smartphone apps. Walking you through the benefits of a Web API to support both applications and mobile apps to give you a solid understanding of the tech to build upon as you see what ASP.NET Core can do for you.

The book wraps up with practical guidelines for the use of database technologies, unit tests, security best practices, and cloud deployments for the real world.

Style and approach

This is a comprehensive guide for .NET developers to build the next generation of web apps. This book can be read from start to finish, but you can also come back to any chapter for a quick refresher, as each chapter is modularized for specific concepts.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 198

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

ASP.NET Core Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started with ASP.NET Core
ASP.NET Core - Unifying MVC, Web API, and Web Pages
High-level overview
Version numbers
Putting it all together
Differences between .NET Framework and .NET Core
Full .NET Framework 4.6
Lightweight .NET Core
Running ASP.NET Core on .NET Framework versus .NET Core
What's new with Visual Studio 2015 and Visual Studio Code?
Community Edition
Professional and Enterprise Editions
Visual Studio Code
Running ASP.NET Core on Windows, Mac OS X, and Linux
ASP.NET Core on Windows
ASP.NET Core on Mac OS X
ASP.NET Core on Linux
Summary
2. Building Your First ASP.NET Core Application
Project templates in Visual Studio 2015
Empty template
Web API template
Web Application template
Hello, ASP.NET - your first ASP.NET application
Writing a response
Launching the application
Web files and folders
Models, views, and controllers - an MVC refresher
Controllers
Models
Views
Web configuration with project.json
Dependencies and frameworks
Commands and tools
Bundling and publishing
Summary
3. Understanding MVC
Building controllers
Controller methods
Basic controllers
URL routes and conventions
Implementing views
Basic views
Tag helpers in views
ViewData, ViewBag, and TempData
Designing models and ViewModels
Creating models
Binding models to views
ViewModels and mapping
Bringing it all together
Scaffolding, validation, and model binding
Database setup and data entry
Exception handling
Summary
4. Using Web APIs to Extend Your Application
Understanding a Web API
The case for Web APIs
Creating a new Web API project from scratch
Building your Web API project
Configuring the Web API in your web application
Setting up dependencies
Parts of a Web API project
Running the Web API project
Adding routes to handle anticipated URL paths
Understanding routes
Setting up routes
Testing routes
Consuming a Web API from a client application
Testing with external tools
Consuming a Web API from a mobile app
Consuming a Web API from a web client
Summary
5. Interacting with Web API using JavaScript
Using JavaScript to interact with Web API
Preparing the server-side code
Client-side JavaScript
JavaScript frameworks
Single-page applications with AngularJS
Getting started with AngularJS
AngularJS syntax and features
Building a web application with AngularJS
Model-View-ViewModel (MVVM) with KnockoutJS
Getting started with KnockoutJS
KnockoutJS syntax and features
Building a web application with KnockoutJS
Task runners, bundling, and minification using Bower, Grunt, and Gulp
Why do we need task automation?
Using Bower as your package manager
Using Gulp and Grunt as task runners
Summary
6. Using Entity Framework to Interact with Your Database in Code
Object-relational mapping in .NET
Why use an ORM?
Why Entity Framework?
The evolution of Entity Framework
EF 6.x for .NET Framework versus EF Core 1.0
What's different in EF Core
Getting started with EF Core
What else is new?
Code First approach to database design and relationships
Updating the models
Updating the controllers
Updating the views
EF Code First migrations for database versioning and maintenance
Setting up migrations
Adding and removing migrations
Running your application
Summary
7. Dependency Injection and Unit Testing for Robust Web Apps
Understanding IoC
Pros and cons of DI
SOLID principles and Gang of Four patterns
Loose coupling
Implementing DI in ASP.NET Core
Lifecycle management
Constructor injection versus action injection
Verifying the expected behavior
DI options in ASP.NET Core
Built-in IoC
Autofac
Other alternatives
Writing unit tests
Setting up a test project
Running unit tests
Going beyond the basics
Summary
8. Authentication, Authorization, and Security
Enabling authentication in ASP.NET
High-level overview
Authentication configuration
External service providers
Using authorization for application features
High-level overview
Basic authorization
Roles and claims
Protecting your data
Data protection in ASP.NET Core
Implementing data protection
How it all works
Implementing web application security
Cross-site scripting
Anti-forgery
Cross-origin requests
Summary
9. Deploying Your Application
Deployment options
Environment configuration
Deploying your web app
Deploying your database
Deploying to IIS
Setting up IIS
Using the filesystem
Importing a publish profile
Deploying to Azure, Microsoft's cloud platform
Creating a web app
Creating a virtual machine
Deploying to Azure
Continuous integration
Preparing for CI
Setting up Continuous Deployment
The pitfalls of CI
Summary

ASP.NET Core Essentials

ASP.NET Core Essentials

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: September 2016

Production reference: 1160916

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham 

B3 2PB, UK.

ISBN 978-1-78588-915-8

www.packtpub.com

Credits

Author

Shahed Chowdhuri

Copy Editor

Safis Editing

Reviewer

Steve Albers

Project Coordinator

Ulhas Kambali

Commissioning Editor

Dipika Gaonkar

Proofreader

Safis Editing

Acquisition Editor

Sonali Vernekar

Indexer

Rekha Nair

Content Development Editor

Prashanth G

Graphics

Kirk D'Penha

Technical Editor

Sushant S Nadkar

Production Coordinator

Melwyn Dsa

About the Author

Shahed Chowdhuri has over 18 years of experience in the field of professional software development, and is currently a Senior Technical Evangelist at Microsoft Corporation. He began his career with what is now known as Classic ASP, and worked with ASP.NET as it continued to evolve over the years. He is a public speaker in the DC metro area and along the East Coast of the United States. He serves as a mentor for startups and indie developers at 1776 DC, and also manages multiple developer groups on Facebook and Meetup. You can find him blogging at WakeUpAndCode.com or on Twitter via @shahedC.

About the Reviewer

Steve Albers is a software developer and speaker living in Northern Virginia.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Preface

Web applications have flourished in enterprise application development, even as mobile applications continue to rise in popularity. Moreover, the use of web APIs provides greater flexibility when it comes to building a backend that can serve both web apps and mobile apps. With ASP.NET Core 1.0, you get the benefits of a mature programming language such as C# with the performance of an all-new .NET Core that allows your web apps and web APIs to run on Windows, Mac, and Linux.

ASP.NET Core lets you choose your operating system during development, whether you're writing your code on a Surface Book, MacBook Air, or on a popular distribution of Linux. While this book primarily covers development on Visual Studio 2015 on a Windows system, you will find links to online guides if you wish to set up your environment on a different operating system.

ASP.NET Core 1.0 Essentials will introduce you to Microsoft's latest revision of ASP.NET, with everything you need to get started today. If you have already worked with ASP.NET MVC before, you will get a refresher of a few things you already know, followed by what's new. If you've only worked with ASP.NET Web Forms or other competing web frameworks, you will find the initial chapters very useful in laying the groundwork for what you need to know.

What this book covers

Chapter 1, Getting Started with ASP.NET Core, teaches you about ASP.NET Core 1.0, including MVC and web API. This chapter will explain the differences between the full .NET Framework 4.6 and .NET Core, while introducing Visual Studio 2015 and the all-new cross-platform Visual Studio Code.

Chapter 2, Building Your First ASP.NET Core Application, shows you how to create a new ASP.NET Core web application. This chapter will dissect the parts of a modern ASP.NET web application, while explaining what's new and what's changed.

Chapter 3, Understanding MVC, teaches you all about ASP.NET Core MVC by going deeper into controllers, views, and models. This chapter will explain how to create all the parts of a modern MVC application and then bring it all together.

Chapter 4, Using Web API to Extend Your Application, is about using the web API to extend your web application to support web  and mobile applications. This chapter will explain how to create routes and configure a web API application and then consume it from a client application.

Chapter 5, Interacting with Web API using JavaScript, teaches you how to interact with the ASP.NET web API using straight JavaScript as well as popular frameworks such as AngularJS and KnockoutJS. This chapter will also cover developer tools, which automate important tasks and help during the development process.

Chapter 6, Using Entity Framework to Interact with Your Database in Code, teaches you how to use Entity Framework in the data layer of your web application. This chapter will introduce object-relational mapping (ORM) tools and the use of EF Code First Migrations .

Chapter 7, Dependency Injection and Unit Testing for Robust Web Apps, shows you how to implement dependency injection by using Inversion of Control . This chapter will cover DI, IoC containers, and Microsoft's new built-in support for dependency injection. The chapter will also cover the basics of unit testing.

Chapter 8, Authentication, Authorization, and Security, is about enabling authentication in ASP.NET web applications while implementing authorization for specific application features. The chapter will also cover guidelines for protecting user data and strategies to combat common security vulnerabilities in web applications.

Chapter 9, Deploying Your Application, covers how to deploy your applications to IIS on a web server or on Microsoft's Azure cloud platform. The chapter will also cover the use of continuous integration to run unit tests and deploy successfully compiled applications.

What you need for this book

To learn ASP.NET Core and run the code examples for this book, the following software is recommended:

Windows 7 or higher (Windows 10 recommended)Visual Studio 2015 Update 3 or higher (the free Community Edition is OK)Any modern web browser, such as Microsoft Edge, Google Chrome, or Mozilla FirefoxOptional: Visual Studio Code (Windows, Mac, Linux)

For more information on developing for and running ASP.NET Core on systems other than Windows, please consult the Running ASP.NET Core on Windows, Mac OS X, Linux  section in Chapter 1, Getting Started with ASP.NET Core. There, you will find a high-level overview of instructions for Mac and Linux, with links to online guides that are frequently updated.

Who this book is for

This book is for software developers who have experience in .NET, preferably with C# or some other object-oriented programming language, which is required in order to build ASP.NET Core web applications. A basic understanding of web application development is also essential.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Uses the HTTP GET method with optional querystring parameters"

A block of code is set as follows:

app.UseMvc(routes => {     routes.MapRoute(         name: "default",         template: "{controller=Home}/{action=Index}/{id?}"); });

Any command-line input or output is written as follows:

dotnet restoredotnet builddotnet ef migrations add Initialdotnet ef database update

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Enter some values and click the Create button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book 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 files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/ASPdotNET-Core-Essentials. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Chapter 1.  Getting Started with ASP.NET Core

Active Server Pages was first made available on Microsoft's IIS web server in the mid-1990s. Fast-forward about 20 years, and ASP.NET Core (that is, ASP.NET 5 while in development) is now open source and runs on Windows, Linux, and OS X. Moreover, Visual Studio is now available as a cross-platform lightweight code editor in addition to the full-featured IDE on Windows.

As a .NET developer, you can take advantage of all the recent improvements, while building on the experience that you already have. The .NET Framework has evolved beyond the full framework and is now available as a cross-platform runtime called .NET Core.

In this chapter, we will discuss the following principles and concepts to get you started with ASP.NET Core:

.NET architectureASP.NET unified programming modelNew Visual Studio IDEsCross-platform runtime

ASP.NET Core - Unifying MVC, Web API, and Web Pages

When developers hear the term ASP.NET, some associate it with ASP.NET Web Forms, while others think of ASP.NET MVC or even Web API. Many developers have started their journey with Web Forms, and have migrated to Model-View-Controller (MVC) in recent years.

The adoption of ASP.NET MVC has been on the rise for years, due to its cleaner code and testability. While you can still develop Web Forms on .NET 4.x, you will be using MVC on .NET Core 1.0. Fortunately, ASP.NET Core runs on both .NET 4.5.1+ and .NET Core, so you can choose which runtime you would like to target.

Going forward, Microsoft has unified MVC, Web API, and Web Pages in ASP.NET Core. What does this mean for you, the developer? It means that you will enjoy more consistency when building your application with MVC, Web API, and Web Pages. In the past, MVC and Web API were implemented separately, leading to duplication and inconsistencies, but with ASP.NET Core, they have been merged into a single programming model.

High-level overview

If you're new to any of the preceding terms, here's a high-level overview:

ASP.NET MVC: Microsoft's implementation of the MVC architectural pattern, used for building test-friendly web applications that are robust yet lightweight.Web API: Microsoft's answer to RESTful APIs, Web API allows developers to build HTTP services that can serve as a backend to web applications and mobile apps.Web Pages: Microsoft's solution for creating lightweight dynamic websites. At the time of writing, Web Pages is not available in ASP .NET Core 1.0 so it will not be covered in this book.

The following figure illustrates a typical ASP.NET web application that uses both MVC and Web API. Note that Web API can be used to serve clients other than web browsers, such as mobile apps. It's important to mention that browsers here can include mobile web browsers as well:

Version numbers

You may have noticed that I mentioned MVC 6 while talking about ASP.NET Core. ASP.NET MVC 6 has been renamed ASP .NET Core 1.0 MVC. To keep track of all the new version numbers, the following is a handy list of what you need to know:

C# 6.0ASP.NET Core:
Runs on .NET Core 1.0 or .NET Framework 4.5.1+
.NET Framework 4.6.2 (at the time of writing).NET Core 1.0ASP.NET Core 1.0 MVCSignalR 3 (not released at the time of writing)

Putting it all together

Now that you're familiar with the moving parts of ASP.NET Core and its various version numbers, how will you decide which parts to use? The good news is that you won't have to figure it all out before you get started on your project.

Think about the requirements of your web software project and the needs of your customers. Choose the technologies that make sense for you and your team, and get started on a Minimum Viable Product (MVP).

If you need to support web browsers, go with ASP.NET MVC and build responsive web applications that look good on any screen size. If you need to extend your application with Web API, expose only what needs to be exposed.

If you need real-time functionality, SignalR is a great place to start. At the time of writing, Microsoft has announced that SignalR 3 will not be included with the 1.0 release of ASP.NET Core. As a result of this, SignalR 3 will not be covered in this book. For more information, stay tuned to the SignalR website:  http://www.asp.net/signalr .

Differences between .NET Framework and .NET Core

Since the inception of .NET Framework, there has always been one release at a time. Whenever a newer version was released, you could upgrade to the latest version or install multiple versions side by side. But there was always one single latest version.

Going forward, there are now two distinct versions: the full .NET Framework and the all-new .NET Core. While ASP.NET Core web applications can run on either one of them, you will decide which is more suitable for your needs. You can change the runtime during development or during release. Best of all, you can deploy the runtime alongside your released product, which allows multiple versions of .NET to be deployed to the same server.

The following screenshot illustrates how ASP.NET Core runs on both .NET Framework 4.6 and .NET Core 1.0:

Full .NET Framework 4.6

Even with the release of .NET Core, there is still a place for the full .NET Framework. It will continue to be the framework of choice for rich Windows desktop applications, created with Windows Presentation Foundation (WPF) or Windows Forms. It will be one of two choices for ASP.NET Core developers.

For ASP.NET 4 developers, .NET Framework 4.6 will be an in-place replacement for .NET 4.x runtimes. This includes .NET 4, 4.5, 4.5.1, and 4.5.2. One good reason to upgrade to .NET 4.6 is the added benefit of new improvements such as better compilation and added language features.

Lightweight .NET Core

The new .NET Core is a lightweight cross-platform subset of the full .NET Framework that makes its home on Windows, Linux, and OS X. It is expected to leap past the .NET Framework in new features that may make their way back to the .NET Framework.

It is worth noting that .NET Core is not an option for Windows desktop developers or ASP.NET 4 developers. In addition to supporting ASP.NET Core web applications with the CoreCLR runtime, .NET Core also includes the .NET native runtime, which is specifically used for Universal Windows Applications on Windows 10.

Running ASP.NET Core on .NET Framework versus .NET Core

To recap, let's focus on the following benefits of running an ASP.NET Core web application on .NET Core, in addition to its cross-platform support:

Flexibility: With .NET Core, web applications can be deployed with a specific version of the .NET Core framework, which will allow you to deploy each application with only the version that it needsPerformance: With .NET Core, you'll enjoy performance benefits due to its lower memory footprint and faster start-up times