URL Shortener with ASP Core
Why Do We Need to Shorten Our URLs?
There are many cases where shortening our URLs can prove useful. Let’s quickly look at some of those. Firstly, as mentioned in our introduction, when our URLs become too long, they become unreadable and difficult to share. Therefore, one reason why we might want to shorten our URLs is to make them easy to read and share.
Also, imagine a scenario where we manually type a long URL, for example, one with over fifty characters. Such a process would be tedious, error-prone, and time-consuming, as we would have to be careful not to mix up the characters. Shortening our URL to fewer characters can prove very useful in such circumstances. It makes our typing task simpler, less error-prone, and faster.
Furthermore, long URLs are susceptible to breaking when we copy them from one medium and paste them into another. With short URLs, we can prevent potential link breakages from occurring.
How Will Our URL Shortener Work?
Now that we have seen some reasons for shortening our URLs, let’s discuss how our URL shortener will work.
To build our URL shortener, we will utilize an ASP.NET Core Web API to receive and handle user requests. The web API will receive long URLs via a POST
request and then generate a short unique code for it. To store this shortcode and the long URL it points to, we will utilize an in-memory dictionary. When we append this shortcode to our base URL we get the shortened URL.
Subsequently, when users send a GET
request to the shortened URL, we can then use the shortcode to retrieve its corresponding long URL from the dictionary.
We should note that our URL shortener app will only support creating and retrieving URLs. We won’t add functionalities for deleting or updating the shortened URLs.
Build Our URL Shortener
Next up, let’s implement the URL shortener. We will carry out this implementation in two parts. First, we will create our service class. Then, we’ll add the web API controllers.
Create the Service Class
Our service class will have two methods – one for retrieving a shortcode when we pass a long URL to it and another for retrieving a long URL via a shortcode.
Before we define these methods, let’s first create the class and add some helper members to it:
These initial members play a crucial part in our URL shortener service.
Our first readonly field, _chars
, gives us a set of alphanumeric characters that we will combine randomly to get a unique code for a long URL.
Next, we define the dictionary that will serve as the database for our application. Finally, we create the GenerateShortCode()
method that will create a shortcode of 5
characters for any long URL we wish to shorten.
With that, we can now implement the service methods.
Create URL Shortener Methods
First, let’s define a GetShortCode()
method:
Here, we first check if a URL already has a shortcode in our database. If it does, we simply return it.
Otherwise, we create one for it in the while
loop using the GenerateShortCode()
method and then add it to our dictionary. In the rare scenario where the shortcode we create already exists in the dictionary, we execute this loop more than once. We run the loop until we have successfully added a shortcode and its main URL to the dictionary.
Next, let’s implement a GetLongUrl()
method:
For this method, we invoke the TryGetValue()
method to retrieve the long URL for a specific shortcode. If the shortcode exists in our database, we retrieve its value and return it. Otherwise, we return the default value for strings.
Now, for us to use this service we just defined in our controller, we have to add it to the Dependency Injection(DI) container in our Program
class:
Here, we register our service as a singleton, which means that we will create and use only one instance of this service throughout the lifetime of our app.
Create the URL Shortener API Endpoint
Now, with our service class ready for use, let’s add a controller to our project, injecting our IUrlShortenerService
interface:
Next, let’s add an action method for shortening URLs to this controller:
We use the TryCreate()
method to check if the long URL our user provided is valid. If it is, we shorten it and return the shortcode in the response body. But if it isn’t, we return a 400 BadRequest error.
With that, let’s add an action method that will return the main URL when a user provides a shortened URL:
In this method, we call the GetLongUrl()
service method to retrieve the corresponding long URL. If we successfully get the URL, we redirect our user to it. Otherwise, we return a 404 NotFound response.
Finally, let’s add a third method that we will use to check if we have shortened a URL correctly and that the shortened URL points to the correct long URL:
Great! Our controller is now complete.
With this controller, we will handle all incoming URL shortening requests from our users, call the necessary service methods to shorten the URLs, and return unique shortcodes to our users. Additionally, if a user visits a shortened URL address, we will retrieve and return the original URL to them.
Test the URL Shortener
Now, it’s time to check if the functionalities in our application are working.
First, let’s try to shorten an internal URL. Let’s add the https://localhost:7075/samplepage
URL as the body of the POST
https://localhost:7075/createshorturl
request and check the result.