Writing REST API error handling my way ?

Taking time to work more on my side project, I stumbled upon the usual issue on how to manage error handling globally on the API. I usually would make my own structure and call it a day, but I thought it would be nice to review how other people are doing it (especially since I’m doing it in c#, a language I’m not proficient with). So I started like everyone would with {json:api}....

08 February, 2019 · 3 min · 558 words · Alexandru Bucur

JSON.NET append to existing key

A quick example for appending to an existing key in a JSON.NET object, since for me at least it wasn’t clear in the documentation. 1 2 3 4 5 JObject myJson = new JObject(new JProperty("errors", new JObject())); /// we can now reference it as follows just match the type myJson["errors"].Value<JObject>().Add(new JProperty("title", title)); Small update: You can even add multiple entries to an JArray the same way 1 2 /// assuming errors is a JArray, this will automatically append new entries ErrorReponse["errors"]....

08 February, 2019 · 1 min · 84 words · Alexandru Bucur

Simple JSON API ASP .NET Core Error handling class

As a follow up to my investigation on API Responses I’ve decided to not create yet another random format and just stick to JSON API even if it means more parsing on the front-end. That being said, ASP .NET Core’s standard response format for ModelState validation doesn’t follow the convention, so I’ve written a small helper class. Unfortunately in .NET Core 2.0 I couldn’t figure out if there’s a way of overwriting the default response format (maybe with an extension method?...

08 February, 2019 · 3 min · 476 words · Alexandru Bucur

How to get the user id after registering an user in ASP .NET Core Identity

Getting back to work on my side project I got into finishing the user registration trough the API. Following the JSON API spec, it requires returning a 201 created and the resource id when creating a database entry. For somebody used with ASP this might seem trivial but it’s not that obvious when creating a new user. Let’s take a simple example: 1 2 3 4 5 6 var user = new ApplicationUser { UserName = model....

08 February, 2019 · 1 min · 149 words · Alexandru Bucur

Dynamic Policy Claims in ASP.NET Core using JWT Tokens (and Role Claims)

Hi guys, as a developer I bet everybody is a little bit ’lazy’ and likes optimizing their workflow. My latest aha moment of laziness was when I was going trough my pet project and working on implementing Policy-based authorization and deciding that adding AddPolicy every single time I want to implement a new Role Claim in the database is counter intuitive. I want to say a big thank you to Jerrie Pelser for the initial work on this....

27 January, 2019 · 4 min · 711 words · Alexandru Bucur

How to rename Asp .Net Core 2.2 Identity Tables to not have AspNet prefix in EF Core

Not sure if the code below should be considered a hack, but the easiest way of removing the AspNet prefix is to iterate through the models. 1 2 3 4 5 6 7 8 9 10 11 12 protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { var table = entityType.Relational().TableName; if (table.StartsWith("AspNet")) { entityType.Relational().TableName = table.Substring(6); } }; } Found the initial code in a 2014 asp net issue and since copy paste didn’t cut it, did a quick update of the method / property names....

16 December, 2018 · 1 min · 109 words · Alexandru Bucur

The confusing way of sending emails in .NET Core and other goodies

Since I finally had some time to play more with C# I’ve encountered my next challenge when wanting to implement user registration with a confirmation email. Even though the official documentation is clear on how to handle the one time code generation I didn’t want to use Sendgrid but plain old SMTP authentication. Further more I don’t agree with having the mail body as a string in my registration task....

08 December, 2018 · 2 min · 290 words · Alexandru Bucur

ps aux output in cronjobs

Hello there, Going trough an ad-hoc cronjob for checking an existing process I’ve hit a weird case that the command itself was working well when ran from the terminal, but failed miserabely when the cronjob ran it. The command was something along the lines of: 1 ps aux | grep 'php artisan a_specific_command --with-some-long-parameters-that were generated' | grep -v grep The thing that I’ve completely forgotten is that by default ps’s output is limited by the number of $COLUMNS....

22 September, 2018 · 1 min · 161 words · Alexandru Bucur

Static / Fixed filenames for generated vue-cli builds

As you may know, the release of vue-cli 3 is getting close (currently at RC3 status). I really like the streamlined way of starting projects and having a good baseline for development (especially when trying to setup a good starting point in house, that has documentation and it’s actively developed). However the default setup isn’t friendly with legacy projects because vue-cli implicitly adds a hash to the generated filenames. That’s great if you’re starting a new project/SPA because it’s like a built in cache buster but doesn’t help if you’re trying to integrate it with your favourite c#/php/python/ruby etc application....

04 July, 2018 · 2 min · 277 words · Alexandru Bucur

Quick and easy way of counting UTF-8 characters in Javascript

Reading the following tutorial regarding a VueJS component that displays the character count for a textarea got me thinking. You see, the problem is that when Javascript was first created it didn’t had proper UTF-8 support. Javascript’s internal encoding is UCS-2 or UTF-16 depending the articles you find on the internet. (actually there’s an awesome article from 2012 that explains this in detail ) . What does that mean you say ?...

18 June, 2018 · 2 min · 225 words · Alexandru Bucur