Skip to content

A simple link shortener for include in .net core projects

License

Notifications You must be signed in to change notification settings

jeancarlo13/JCTools.Shortener

Repository files navigation

JCTools.Shortener

v1.1.2 .net core 3.1 .net 5.0

A simple links shortener for include in .net core projects.

Allows add support for create and manage random and unique short links. Its short links are ready for share link to the features or pages of your site.

The short links are created with the letters of the alphabet of the english language and the numerals 0-9, a total use 62 distinct characters.

This characters can will generate more of 57 billions of distinct combinations.

By default, the short links have a longitude between 2 and 6 characters; but, too can configure this longitude.

The longitude and the characters can was changed in the service initialization into the startup class.

Usage

  1. Add the package JCTools.Shortener to your project.
    dotnet add package JCTools.Shortener -v 1.1.1
  1. Implement the interface IDatabaseContext in your database contest
    public class DataContext : Microsoft.EntityFrameworkCore.DbContext, JCTools.Shortener.Settings.IDatabaseContext
    {
        public DataContext(DbContextOptions<DataContext> options)
            : base(options)
        { }
        /// <summary>
        /// the short links collection
        /// </summary>
        public DbSet<ShortLink> ShortLinks { get; set; }

    }
  1. Add the namespace JCTools.Shortener to the startup class
    using JCTools.Shortener;
  1. Configure the shortener service into the ConfigureServices method of the Startup class
    services.AddLinksShortener<DbContext>();
  1. Inject the JCTools.Shortener.Services.ILinkGenerator service
    public class OtherController : Controller
    {
        private readonly ILinkGenerator _shortenerService;

        public OtherController(ILinkGenerator shortenerService)
        {
            ...
    
            this._shortenerService = shortenerService;
    
            ...
        }

        ...

    }
  1. Generate the short link
    ...

    // only generate a token
    var token = await _shortenerService.GenerateTokenAsync();

    // Generate a new entry with a short link
    var realUrl = "https://www.google.com/doodles/"
    var link = await _shortenerServices.GenerateAsync(realUrl);

    // Generate a new entry with a short link and store into the database
    var other = await _shortenerServices. GenerateAndSaveAsync(realUrl); 

    ...

Other settings

In the configuration of the Shortener services into the ConfigureServices method of the Startup class, is possible change the default configuration of the short links.

    ...

    services.AddLinksShortener<DbContext>(o => {
        // 1. Change the collection of the characters to be used for generate the short links
        o.ValidCharacters = "1234567890-_qwerty";
        // 2. The min longitude (default 2) of the short links
        o.MinLength = 3; 
        // 3. The max longitude (default 6) of the short links
        o.MaxLength = 10;
        // 4. By default, the access to the redirection action is anonymous
        // It's possible change using a authorization policy  
        o.ConfigurePolicy = p => {
            p.RequireAuthenticatedUser();
            p.RequireRole("admin");
            
            ...
        
        };
    });

    ...

License

This package is released under the MIT license

About

A simple link shortener for include in .net core projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages