Learning by doing
Hello there! In this post, we're gonna go over the basics of creating your own blogging system using C# specifically .NET Core 3.0. The aim of this series of blog posts is to teach you guys how easy it can be to write your own basic blogging system and hopefully get you inspired to create your own or use this as a base for something much better and or bigger.
Warning: The first few paragraphs are a bit of a backstory as to why I personally wanted my first tutorial series to be on this exact subject. If you are not at all interested in reading through the backstory simply scrolling down.
The Story
Back at a very young age, I could never quite figure out the sense or purpose behind keeping a diary. I obviously knew schoolmates male and female that wrote a daily diary and obviously, in our very young teenage years, all the boys wanted a peek inside one of those to see who our girl schoolmates wrote about and what they thought about us. But even in those days the practice of writing about the events that happened to you and the thoughts that you had only to keep it locked away to yourself seemed... well odd.I thought surely if you write about an experience or a great idea you had you'd want to share it with as many people as possible and get their take on it.I even got a pretty nice leather notebook that was supposed to be a diary gifted to me, but I never got around to it. It just stayed there in a drawer completely empty until it was either thrown out or lost to time.But then only a few short years after we finally upgraded from dial-up internet to broadband. This increased my 1h of internet time to always online. The change was drastic and I was immediately taken in. At first, I did what any 13 years old would do on the internet... I visited flash game websites, watched funny videos, chatted with my friends on MSN Messenger and joined public forums where I could discuss my favorite cartoons, sports teams, movies, and hobbies.
Then I Discovered Blogging
One day as I was reading through a forum I frequented I noticed somebody posted a link and asked people for their opinion on the blog they've just written. Now I've never heard of blogging before this was completely new territory to me. I noticed the long wall of text thought, pff who has time for this immediately closed it and went on with my business.But then something odd happened, the next day when I went back and logged onto my favorite forum I noticed this the post from this guy asking for people's opinion about his blog post was the top post and not only that, it appeared to have gathered more than 300 comments, in a single night. I was confused and at the same time amazed. Obviously, I had to see what the comments were saying, surely they had to be as unimpressed with the great big wall of text this person linked as me.No, it was a mixed bag, some disagreed with the author on the points he was trying to make, some praised him and defended his position, others were there to antagonize him. This was still not enough to convince me so I ignored it and went about my business avoiding the forum post in question. A couple of days passed and the post was still at the top of the forum. That was when finally I decided "fuck it" let's see what all the commotion is about. I started reading, thinking I'll get bored in no time and will skim through it as I did with everything else that I considered too long and boring back then. But then I just kept reading and reading, for the life of me I can't remember after all these years what exactly the post was about or why it drew me in so much but I clearly remember reaching the end of it and feeling disappointed that there was not more.Just like that, I understood it. That was also the first year I learned coding. Now back then we didn't have Youtube and video tutorials that would teach you how to do cool shit online. But we did have manuals, glorious dry boring manuals.The first language I learned was C++ and right after that PHP because I really really wanted to build websites, forums, portals, and whatnot.One of the very first things I've built was a blog of my very own, of course being 13 and not having any disposable income I had no way to host it. But I did know how to open up the ports on our router and host it myself, this of course meant that the blog was only available at certain hours. Not that it would have seen much success regardless, the content was...well it read like it was written by a 13-year-old, because well, it was.I've kept making blogging systems over the years and gotten pretty good at it and now finally after his long and boring story, I want to give some of my knowledge over to you.
Now Let's Get Down To Business
So the very first things you're gonna need are:
- DotNet Core 3.0
- A Code editor (visual studio code should do just fine)
- Your favorite database (I'm gonna be using feel free to switch over to a SQL database if you wish.
Keep in mind this tutorial is meant for intermediate developers, if you've never worked with C# or dotnet that's fine I'll explain as we go along but some prior development knowledge is required, especially stuff like understanding how databases and how to write basic SQL CRUD statements.Okay! now that that's outta our way and you got your tools ready.The very first part we're gonna do is run
the output you're looking for is:
D:\Dayvi\Workspace\Tutorials\Blog series\Part one>dotnet --version
3.0.100
If that is not your output or there's a newer version of Dotnet available then some of the parts of this tutorial might differ from your system.Right then create your workspace folder open a command prompt in it and run
If everything went well you should see:
D:\Dayvi\Workspace\Tutorials\Blog series\Part one>dotnet new mvc
The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
This template contains technologies from parties other than Microsoft, see //aka.ms/aspnetcore/3.0-third-party-notices for details.
Processing post-creation actions...
Running 'dotnet restore' on D:\Dayvi\Workspace\Tutorials\Blog series\Part one\Part one.csproj...
Restore completed in 60,91 ms for D:\Dayvi\Workspace\Tutorials\Blog series\Part one\Part one.csproj.
Restore succeeded.
Now let's open up our Visual Studio code by simply running
directly in the command prompt, the period in that command tells it to open up the current folder in visual studio code. Hit F5 for debugging and wait for a prompt to pop up in Visual Studio code asking you which config you wanna use. Simply select .NET Core and hit Enter.Just like that your project should be up and running in debug mode.You can also run it by opening a command prompt in the folder where your project is and running
> dotnet build
> dotnet run
Or
for more info
Personally I like
dotnet watch run
the best when I'm developing because it recompiles and relaunches your application automatically every time you make and save a change. Aaaaanyhow after you've successfully launched your dotnet MVC application in your preferred method you should see a message like this:
info: Microsoft.Hosting.Lifetime[0]
Now listening on: //localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Now listening on: //localhost:5000
Open up either of these addresses in your favorite browser(visual studio and visual studio code should it automatically for you n a successful launch). You should not be greeted with a welcome screen.Now let's create our folder structure when finished it should look a little something like this:
├───App
├───bin
│ └───Debug
│ └───netcoreapp3.0
│ └───Properties
├───Controllers
├───Models
├───obj
│ └───Debug
│ └───netcoreapp3.0
│ └───staticwebassets
├───Properties
├───Views
└───wwwroot
Nothing too fancy, nothing too crazy. Some of you might be wondering why I didn't simply work with:
well my reason is quite simple, I believe it comes with waaay too many files that I have to delete before I have a clean install. Now let's head back to visual studio code right click on our Models folder (if you're using visual studio make sure to include the newly created folders in your project) and select create new C# Class, if you do not have that option install the recommended extensions for visual studio code or update visual studio code.
Name your class
Model.cs
and edit it to look like this:
using System;
namespaceBlog.Models {
public class Model {
// Everything needs and ID, not explanation required
public string ID{get;set;}
// Will hold the original creation date of the field,
// the default value is set to DateTime.Now
public DateTime Created {get;set;} = DateTime.Now;
// will hold the last updated date of the field
///will initially be set to DateTime.Now, but should be updated on every...update
public DateTime Updated {get;set;} = DateTime.Now;
public bool Deleted {get;set;} = false;
}
}
This will be our base model class that every other model will inherit from, next let's create our post model well use a super inspired and original name and call it, you read?
Post.cs
!
namespace Blog.Models {
public class Post : Model {
public string Title {get;set;}
public int Views {get;set;} = 0;
public string Content {get;set;}
public string Excerpt {get;set;}
public string CoverImagePath {get;set;}
public bool Public {get;set;}
}
}
As you can see we're extending the Model class which means the Post class has all the members that Model has, neato!Remember in my story when I said I felt the first blog I've ever read ended too early? Well..Hope you enjoyed part one of my tutorial series if you have any questions or remarks feel free to contact me at I know this wasn't much information but the next one will have a lot more meat and a lot less story! I just felt it was important to try and draw you in, I hope it worked and if not hey lesson learned am I right?