loading...

Getting Started

douglasstarnes profile image Douglas Starnes Updated on ・5 min read

For the April 2020 Memphis .NET User Group meeting, I created an ASP.NET Core Razor Pages application. It's called Quanda, which is a portmanteau of Question and Answer. The idea is inspired by Stack Overflow where people can ask questions and vote on answers. The full source code is available on Github under an MIT license.

For how many posts it takes, I'll explain how Quanda works and what features of ASP.NET Core Razor Pages it uses.

To get setup with ASP.NET Core Razor Pages you'll need to install some software. This is all free, open source, cross platform and officially supported by Microsoft.

.NET Core SDK

To build and run ASP.NET Core applications, you'll need to have the .NET Core SDK. This can be found on the Microsoft .NET home page. Click the Download button to go to the download page. The page will recognize the platform you are using and suggest an appropriate download. On Windows, make sure to get the .NET Core SDK and not the .NET Framework SDK. The .NET Framework is Windows only and has some API differences with .NET Core. On macOS there is only one option. For Linux, follow the instructions to install the .NET Core SDK using the package managers for your flavor of Linux.

After installing the SDK, open a terminal or command prompt. You should be able to run the command:

$ dotnet --info

And see details about the .NET Core SDK you just installed. You'll be using the command line interface (CLI) tool a lot in to build Quanda in lieu of the Visual Studio wizards. If you got this far, you're ready to move onto the editor.

Visual Studio Code

In the past, to write .NET apps, you had to use Visual Studio, Microsoft's flagship Integrated Development Environment. And while Visual Studio offers fantastic support for .NET Core development, it's also tied to Windows. There is a version for macOS but it's still catching up to Visual Studio on Windows. And there is not version for Linux. That's why Microsoft created Visual Studio Code, a programmer's text editor inspired by Visual Studio.

Visual Studio Code, like the .NET Core SDK, is free, open source and cross platform. You can download it by going to the Visual Studio Code home page. Again, the page will detect the platform you are on and provide a link for an installer. Make sure to get the stable build of Visual Studio Code and not the Insider Build.

VS Code Extensions

Out of the box, so to speak, VS Code doesn't do a lot. The power of extensions make VS Code a tool worthy of almost any project, including .NET Core. Extensions can be installed using the Extensions pane in the sidebar. Press Ctrl-Shift-X (or Cmd-Shift-X on macOS) to display it. The in the text box at the top, type the name of the extension you want to install. For Quanda, I used several extensions that I recommend you install as well as I'll be referencing them in the walkthrough.

  • C# - This extension, from Microsoft, provides tooling support for the C# language and .NET Core projects. With the C# extension installed, VS Code will gain syntax highlighting, Intellisense, quick fixes, snippets, and an integrated debugger.

  • NuGet Gallery - Even though this extension by Patryk Cislo is in preview, I feel good about recommending it. In Visual Studio, you install NuGet packages by searching for them in a graphical interface. While installing NuGet is possible through the dotnet command installed with the .NET Core SDK, this extension makes it much easier.

  • SQLite - You will use the SQLite database to store data for Quanda. I'll explain more about SQLite in a later post. For now it's enough to know that it's a file-based database instead of a server. So if you can crack open the database file, you can inspect the data. There is a command line application which does this or you can install the SQLite extension by Alex Covizzi. This lets you query a SQLite database and see the results with leaving VS Code.

PowerShell Core (optional)

As mentioned before, you'll be doing a lot of work with CLI tools when developing with VS Code. That means you will have to contend with differences across shells. Windows has the Windows Command Prompt, macOS uses zsh as the default and Linux has a number of options but generally it's bash. Most of the same commands work with no difference across bash and zsh. I've tried using a bash shell emulator on Windows and while it works, there are some subtle weird results. However, if you use PowerShell Core, shell differences across platform are not a concern any more.

PowerShell Core is a cross-platform and open source implementation of the Windows command line shell, PowerShell. It's also a scripting language built on .NET Core. You'll see more of PowerShell Core throughout the rest of this series but to get started go to the PowerShell documentation and follow the instructions for your platform.

With PowerShell Core installed, open Visual Studio Code. You can press Ctrl-Backtick to bring up a terminal window below the editor. By default this will be default shell on the system. However, you can change it by pressing Ctrl-Shift-P (Cmd-Shift-P on macOS) which brings up the command bar at the top of the editor. Search for the command Select Default Shell and press Enter to execute it. A list of available shells will pop up. Select the option for PowerShell Core. Now the next time you open the terminal window, the shell will be PowerShell Core.

Creating an ASP.NET Core Razor Pages application

Inside of Visual Studio Code, with a terminal window open, run the command

$ dotnet new -h

The -h option will display the help for the dotnet new command. The help contains a list of templates that can be used to scaffold a wide range of projects. The one you are going to use for Quanda is the ASP.NET Core Web App which has a short name of webapp. Now in PowerShell run the command

$ dotnet new webapp -o Quanda

The -o option will do two things. First it will use Quanda as the name of the project. Second it will create a new directory named Quanda and put the scaffolded project in the directory. Change to the Quanda directory

$ cd Quanda

And run this command to restart VS Code in the current window opening the current directory (specified with the .) in the project Explorer pane.

$ code -r .

VS Code will recognize that this is a .NET Core project and display a notification in the lower right of the editor. It asks if you want to add debugging assets to the project. Generally, you will want to allow this as it makes debugging applications from with the editor much easier. Click Yes and a .vscode directory will be added to the project. This is provided by the C# extension.

To actually run the application simply execute the command

$ dotnet run

Then open a web browser and go to https://localhost:5001/. The browser might complain about an invalid SSL certificate but since the application is running locally, you can ignore that. And you'll see the minimal ASP.NET Core Razor Pages app that generated.

In the next post, you'll learn how the structure of an ASP.NET Core Razor Pages app is laid out. See you next time!

Discussion

markdown guide