FEATURED POSTS
PAGES
Finally, the day is here. I'm jumping into ASP.NET Core 2. It's been ages since I've been on the Microsoft platform. I used to admin Windows NT servers and write ASP code (called Classic today) many, many moons ago.
For a bit now I've wanted to jump into this because I use C# for game programming and really just wanted to see what it was like to develop apps in this ecosystem.
My biggest headache was trying to figure out just what in the heck all the .NET stuff was about. I'd hear ".NET framework" and ".NET core" and "ASP.NET" and "ASP.NET Core" and so on. For many, many days, I'd yell at the screen, "I just need a darn starting point!"
Finally I started carving out time to look through docs and some posts and got a handle on what was what. And so today, I'm proud to announce that the ASP.NET Core 2 training has begun! If you're new or curious, please read on and follow along my journey as it may help you out.
DISCLAIMER
I'm not a .NET pro (not yet muhahaha). As of this writting, I'm working overtime to get up to speed and enjoy this ecosystem, but it's a process. If you see anything here that's completely wrong, please let me know so I can fix it.
At the end of the day, these are my notes to help out in case future me forgets something. And it's also a great way to see growth. :)
WHAT IS ASP.NET CORE?
In short, .NET Core is an open-sourced, cross-platform framework for building web, device and IoT apps. It was rebuilt from the ground up to be modular, scalable and portable.
Source: ASP.NET
You can run a .NET Core application on IIS or Linux and you can develop apps on Windows, Mac or Linux (using Visual Studio, Visual Studio Code, Sublime Text, etc).
From what I've learned thus far, by chosing .NET Core over the full .NET framework, you gain modularity, smaller footprint, performance boost and running your apps where you see fit.
The biggest loss seems to be that the available dependencies are less than the full .NET framework, but this is changing as .NET Core matures.
GETTING STARTED
Let's start by bookmarking the offical docs: .NET Core Documentation.
Next, we need to get the .NET SDK. Microsoft has prepared a quick 10-minute Hello World tutorial to get us going.
Get the .NET SDK here. -> redirects to a getting started page.
On this page, you'll download the SDK, make a Hello World app in the console and run it.
If you're new to C#, there's also a link at the end to a quickstart guide for the language. In case you missed it: C# Quickstart: Collections.
At the time of this writing, I installed version 2.1.402. To see the version, type this in the console:
dotnet --version
MY IDE CHOICES: Visual Studio & Visual Studio Code
I use Visual Studio and Visual Studio Code a lot in my day-to-day. Over the years I've flopped around (Sublime Text, IntelliJ, WebStorm, Brackets, Atom, etc), but since 2017 I've been mostly using these two IDEs. For the past couple of years at work, I used IntelliJ more frequently because I was supporting a 1,000,000+ line PHP-heavy legacy app and the JetBrains IDE worked well for the task.
All that said, for my ASP.NET Core tasks, I'll be using Visual Studio 2017, which is my preferred full IDE. I will configure VSC as well because I love it too. Good job Microsoft.
I'll provide a little getting-started info for Visual Studio Code, but the meat of my getting-going instruction will take place in Visual Studio.
Visual Studio
If you're following along and don't see any .NET options in Visual Studio under projects, make sure you open the Visual Studio Installer (the new projects window should give you a link to it) and scroll through and select the .NET stuff along with anything else you want (for example, I have UNITY3D stuff checked).
To setup a new project (at the time of this writing):
- open Visual Studio
- go to "New Project"
- go to Visual C# > .NET Core (or type "asp.net core" in the search)
- select "ASP.NET Core Web Application"
- select your project template/requirements (see screenshot)
- select .NET Core
- select ASP.NET Core 2.1
For my first API, I selected Empty. While this means I get an empty project, I though it would help me understand more of what's going on as I follow along with an example.
Visual Studio Code
To use VSC, you basically just need the C# extension from the marketplace.
If you are setting up VSC for the first time, I built an extension (Fast Start - frozenrelic) that has all the things I like in VSC + settings ready to go in 1 click. Just install it and copy the JSON to your user settings (see extension for more details).
When I get a new machine, I just install this single extension and my Visual Studio Code is ready to rock n roll right away. I use it for C# (Unity, .NET Core now, etc), JavaScript, Docker, NPM tasks, Python, PHP, LUA, etc.
I'll keep updating it as I go along. The README for the extension lists out everything it installs and why, so if you're not interested in GO Lang for example, you know exactly what to uninstall if you're trying to keep your installation lean.
To setup a new project:
- open Visual Studio Code
- open terminal and type "dotnet new console"
- open the folder in VSC
- Edit as desired.
FIRST APP
One you setup an empty ASP.NET Core 2 app, you'll see two C# files: Program.cs and Startup.cs.
Program.cs: This is the starting point of the program (hosting,etc). It's been made cleaner since 1.0 I see.
Startup.cs: This is the app's entrypoint. From here you can add & configure services (components).
If you look at Startup.cs, you'll see the following line:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
These two dependencies are injected and can be used by your app. For example:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
Out of the box, this "empty" app actually works.
To run the app in Visual Studio, just click on run IIS Express and it will open up localhost:port in your default browser.
You'll see "Hello World!" as the response for all endpoints you try.
MIDDLEWARE NOTE
I was just looking up some information about middleware for the API I'm building and I wanted to note that the order in which you add middleware matters.
For example, adding authentication after other things that shouldn't be run unless the request is authenticated would result in a poorly-constructed & broken pipeline.
Also, ASP.NET Web API and ASP.NET MVC were merged into ASP.NET Core MVC, so now there's one middleware for web services and presentaion.
But more on this next time when I get into adding the dependency to an app.
WHAT'S NEXT?
Right now I'm building out an API, so for my next article, I'm going to talk about adding in dependencies (specifically the MVC middleware) as well as sprinkle in any discovery notes I might have.
Until next time, keep coding!