A simple and efficient URL shortening service built with ASP.NET Core, Entity Framework, and Redis caching.
- Shorten long URLs into compact 7-character codes
- Redis caching for fast redirects
- SQL Server database for persistent storage
- Automatic unique code generation
- .NET 8.0 or higher
- SQL Server
- Redis
Update your appsettings.json with the following connection strings:
{
"ConnectionStrings": {
"DefaultConnection": "Your SQL Server connection string",
"Redis": "Your Redis connection string"
}
}Creates a shortened URL from a long URL.
Endpoint: POST /api/shorten
Request Body:
{
"url": "https://example.com/very/long/url/that/needs/shortening"
}Response:
https://yourdomain.com/api/abc123X
Status Codes:
200 OK- URL successfully shortened400 Bad Request- Invalid URL format
Redirects from a short code to the original long URL.
Endpoint: GET /api/{code}
Example:
GET /api/abc123X
Response:
- Redirects to the original long URL
- Uses Redis cache for improved performance (10-minute TTL)
Status Codes:
302 Found- Successful redirect404 Not Found- Short code does not exist
curl -X POST https://yourdomain.com/api/shorten \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/long-url"}'Simply visit the shortened URL in your browser or make a GET request:
curl -L https://yourdomain.com/api/abc123XShortenedUrls Table:
Id(Guid) - Primary keyLongUrl(string) - Original URLShortUrl(string) - Complete shortened URLCode(string, 7 chars) - Unique short code (indexed)CreatedOnUtc(DateTime) - Creation timestamp
- Redis caching reduces database queries for frequently accessed URLs
- Cache TTL: 10 minutes
- Unique code generation with collision detection