In today’s digital world, URL shortening services are essential for making lengthy web addresses more manageable and shareable. These services not only enhance the readability of URLs but also provide tracking capabilities and analytics for marketers and developers. If you’re interested in building your own URL shortener, ASP.NET Minimal API offers a streamlined and efficient way to achieve this.
This guide will walk you through the process of creating a URL-shortening application using ASP.NET Minimal API. We’ll cover everything from setting up the project to creating endpoints for shortening and redirecting URLs, all while leveraging the simplicity and performance benefits of minimal APIs.
Whether you’re a seasoned ASP.NET developer or just starting out, this tutorial will equip you with the knowledge to build a robust and scalable URL shortener. Let’s dive in and start building! 🚀
Creating a URL shortening application using ASP.NET Minimal API involves a few key steps: setting up the project, creating services, creating the necessary endpoints for shortening and redirecting URLs, and managing the URL mappings.
Here’s a step-by-step guide:
Step 1: Create a New ASP.NET Minimal API Project
First, create a new ASP.NET Minimal API project using the .NET CLI:
dotnet new web -o ShortenedUrlAPIApp
cd ShortenedUrlAPIApp
Step 2: Define the URL Mapping Model
Create a simple model to store the original URL and its shortened version. Add a new class file named ShortenedUrl.cs:
public class ShortenedUrl
{
public Guid Id { get; set; }
public string LongUrl { get; set; } = string.Empty;
public string ShortUrl { get; set; } = string.Empty;
public string Code { get; set; } = string.Empty;
public DateTime CreatedOnUtc { get; set; }
}
Step 3: Set Up Entity Framework ORM
We’ll use Entity Framework with the MSSQL database for data management to keep the URL mappings data.
MSSQL Database Connection: appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"connMSSQLNoCred": "Server=DESKTOP-HLGBAEE\\MSSQLSERVER2017;Database=ShortenedUrlAPIApp;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=true",
"connMSSQL": "Server=DESKTOP-99Q87I2\\MSSQLSERVER2017;Database=ShortenedUrlAPIApp;User ID=sa;Password=dev123456;MultipleActiveResultSets=true;TrustServerCertificate=true"
}
}
Step 4: Create Services to Generate Unique Codes
Adding a service to generate unique codes for shortened URLs is a crucial step in ensuring that each shortened URL is unique. This service will generate a random string of characters and check if it already exists in the database. If it does, it will generate a new one until a unique code is found.
Step 5: Configure the Endpoints
Open the Program.cs
 file and set up the endpoints for shortening URLs and redirecting to the original URLs.
Step 6: Run the Application
Run the application using the .NET CLI:
dotnet run
Building a URL shortener using ASP.NET Minimal API showcases the power and efficiency of modern web development tools. With minimal boilerplate code and straightforward configurations, ASP.NET Minimal API allows you to create a functional and scalable application quickly. This tutorial has walked you through setting up your project, defining the necessary models, creating endpoints, and handling URL mappings in the MSSQL database.
By leveraging ASP.NET Minimal API, you gain the benefits of a lightweight framework that is ideal for microservices and small-scale applications. You can easily expand upon this foundation to include features like persistent storage, custom URL slugs, and detailed analytics. The skills and concepts covered here can also be applied to a wide range of other web development projects.
Now, you have a fully functional URL shortener that demonstrates the capabilities of ASP.NET Minimal API. Continue to experiment, optimize, and build upon this project to meet your specific needs.
Happy coding! 🚀
đź‘‹ .NET Application Collections
🚀 My Youtube Channel
đź’» My Github
👉 Full Project