LinkBuilding 0.1.73

.NET 6.0
Install-Package LinkBuilding -Version 0.1.73
dotnet add package LinkBuilding --version 0.1.73
<PackageReference Include="LinkBuilding" Version="0.1.73" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LinkBuilding --version 0.1.73
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LinkBuilding, 0.1.73"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install LinkBuilding as a Cake Addin
#addin nuget:?package=LinkBuilding&version=0.1.73

// Install LinkBuilding as a Cake Tool
#tool nuget:?package=LinkBuilding&version=0.1.73
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Link Building

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

_linkBuilder.BuildUriFromExpression<InvoicesController>(
    httpContext,
    c => c.GetInvoice(2));
_linkBuilder.BuildUriFromExpression(
    httpContext,
    (InvoicesController 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 all cases documented in LinkBuilding.Tests
  • 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 build an endpoint specific URL:

_linkBuilder.BuildUriFromExpression<InvoicesController>(
    httpContext,
    c => c.GetInvoice(2),
    "ApiGetInvoice");
_linkBuilder.BuildUriFromExpression<InvoicesController>(
    httpContext,
    c => c.GetInvoice(2),
    "GetInvoice");

LinkBuilder will validate whether such endpoint name is defined in action's HttpMethod, AcceptVerbs, or Route attribute and build 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 LinkBuilder.

Performance:

Extracting values from expression trees does introduce some overhead. To work around this problem, LinkBuilding 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 build 100000 URLs in a second using a template like this: https://localhost:44339/api/invoices/{id}.

Setup:

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

    public InvoicesController(ILinkBuilder linkBuilder)
    {
        _linkBuilder = linkBuilder;
    }
}

Examples:

Check out LinkBuilding.Examples for working examples.

Credits:

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
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
0.1.73 129 12/18/2021
0.1.72 90 12/17/2021
0.1.71 109 12/17/2021
0.1.70 79 12/16/2021
0.1.69 86 12/16/2021
0.1.68 80 12/16/2021
0.1.67 88 12/15/2021
0.1.66 84 12/15/2021
0.1.65 82 12/15/2021
0.1.64 87 12/13/2021
0.1.63 90 12/11/2021
0.1.62 83 12/11/2021
0.1.61 80 12/11/2021
0.1.60 88 12/11/2021
0.1.59 81 12/11/2021
0.1.58 85 12/11/2021
0.1.57 87 12/11/2021
0.1.56 80 12/11/2021
0.1.55 91 12/10/2021
0.1.54 88 12/10/2021
0.1.53 93 12/10/2021
0.1.52 82 12/9/2021
0.1.51 80 12/9/2021
0.1.50 101 12/9/2021
0.1.49 108 12/9/2021
0.1.48 103 12/9/2021
0.1.47 102 12/9/2021
0.1.46 109 12/9/2021
0.1.45 105 12/9/2021
0.1.44 102 12/9/2021
0.1.43 103 12/9/2021
0.1.42 100 12/9/2021
0.1.41 100 12/9/2021
0.1.40 95 12/9/2021
0.1.39 88 12/9/2021
0.1.38 98 12/9/2021
0.1.37 100 12/9/2021
0.1.36 98 12/8/2021
0.1.35 93 12/8/2021
0.1.34 97 12/8/2021
0.1.33 96 12/8/2021
0.1.32 96 12/8/2021
0.1.31 95 12/8/2021
0.1.30 94 12/8/2021
0.1.29 92 12/8/2021
0.1.28 91 12/8/2021
0.1.27 82 12/8/2021
0.1.26 95 12/8/2021
0.1.25 95 12/8/2021
0.1.24 100 12/8/2021
0.1.23 87 12/7/2021
0.1.22 84 12/7/2021
0.1.21 84 12/7/2021
0.1.20 83 12/6/2021
0.1.19 86 12/6/2021
0.1.18 84 12/6/2021
0.1.17 92 12/6/2021
0.1.16 90 12/6/2021
0.1.15 90 12/6/2021
0.1.14 94 12/6/2021
0.1.13 95 12/6/2021
0.1.12 435 12/4/2021
0.1.11 399 12/4/2021
0.1.10 386 12/4/2021
0.1.9 364 12/4/2021
0.1.8 334 12/4/2021
0.1.7 344 12/4/2021
0.1.6 336 12/4/2021
0.1.5 89 12/4/2021
0.1.4 89 12/4/2021
0.1.3 89 12/4/2021
0.1.2 90 12/4/2021
0.1.1 90 12/4/2021
0.1.0 99 12/4/2021