UriGeneration 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package UriGeneration --version 1.0.3
NuGet\Install-Package UriGeneration -Version 1.0.3
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="UriGeneration" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UriGeneration --version 1.0.3
#r "nuget: UriGeneration, 1.0.3"
#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.
// Install UriGeneration as a Cake Addin
#addin nuget:?package=UriGeneration&version=1.0.3

// Install UriGeneration as a Cake Tool
#tool nuget:?package=UriGeneration&version=1.0.3

Uri Generation

Strongly typed, cached URL generation for ASP.NET Core using lambda expressions:

_uriGenerator.GetUriByExpression<InvoicesController>(
    httpContext,
    c => c.GetInvoice(2));

Features:

  • Extracts action name, controller name, and route values from expression
  • Delegates URL generation to LinkGenerator
  • Supports ActionName, Area, NonAction and NonController attributes
  • Supports Controller and Async suffixes
  • Supports LinkOptions
  • Supports bypassable caching

Attribute routing:

If multiple URL paths match an action in your controller:

[HttpGet("api/invoices/{id}", Name = "ApiGetInvoice")]
[HttpGet("invoices/{id}", Name = "GetInvoice")]
public InvoiceResource GetInvoice(int id)

you can specify an endpoint name to generate an endpoint-specific URL:

_uriGenerator.GetUriByExpression<InvoicesController>(
    httpContext,
    c => c.GetInvoice(2),
    "ApiGetInvoice");
_uriGenerator.GetUriByExpression<InvoicesController>(
    httpContext,
    c => c.GetInvoice(2),
    "GetInvoice");

UriGenerator will validate whether such endpoint name is defined in action's HttpMethod, AcceptVerbs, or Route attribute and generate URL based on it and expression's route values.

To further improve on this, you can use classes such as Microsoft.Azure.Management.ResourceManager.Fluent.Core.ExpandableStringEnum instead of strings to define your endpoint names. That would require you to write a simple adapter class that takes a string from your class and passes it to UriGenerator.

Performance:

Extracting values from expression trees does introduce some overhead. To work around this problem, UriGeneration uses AspNetWebStack's CachedExpressionCompiler, so that equivalent route values' values' expression trees only have to be compiled once.

Additionally, it uses its internal Microsoft.Extensions.Caching.Memory.MemoryCache instance to cache extracted controller names, action names, and route values' keys within the scope of the application lifetime.

This means that, for example, on 2017 Surface Book 2 you are able to generate 100000 URLs in a second using a template like this: https://localhost:44339/api/invoices/{id}.

Setup:

  • Install UriGeneration via NuGet Package Manager, Package Manager Console or dotnet CLI:
Install-Package UriGeneration
dotnet add package UriGeneration
  • Register UriGeneration in a service container (each cache entry will have a size of 1):
builder.Services.AddUriGeneration();
builder.Services.AddUriGeneration(o =>
{
    o.SizeLimit = 100;
    o.CompactionPercentage = 0.75;
});
  • Request an instance of IUriGenerator singleton service from any constructor in your app:
public class InvoicesController
{
    private readonly IUriGenerator _uriGenerator;

    public InvoicesController(IUriGenerator uriGenerator)
    {
        _uriGenerator = uriGenerator;
    }
}

Debugging:

To see the default log messages, enable LogLevel.Debug in the logging configuration.

Credits:

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

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
3.0.3 119 2/27/2024
3.0.2 112 1/6/2024
3.0.1 96 12/29/2023
3.0.0 82 12/29/2023
2.0.15 127 11/21/2023
2.0.14 132 9/21/2023
2.0.13 141 9/2/2023
2.0.12 113 9/2/2023
2.0.11 305 1/19/2023
2.0.10 291 12/30/2022
2.0.9 316 11/26/2022
2.0.8 328 11/1/2022
2.0.7 383 9/16/2022
2.0.6 457 9/13/2022
2.0.5 382 9/10/2022
2.0.4 379 9/10/2022
2.0.3 394 9/10/2022
2.0.2 387 9/9/2022
2.0.1 374 9/9/2022
2.0.0 380 9/9/2022
1.1.1 421 8/25/2022
1.1.0 394 8/25/2022
1.0.4 384 8/18/2022
1.0.3 370 8/18/2022
1.0.2 373 8/18/2022
1.0.1 376 8/18/2022
1.0.0 383 8/18/2022
0.1.76 389 8/18/2022
0.1.75 393 8/10/2022