LinkBuilding 0.1.51
dotnet add package LinkBuilding --version 0.1.51
NuGet\Install-Package LinkBuilding -Version 0.1.51
<PackageReference Include="LinkBuilding" Version="0.1.51" />
paket add LinkBuilding --version 0.1.51
#r "nuget: LinkBuilding, 0.1.51"
// Install LinkBuilding as a Cake Addin
#addin nuget:?package=LinkBuilding&version=0.1.51
// Install LinkBuilding as a Cake Tool
#tool nuget:?package=LinkBuilding&version=0.1.51
Link Building
Strongly typed, cached URL generation for ASP.NET Core using lambda expressions:
_linkBuilder.GetUriByExpression<InvoicesController>(
httpContext,
c => c.GetInvoice(2));
_linkBuilder.GetUriByExpression(
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 all optional LinkGenerator's methods' parameters
- Supports bypassable caching
Attribute routing:
If multiple URL paths match action in your controller:
[HttpGet("api/invoices/{invoiceId}", Name = "GetApiInvoice")]
[HttpGet("invoices/{invoiceId}", Name = "GetInvoice")]
public InvoiceResource GetInvoice(int invoiceId)
you can specify an endpoint name to generate an endpoint specific URL:
_linkBuilder.GetUriByExpression<InvoicesController>(
httpContext: httpContext,
action: c => c.GetInvoice(2),
endpointName: "GetApiInvoice");
_linkBuilder.GetUriByExpression<InvoicesController>(
httpContext: httpContext,
action: c => c.GetInvoice(2),
endpointName: "GetInvoice");
LinkBuilder 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 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.IMemoryCache 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 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 IMemoryCache entry will have a size of 1):
builder.Services.AddLinkBuilding();
builder.Services.AddLinkBuilding(o =>
{
o.SizeLimit = 18;
o.CompactionPercentage = 0.75;
});
- Start injecting LinkBuilder singleton service into your classes:
public class YourClass
{
private readonly ILinkBuilder _linkBuilder;
public YourClass(ILinkBuilder linkBuilder)
{
_linkBuilder = linkBuilder;
}
}
Examples:
Check out LinkBuilding.Examples for working examples.
Credits:
- LinkBuilder and CachedExpressionCompiler by https://github.com/aspnet/AspNetWebStack (Apache-2.0 license)
Product | Versions 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. |
-
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 |
---|