Cover Page

Front‐end Development with ASP.NET Core, Angular, and Bootstrap

 

 

 

Simone Chiaretta

 

 

 

 

 

 

Wiley Logo

For Signe, for the support over the project

ABOUT THE AUTHOR

SIMONE CHIARETTA (Brussels, Belgium) is a web architect and developer who enjoys sharing his development experiences and more than 20 years’ worth of knowledge on web development with ASP.NET and other web technologies. He has been a Microsoft MVP for ASP.NET for eight years, has authored several books about ASP.NET MVC (Beginning ASP.NET MVC 1.0 and What’s New in ASP.NET MVC 2?, both published by Wrox, and OWIN Succinctly and ASP.NET Core Succinctly, published by Syncfusion), and has contributed to online developer portals (like Simple Talk). Simone also co‐founded the Italian ALT.NET usergroup ugialt.NET and is the co‐organizer of many conferences in Milan, including the widely appreciated Web.NET European Conference in 2012.

You can read Simone’s thoughts and development tips on his blog: http://codeclimber.net.nz.

When not writing code and blog posts and taking part in the worldwide.NET community, Simone likes to play with Arduino, drones, and underwater robots, and he is training to complete his first Ironman in 2018. He is one of the many expats living and working in the capital of Europe, Brussels, where he is leading the team that develops the public web site of the Council of the European Union, one of the ruling bodies of the European Union.

ABOUT THE TECHNICAL EDITOR

UGO LATTANZI is a Microsoft‐certified ASP.NET MVP who specializes in enterprise application development with a focus on web applications, service‐oriented applications, and environments where scalability is a top priority. He is highly skilled in technologies such as ASP.NET MVC, NodeJS, Azure, and more. He is currently the Chief Software Architect for Technogym, and he formerly has served as a technical manager for MTV and has been a consultant for some of the largest companies in Italy. He also has enjoyed an active role in the tech community, serving as a technical writer, editor, and speaker for numerous conferences, book publishers, newspapers, webcasts, and forums. He’s also the co‐owner of the Web.NET European Conference located in Milan.

CREDITS

  • PROJECT EDITOR
    Kelly Talbot
  • TECHNICAL EDITOR
    Ugo Lattanzi
  • PRODUCTION EDITOR
    Barath Kumar Rajasekaran
  • COPY EDITOR
    Kelly Talbot
  • PRODUCTION MANAGER
    Katie Wisor
  • MANAGER OF CONTENT ENABLEMENT AND OPERATIONS
    Pete Gaughan
  • MARKETING MANAGER
    Christie Hilbrich
  • BUSINESS MANAGER
    Amy Knies
  • EXECUTIVE EDITOR
    Jim Minatel
  • PROJECT COORDINATOR, COVER
    Brent Savage
  • PROOFREADER
    Nancy Bell
  • INDEXER
    Nancy Guenther
  • COVER DESIGNER
    Wiley
  • COVER IMAGE
    © DigitalVision/Getty Images, Inc.

ACKNOWLEDGMENTS

I WANT TO START BY THANKING MY GIRLFRIEND SIGNE, for her support and for putting up with the many hours that this project took from my free time. This book would have not been possible without her support and blessing.

Then I would like to thank the editorial group at Wiley/Wrox. To Jim Minatel for helping draft the original idea behind the book and getting started with the project, and to Kelly Talbot, who without any shadow of a doubt did an amazing job with the copyediting of the manuscript and improving the language and readability of the book, and who kept pushing me and motivating me during the various rewrites that this book needed due to fundamental changes in the technologies covered.

And also a great thank you to Ugo Lattanzi, the technical editor, who helped catch some technical flaws and overall helped a lot with the quality of the book.

Besides the people directly involved in the writing of the book, I also want to thank the ASP.NET team, first for creating such a great framework, and also for helping me out in the early stages to understand where the framework was going. In particular I want to thank Bertrand Le Roy, Scott Hunter, and Scott Hanselman for providing me with up‐to‐date details about the upcoming releases.

