TinyLinker 1.0.0

dotnet add package TinyLinker --version 1.0.0
                    
NuGet\Install-Package TinyLinker -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TinyLinker" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TinyLinker" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TinyLinker" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TinyLinker --version 1.0.0
                    
#r "nuget: TinyLinker, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TinyLinker@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TinyLinker&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=TinyLinker&version=1.0.0
                    
Install as a Cake Tool
# TinyLinker

TinyLinker is a .NET package for creating and resolving short URLs, complete with click tracking and expiry support.
It uses SQL Server to store URL data and integrates seamlessly with ASP.NET Core using dependency injection (DI).

---

## Features

- Shorten long URLs into short codes
- Resolve short URLs to the original URL
- Track click count automatically
- Expiry support for URLs
- Fully async and DI-ready
- Compatible with SQL Server

---

## Table of Contents

1. [Installation](#installation)  
2. [Database Setup](#database-setup)  
3. [Registering TinyLinker in ASP.NET Core](#registering-tinylinker-in-aspnet-core)  
4. [Configuration Options](#configuration-options)  
5. [Usage](#usage)  
6. [Example Controller](#example-controller)  
7. [Optional Cleanup](#optional-cleanup)

---

## Installation

Using NuGet:

```bash
dotnet add package TinyLinker
```
---

## Database Setup

Create the table to store URLs:

```sql
CREATE TABLE [dbo].[TinyLinks] (
    [Id] INT IDENTITY(1,1) PRIMARY KEY,
    [Code] NVARCHAR(50) NOT NULL UNIQUE,
    [OriginalUrl] NVARCHAR(MAX) NOT NULL,
    [ClickCount] INT NOT NULL DEFAULT 0,
    [CreatedAt] DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME(),
    [ExpiryAt] DATETIME2 NULL
);

CREATE UNIQUE INDEX IX_TinyLinks_Code
ON [dbo].[TinyLinks] ([Code]);

CREATE INDEX IX_TinyLinks_ExpiryAt
ON [dbo].[TinyLinks] ([ExpiryAt]);
```

**Columns Explanation:**

| Column      | Type           | Description              |
| ----------- | -------------- | ------------------------ |
| Id          | INT IDENTITY   | Primary key              |
| Code        | NVARCHAR(50)   | Short URL code (unique)  |
| OriginalUrl | NVARCHAR(MAX)  | Full/original URL        |
| ClickCount  | INT            | Number of times resolved |
| CreatedAt   | DATETIME2      | Creation timestamp       |
| ExpiryAt    | DATETIME2 NULL | Optional expiry date     |

---

## Registering TinyLinker in ASP.NET Core

In `Program.cs`:

```csharp
using TinyLinker.Extensions;

builder.Services.AddTinyLinker(options =>
{
    options.ConnectionString = builder.Configuration.GetConnectionString("Default");
    options.CodeLength = 7;                   // Length of short code
    options.Expiry = TimeSpan.FromDays(10);   // Expiry duration
    options.Schema = "dbo";                    // SQL schema
    options.UrlTableName = "TinyLinks";        // Table name
});
```

---

## Configuration Options

| Option             | Type     | Description                      |
| ------------------ | -------- | -------------------------------- |
| `ConnectionString` | string   | SQL Server connection string     |
| `CodeLength`       | int      | Length of short URL code         |
| `Expiry`           | TimeSpan | Duration after which URL expires |
| `Schema`           | string   | SQL schema (default `dbo`)       |
| `UrlTableName`     | string   | Name of the table storing URLs   |

---

## Usage

Inject `ITinyLinkerService` into your controller or service:

```csharp
private readonly ITinyLinkerService _tinyLinker;

public MyController(ITinyLinkerService tinyLinker)
{
    _tinyLinker = tinyLinker;
}
```

### Shorten a URL

```csharp
var code = await _tinyLinker.ShortenAsync("https://tinylinker.com/pages/12/view");
Console.WriteLine(code); // e.g., "Ab12Xy7"
```

### Resolve a URL

```csharp
var url = await _tinyLinker.ResolveAsync(code);

if (url != null)
{
    Console.WriteLine("Original URL: " + url);
}
else
{
    Console.WriteLine("URL not found or expired.");
}
```

Click counts are automatically incremented whenever a URL is resolved.

---

## Example Controller

```csharp
using Microsoft.AspNetCore.Mvc;
using TinyLinker.Interfaces;

[ApiController]
[Route("api/[controller]")]
public class UrlController : ControllerBase
{
    private readonly ITinyLinkerService _tinyLinker;

    public UrlController(ITinyLinkerService tinyLinker)
    {
        _tinyLinker = tinyLinker;
    }

    [HttpPost("shorten")]
    public async Task<IActionResult> ShortenUrl([FromBody] string url)
    {
        var code = await _tinyLinker.ShortenAsync(url);
        return Ok(code);
    }

    [HttpGet("resolve/{code}")]
    public async Task<IActionResult> ResolveUrl(string code)
    {
        var url = await _tinyLinker.ResolveAsync(code);
        if (url == null) return NotFound();
        return Redirect(url);
    }
}
```

---

## Optional Cleanup of Expired URLs

You can periodically remove expired links with a SQL job:

```sql
DELETE FROM [dbo].[TinyLinks]
WHERE ExpiryAt IS NOT NULL
AND ExpiryAt < SYSUTCDATETIME();
```

---

## Notes

* TinyLinker **does not automatically create tables**; you must create the database table manually.
* Ensure your table and column names match your `TinyLinkerOptions`.
* Short codes are random and collision-resistant.
* Click count and expiry are automatically handled.

---

## License

MIT License
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 120 5/5/2026