Dotnet Autosave Blog Posts

So for the past few nights I've been working on a way to auto save blog posts, so that I don't loose what I've written when I'm logged out. I've had success with solving the problem of that functionality tonight so it's a good way to start the first blog post. 

The solution was a whole lot easier than what I was initially trying to do, making it too complicated. I am going to assume you understand ASP.NET Core and Ajax when I talk in this post but even if you don't, it's okay. The first problem I happened upon at the start of the night was that dotnet was completely ignoring my logic yet everything looked fine in the frontend and backend so I was a bit confused. What really helps when trying to solve a problem is to go away from the computer for a while, do something else and think about the problem. When I came back to the computer, I realised I had already sort of thought about this when I first started this project, with the save draft link. 

In layman's terms, when you click on the save draft link, entity framework core creates a new post and returns the ID to the frontend. This was where I had to think for a bit. The easiest solution was to use localStorage, and store the post ID in the browser. The engineering behind the auto post functionality is a timer, which is very easy to do in JavaScript. Using setInterval, you can periodically save changes to the database through Ajax. I think it kept returning an error which I had set up with the try / catch block for NullReferenceException to say "Post not found". It was strange but then I realised that this is an update it's sending to the server, and it was missing the PostID. Thankfully all is working as expected now and I can start writing blog posts properly! 

Thanks for reading!