A special thanks goes to Mads Kristensen for writing the preface and for always helping me out with questions about integration of front‐end features into Visual Studio 2017.

I also want to thank Jason Imison for his help in better understanding the role of OmniSharp inside VS Code and to Juri Strumpflohner, for helping me out with some Angular‐related matters.

Finally, I also have to thank my coworkers and manager Gunter for their support and review of the manuscript.

FOREWORD

The web becomes an increasingly capable platform with each new browser release. Tomorrow’s new features become available to early adopters while yesterday’s stabilize for mass adoption. This ever‐growing toolbox of HTML, CSS, and JavaScript capabilities seems to be accelerating and shows no signs of slowing down. In fact, it’s growing so fast that specialized tooling is required to take advantage of these new features—tooling such as WebPack and Gulp.

The amount of knowledge required to build modern browser applications is illustrated by the new job title “front‐end engineer”—a term that didn’t exist just a few years ago.

In addition to the advances in the web platform, the server‐side technologies powering the web are evolving as well. To provide the best experience for both end‐users and web developers, the server‐side platform must be ultra‐fast, secure, cross‐platform, and cloud scalable, and it must have great tooling.

Most web applications consist of client‐side code running in the browser and server‐side code running on one or more servers. To be a proficient web developer, it is necessary to know enough of both client and server technologies, and that is a big challenge. Because how much is enough, and how much time should we devote to continuously learning?

One way to make it easier for ourselves is to choose the right set of frameworks and tools to build our applications upon. Frameworks are useful because they usually wrap complex platform features into easy‐to‐use components, so web developers can focus on writing their applications’ logic instead of all the plumbing needed to work with the browser or server platforms.

Choosing the right frameworks is important. The options are many, but there are a few that have shown to be particularly well‐suited for building modern web applications. ASP.NET Core as the application framework for the server and Angular for the client is a great combination. Bootstrap makes sure the app looks good in all browsers and on all types of devices.

Simone has done a great job showing how these frameworks complement each other and how tooling provides a solid development experience. In the fast‐moving world of web development, it is good that we have books like this to give us a pragmatic approach to building web applications using the latest and greatest in both client and server technologies.

MADS KRISTENSEN

INTRODUCTION

There was a time when “backend” and “front‐end” developers were doing pretty different work. Backend developers were writing code to render web pages, using some server‐side language; front‐end developers were programming some interactivity using JavaScript and making the web pages look beautiful with CSS.

A few years ago, with the arrival of SPAs (Single Page Applications), JavaScript was not only used to add “some” interactivity, but was used to build the application itself. The skill‐set of the so‐called backend developers had to expand to include all the tools that were typical of front‐developers, such as specific JavaScript frameworks and some basic understanding of CSS.

The goal of this book is to explain the tools in “front‐end” developers’ toolbox and how to use them effectively in combination with ASP.NET Core MVC.

WHY WEB DEVELOPMENT REQUIRES POLYGLOT DEVELOPERS

In everyday life, a polyglot is a person who knows and is able to use several languages. They are not necessarily bilingual (or multi‐lingual), but they can use their second or third or fourth language with a good level of proficiency.

With that definition in mind, what is a polyglot developer? It’s a developer who knows more than one (programming) language or framework and uses them within the scope of the same application.

From the beginning of IT industry, applications were mainly written in one language. I personally started with C, then moved to Visual Basic, landed on Cold Fusion, used JavaScript at its early stages (both on the client and the server), did a bit of Java, and finally landed on the.NET platform, but only one at a time.

It was the time of the big enterprise frameworks, when vendors tried to squeeze into their language or platforms all the possible features that an application might need. You probably remember when Microsoft tried to shield web developers from the actual languages of the web, HTML and JavaScript, introducing ASP.NET WebForms and the ASP.NET Ajax Framework. And if you look back at your personal experience in the IT industry, you’ll probably find many other similar examples.

