Book Image

.NET Core 2.0 By Example

By : Neha Shrivastava, Rishabh Verma
Book Image

.NET Core 2.0 By Example

By: Neha Shrivastava, Rishabh Verma

Overview of this book

With the rise in the number of tools and technologies available today, developers and architects are always exploring ways to create better and smarter solutions. Before, the differences between target platforms was a major roadblock, but that's not the case now. .NET Core 2.0 By Example will take you on an exciting journey to building better software. This book provides fresh and relevant content to .NET Core 2.0 in a succinct format that’s enjoyable to read. It also delivers concepts, along with the implications, design decisions, and potential pitfalls you might face when targeting Linux and Windows systems, in a logical and simple way. With the .NET framework at its center, the book comprises of five varied projects: a multiplayer Tic-tac-toe game; a real-time chat application, Let'sChat; a chatbot; a microservice-based buying-selling application; and a movie booking application. You will start each chapter with a high-level overview of the content, followed by the above example applications described in detail. By the end of each chapter, you will not only be proficient with the concepts, but you’ll also have created a tangible component in the application. By the end of the book, you will have built five solid projects using all the tools and support provided by the .NET Core 2.0 framework.
Table of Contents (16 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Creating simple running code


Let's create our very first .NET Core 2.0 application. We will create it using Visual Studio Code as well as Visual Studio 2017 in Windows, and with Visual Studio Code in Ubuntu.

The .NET Core 2.0 SDK installs the templates for creating the class library, console, web, MVC, razor, web API, and so on, for applications based on .NET Core 2.0. As our first application, we will create a simple MVC application on .NET Core 2.0, and get familiar with the application code and the .NET Core 2.0 command-line integration.

Let's start by creating this application in Windows using Visual Studio 2017, and then we will create the same application from Visual Studio Code, first in Windows and then in Linux.

Creating an application in Windows through Visual Studio 2017 version 15.3

For this, we need to perform the following steps:

  1. Open Visual Studio 2017.
  2. Go to File | New | Project. In the New Project dialog, you should see the .NET Core template inside Visual C#:
  1. Click on .NET Core and select ASP.NET Core Web Application.
  2. Name the project HelloDotNetCore2 and click OK.
  1. It will show a New ASP.NET Core Web Application dialog. Ensure .NET Core and ASP.NET Core 2.0 are selected in the two dropdowns displayed in this dialog, as we are discussing .NET Core 2.0 here. The first dropdown signifies the target framework of the application and the second dropdown is the version of ASP.NET Core that we are using. You have the option to choose .NET Framework as the target framework in the first dropdown, but then the resulting application would not be cross-platform. If the application has to be cross-platform, it should target .NET Core. The second dropdown has the different versions of ASP.NET Core that have been released so far, such as 1.0, 1.1, and 2.0. We will keep it as ASP.NET Core 2.0. You will also notice that with ASP.NET Core 2.0, the number of templates has increased from the previous version. Apart from Empty, Web API, and Web Application, which were present in ASP.NET 1.1, we also have templates for Web Application (Model-View-Controller),  Angular, React.js, and React.js and Redux. We will choose Web Application (Model-View-Controller) as our template. There is support for a Docker container but let's keep it unchecked for the time being. We will discuss Docker in detail in a later chapter. Also, keep the Authentication as No Authentication. We will explore authentication options in detail in a later chapter. Click OK:

  1. And voila! Visual Studio creates the HelloDotNetCore2 project for us and restores the required packages to build in the background. You can check this by inspecting the Package Manager Console output. Your very first ASP.NET Core 2.0 is ready to be run:
  1. Click Debug or press F5 to run the application. We will see a detailed walk- through of all the project artifacts in the next chapter.

Creating application in Windows through Visual Studio Code

Visual Studio is the IDE and is aware of projects and templates. As stated earlier, Visual Studio Code is a file and folder-based editor and hence it is not aware of projects and templates. So to create the same application through Visual Studio Code, we will make use of the .NET command line. Let's get going!

Open Visual Studio Code, go to View, and click on TERMINAL. It will open Command Prompt/the PowerShell terminal in the bottom section of Visual Studio Code. If you see the PowerShell terminal, type cmd so that it turns into Command Prompt. This is an optional step if you are comfortable with PowerShell.

Let's try and explore the commands available to create a new project, so let's type dotnet --help . This is the help command for dotnet and will let us know about the options that we have available for creating a .NET Core 2.0 MVC application as shown in the following screenshot:

The SDK lists all the possible commands and options that can be used with explanations. So keep in mind, any time you need any help with .NET commands, SDK is there to help us. Just ask for help by typing dotnet --help in the terminal.

From the command list, it looks like the command of interest to us is the new command, as its description reads that this command initializes .NET projects.

So, let's ask SDK how to use the new command by typing dotnet new --help. This will let us know the command that we need to run to create a new MVC application:

Based on the preceding help text, let's enter the following command:

dotnet new mvc -lang C# -n HelloDotNETCore2

This will create a new MVC project named HelloDotNetCore2 in the C# language, in the folder named HelloDotNetCore2 at the location of the terminal. Now let's build and run the application by typing the following commands:

cd HelloDotNetCore2
dotnet build
dotnet run

The first command navigates to the newly created folder, HelloDotNetCore2. Then, we build the application with the second command and run it through the third command. The dotnet build command is just to show that we have a build command as well. The dotnet run command actually builds and runs the application. Now, go to the browser of your choice and navigate to http://localhost:5000 to see the application running in your browser:

Alternatively, you can go to the Explorer view in the activity bar, open the HelloDotNetCore2 folder, and press F5. This will also build the application and launch it in the browser.

Note

Steps for creating the application in Ubuntu through Visual Studio Code are the same as in Windows, but instead of Command Prompt, we have Bash.