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 148 12/18/2021
0.1.72 110 12/17/2021
0.1.71 128 12/17/2021
0.1.70 93 12/16/2021
0.1.69 106 12/16/2021
0.1.68 96 12/16/2021
0.1.67 106 12/15/2021
0.1.66 106 12/15/2021
0.1.65 105 12/15/2021
0.1.64 109 12/13/2021
0.1.63 109 12/11/2021
0.1.62 97 12/11/2021
0.1.61 94 12/11/2021
0.1.60 104 12/11/2021
0.1.59 102 12/11/2021
0.1.58 105 12/11/2021
0.1.57 101 12/11/2021
0.1.56 97 12/11/2021
0.1.55 105 12/10/2021
0.1.54 107 12/10/2021
0.1.53 113 12/10/2021
0.1.52 106 12/9/2021
0.1.51 98 12/9/2021
0.1.50 115 12/9/2021
0.1.49 126 12/9/2021
0.1.48 122 12/9/2021
0.1.47 121 12/9/2021
0.1.46 131 12/9/2021
0.1.45 121 12/9/2021
0.1.44 119 12/9/2021
0.1.43 116 12/9/2021
0.1.42 116 12/9/2021
0.1.41 120 12/9/2021
0.1.40 113 12/9/2021
0.1.39 104 12/9/2021
0.1.38 112 12/9/2021
0.1.37 119 12/9/2021
0.1.36 116 12/8/2021
0.1.35 112 12/8/2021
0.1.34 112 12/8/2021
0.1.33 118 12/8/2021
0.1.32 112 12/8/2021
0.1.31 116 12/8/2021
0.1.30 115 12/8/2021
0.1.29 111 12/8/2021
0.1.28 112 12/8/2021
0.1.27 106 12/8/2021
0.1.26 111 12/8/2021
0.1.25 113 12/8/2021
0.1.24 117 12/8/2021
0.1.23 106 12/7/2021
0.1.22 103 12/7/2021
0.1.21 105 12/7/2021
0.1.20 102 12/6/2021
0.1.19 106 12/6/2021
0.1.18 101 12/6/2021
0.1.17 111 12/6/2021
0.1.16 107 12/6/2021
0.1.15 104 12/6/2021
0.1.14 110 12/6/2021
0.1.13 113 12/6/2021
0.1.12 450 12/4/2021
0.1.11 411 12/4/2021
0.1.10 403 12/4/2021
0.1.9 383 12/4/2021
0.1.8 348 12/4/2021
0.1.7 360 12/4/2021
0.1.6 354 12/4/2021
0.1.5 111 12/4/2021
0.1.4 110 12/4/2021
0.1.3 109 12/4/2021
0.1.2 112 12/4/2021
0.1.1 114 12/4/2021
0.1.0 115 12/4/2021