But lately a new trend has appeared, which has gone in the opposite direction. The IT industry understands that maybe some languages are more suitable than others for specific tasks, and instead of trying to force everything on top of one single language, applications are being written in multiple languages. We are entering the era of the polyglot developer.

Now that we share the same definition of a polyglot developer, let’s see what the benefits of being one are.

The Right Tool for the Job

The first and probably most important benefit of being a polyglot is being able to choose the right tool for the job without having to compromise because your language or framework of choice doesn’t support a given feature.

For example, by using the Microsoft AJAX Framework, you are limited by the functionalities it provides, while by directly using JavaScript you have all the flexibility provided by the language.

And I’m confident you will agree with me that as a web developer you have to know HTML, but nevertheless using the design surface of Visual Studio, you can build web applications just by dragging elements from the toolbox. Obviously, you do not have the same level of control as when crafting your HTML code directly.

So, to a certain degree, every web developer is already a polyglot developer.

The integration of SASS into Visual Studio 2015 is another example. A few years ago the Ruby community came up with the idea of a preprocessor for CSS styles. Instead of creating a.NET version of SASS, Microsoft decided to integrate the original version into its IDE, and SASS was the right tool for preprocessing CSS styles.

Cross Pollination

A second benefit of knowing multiple languages or frameworks is that you can take inspiration from what vendors and open‐source communities are doing in other languages, and when you cannot just use it, you can adapt it or make a version specific to your own.

A very good example of this is ASP.NET MVC. About 10 years ago, the popular language of the moment was Ruby, thanks to its simple web framework, Ruby on Rails, built on top of the Model View Controller pattern. The.NET developer community took inspiration from it and started building.NET web frameworks also based on the MVC pattern. This led to Microsoft building the ASP.NET MVC framework that is one of the main topics of this book.

Growing Your Comfort Zone

Using several languages and frameworks brings an additional, if not technical, benefit: It forces you to step out of your existing comfort zone, making you more adaptable and breaking the potential boredom of always following the same routine. Not surprisingly, many developers are hesitant to experiment with new things and prefer the comfort of using the tools, frameworks, and language they know best, even if doing so gives them less flexibility and control. But if you are reading this book, you are probably not one of them. So, through the rest of the book, prepare to learn new languages and frameworks that originated outside of the Microsoft.NET space. In the beginning, you’ll be stepping out of your comfort zone. By the time you are done, you’ll find that your comfort zone has become even larger and more rewarding.

WHO THIS BOOK IS FOR

The audience of this book is web developers who have knowledge of ASP.NET MVC, either with the latest version or with previous versions of the framework, and who want to start using the tools and frameworks that are popular in the front‐end development world. Additionally, this book can serve as a guide for developers who are already adopting some of the front‐end tools and frameworks but want to make a more efficient use of them via the integrations that have been introduced with Visual Studio 2017.

WHAT THIS BOOK COVERS

This book is about front‐end development with ASP.NET Core MVC. Together with giving an overview of the latest framework from Microsoft, it also covers some of the most popular frond‐end frameworks and tools, like Angular, Bootstrap, Nuget, Bower, webpack, Gulp, and Azure.

In addition to the frameworks, the book shows the new front‐end development‐oriented features of Visual Studio 2017 but also how to develop an ASP.NET Core MVC application without it, using instead standard developers’ text‐editors like Visual Studio Code on Mac OS X.

This is not a beginners’ book, so I assume that you know the basics of HTML, JavaScript, and CSS, that you know C# or VB.NET (keep in mind that all samples will be in C#), and that you have already worked with ASP.NET MVC and WebAPI.

HOW THIS BOOK IS STRUCTURED

