Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

Shortener

> 数据库
Open source

Shortener Url as a service in ASP.NET Core and Actor Model

178 stars0 likes2 views
WebsiteGitHub

About

Shortener Url as a service in ASP.NET Core and Actor Model

Shortener Service

This is a URL shortener service implemented in ASP.NET Core and MongoDB. It allows users to shorten long URLs into shorter, more manageable ones.

Features

  • Shorten long URLs into unique, shortcodes
  • Redirect users from short URLs to original long URLs
  • MongoDB database for persisting data
  • RESTful API for easy integration with other applications
  • Metrics

Technologies Used

  • ASP.NET Core
  • MongoDB
  • C#
  • Minimal APIs
  • InMemory Cache
  • Open Telemetry
  • Prometheus

Installation

  1. Clone the repository:
bash
git clone https://github.com/thisisnabi/Shortener.git

Give a Star! ⭐

If you find this project helpful or interesting, please consider giving it a star on GitHub. It helps support the project and recognizes the contributors.

Getting Started

To start with the URL shortener service, follow the installation instructions in the Installation section above. Once the service is up and running, you can begin using the API endpoints to shorten URLs, track statistics, and manage your shortened links.

Problem

The primary problem with long URLs is their lack of user-friendliness and practicality in various contexts. Long URLs can be cumbersome to manually input, difficult to remember, and can exceed character limitations imposed by certain platforms, such as social media posts or text messages.

This can result in broken links, truncated URLs, or user frustration when attempting to share or access lengthy URLs in constrained environments.

Solution

To address the problem of long URLs and make them more manageable for users, a URL shortener service provides an effective solution. By condensing lengthy URLs into shorter, more concise forms, users can easily share and access links across various platforms and communication channels.

Shortening URLs

Implement a URL shortening algorithm to generate unique, shortcodes or aliases for long URLs. This algorithm should produce compact shortcodes that are unlikely to collide with existing codes in the system.

csharp
public async Task GenerateShortenUrlAsync(string destination, CancellationToken cancellation)
{
    var shortenCode = GenerateCode(destinationUrl);

    var link = new Link
    {
        CreatedOn = DateTime.UtcNow,
        DestinationUrl = destinationUrl,
        ShortenCode = shortenCode
    };

    await _dbContext.Links.AddAsync(link, cancellation);
    await _dbContext.SaveChangesAsync(cancellation);

    return $"{_appSettings.BaseUrl}/{shortenCode}";
}

Redirection Mechanism

Set up a redirection mechanism that maps incoming requests for shortened URLs to their corresponding long URLs. When a user accesses a shortened URL.

csharp
app.MapGet("/{short_code}", async (
    [FromRoute(Name = "short_code")] string ShortCode,
    ShortenUrlService shortenService,
    CancellationToken cancellationToken)
=> {
    var destinationUrl = await shortenService.GetDestinationUrlAsync(ShortCode, cancellationToken);

    return Results.Redirect(destinationUrl);
});

The service should redirect them seamlessly to the original destination URL without any noticeable delay.

Metrics

By systematically capturing and analyzing these metrics, marketing teams can gain insights into the effectiveness of their campaigns, identify potential issues, and optimize their strategies to enhance user engagement and achieve better outcomes.

…

License

This project is licensed under the MIT License: MIT License.

Stay Connected

Feel free to raise any questions or suggestions through GitHub issues.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#asp-net-corecsharpdotnetmongodb

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库