If you still haven’t decided whether this it the right book for you, in this section I’m briefly going to explain how the book is structured and the content of each chapter.

  • Chapter 1, “What’s New in ASP.NET Core MVC”: The first chapter covers all the new features and the new approach to development brought to the table with ASP.NET Core and ASP.NET Core MVC and in general with.NET 2017. This chapter can be used as a refresher for those who already know the latest version of ASP.NET MVC or as a concise introduction for those who haven’t seen it yet.
  • Chapter 2, “The Front‐End Developer Toolset”: This chapter starts to explore the world of front‐end developers, introducing the categories of tools they use and describing the top players in each category of tools and frameworks.
  • Chapter 3, “Angular in a Nutshell”: The third chapter introduces Angular, the JavaScript framework from Google, explaining its main concepts and the new Angular tools that came with Visual Studio 2017.
  • Chapter 4, “Bootstrap in a Nutshell”: The fourth chapter introduces the CSS framework from Twitter, Bootstrap, and shows how to use it to build responsive websites. The chapter also talks about Less, a CSS pre‐processing language, and its integration with Visual Studio 2017.
  • Chapter 5, “Managing Dependencies with NuGet and Bower”: Managing all those components, both front‐end and server‐side, can be very painful, but luckily there are a few component managers that make it very easy. You can use NuGet to manage.NET server‐side dependencies and Bower to handle them on the client‐side. The fifth chapter explains how to use them, alone and in combination with Visual Studio 2017, and also how to package your libraries for sharing within your company or with the rest of the world.
  • Chapter 6, “Building Your Application with Gulp and webpack”: The sixth chapter is about Gulp and webpack, two build systems that can be programmed with JavaScript. Their integration with Visual Studio 2017 is also explained in this chapter, together with some common recipes used in ASP.NET development.
  • Chapter 7, “Deploying ASP.NET Core”: Once the application is ready, it’s time for the deployment. This chapter uses Azure to show how to implement a continuous deployment flow that integrates testing, build, and deployment.
  • Chapter 8, “Developing Outside of Windows”: One of the main features of the.NET Core stack is that it can also run on Linux and Mac. Microsoft built a cross‐platform IDE, but there are other options too. In this chapter you’ll see how to do all your ASP.NET development on a Mac.
  • Chapter 9, “Putting It All Together”: The last chapter of the book puts together all the concepts and goes through all the steps required to build a modern, responsive website, with integration with third‐party services and authentication via oAuth.

WHAT YOU NEED TO USE THIS BOOK

The book has lot of samples, so the best way to experience it is to try them yourself on your computer. In order to do so, you’ll need either Windows 7, 8, or 10 and you’ll need Visual Studio 2017 Community edition.

ASP.NET Core MVC can also be developed on any text editor on Windows, Mac OS X, or Linux. Microsoft also developed a cross‐platform text editor called Visual Studio Code. You will also need it in order to follow along in Chapter 8 about developing outside of Windows. You can use any of the other compatible text editors, but the commands used and screenshots will be different from the ones used with Visual Studio Code.

The source code for the samples is available for download from the Wrox website at www.wiley.com/go/frontenddevelopmentasp.netmvc6.

CONVENTIONS

To help you get the most from the text and keep track of what’s happening, we’ve used a number of conventions throughout the book.

As for styles in the text:

  • We highlight new terms and important words when we introduce them.
  • We show keyboard strokes like this: Ctrl + A.
  • We show file names, URLs, and code within the text like so: persistence.properties.

We present code in two different ways:

We use a monofont type with no highlighting for most code examples.

We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.

SOURCE CODE

As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at:

www.wiley.com/go/frontenddevelopmentasp.netmvc6

You can also search for the book at www.wrox.com by ISBN (the ISBN for this book is 978‐1‐119‐18131‐6) to find the code. And a complete list of code downloads for all current Wrox books is available at www.wrox.com/dynamic/books/download.aspx.

Most of the code on www.wrox.com is compressed in a.ZIP,.RAR archive, or similar archive format appropriate to the platform. Once you download the code, just decompress it with an appropriate compression tool.

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher quality information.

To find the errata page for this book, go to www.wiley.com/go/frontenddevelopmentasp.netmvc6 And click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors.

If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.