UriGeneration 4.0.3
dotnet add package UriGeneration --version 4.0.3
NuGet\Install-Package UriGeneration -Version 4.0.3
<PackageReference Include="UriGeneration" Version="4.0.3" />
paket add UriGeneration --version 4.0.3
#r "nuget: UriGeneration, 4.0.3"
// Install UriGeneration as a Cake Addin #addin nuget:?package=UriGeneration&version=4.0.3 // Install UriGeneration as a Cake Tool #tool nuget:?package=UriGeneration&version=4.0.3
Uri Generation
A strongly typed URL generation library for ASP.NET Core:
// uri: https://localhost:44339/api/invoices/2
string? uri = _uriGenerator.GetUriByExpression<InvoicesController>(
httpContext,
controller => controller.GetInvoice(2));
Features:
- Extracts action name, controller name, and route values from expression
- Delegates URL generation to LinkGenerator
- Supports ASP.NET Core model metadata and application model conventions
- Supports simple types and collections of simple types only
- Supports specifying an endpoint name
- Supports LinkOptions
- Supports bypassable caching
- Invalidates HttpContext's ambient route values
Binding source filter:
You can specify a predicate which can determine whether an action parameter should be included based on its binding source.
The default one is:
Func<BindingSource?, bool> bindingSourceFilter = bindingSource =>
bindingSource == null
|| bindingSource.CanAcceptDataFrom(BindingSource.Query)
|| bindingSource.CanAcceptDataFrom(BindingSource.Path);
You should pass null or default(T) to excluded action parameters when calling IUriGenerator.GetUriByExpression or a similar method.
For more information on binding sources, see ASP.NET Core documentation.
Specifying an endpoint name:
If you use named attribute routes:
[HttpGet("api/invoices/{id}", Name = "ApiGetInvoice")]
[HttpGet("invoices/{id}", Name = "GetInvoice")]
public InvoiceResource GetInvoice(int id)
or dedicated conventional routes:
app.MapControllerRoute(
name: "ApiGetInvoice",
pattern: "api/invoices/{id}",
defaults: new { controller = "Invoices", action = "GetInvoice" });
app.MapControllerRoute(
name: "GetInvoice",
pattern: "invoices/{id}",
defaults: new { controller = "Invoices", action = "GetInvoice" });
you can specify an endpoint name to generate an endpoint-specific URL:
// uri: https://localhost:44339/api/invoices/2
string? uri = _uriGenerator.GetUriByExpression<InvoicesController>(
httpContext,
controller => controller.GetInvoice(2),
"ApiGetInvoice");
// uri: https://localhost:44339/invoices/2
string? uri = _uriGenerator.GetUriByExpression<InvoicesController>(
httpContext,
controller => controller.GetInvoice(2),
"GetInvoice");
For more information on endpoint names, see ASP.NET Core documentation.
Performance:
Extracting values from expression trees does introduce some overhead. To partially work around this problem, UriGeneration uses ASP.NET's CachedExpressionCompiler, so that equivalent action arguments' expression trees only have to be compiled once.
Additionally, it uses its internal Microsoft.Extensions.Caching.Memory.MemoryCache instance to cache extracted action methods' metadata.
This means that, for example, on 2017 Surface Book 2 you are able to generate 150 000 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 method cache entry will have a size of 1):
builder.Services.AddUriGeneration();
builder.Services.AddUriGeneration(options =>
{
options.MethodCacheSizeLimit = 500;
options.BypassMethodCache = false;
options.BypassCachedExpressionCompiler = false;
options.BindingSourceFilter = bindingSource =>
bindingSource == null
|| bindingSource.CanAcceptDataFrom(BindingSource.Query)
|| bindingSource.CanAcceptDataFrom(BindingSource.Path);
});
- 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:
- 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 is compatible. 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 is compatible. 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. net9.0 is compatible. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.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 |
---|---|---|
4.0.3 | 50 | 11/20/2024 |
4.0.2 | 76 | 11/14/2024 |
4.0.1 | 71 | 11/13/2024 |
4.0.0 | 75 | 11/13/2024 |
3.0.5 | 743 | 6/8/2024 |
3.0.4 | 93 | 6/8/2024 |
3.0.3 | 267 | 2/27/2024 |
3.0.2 | 151 | 1/6/2024 |
3.0.1 | 128 | 12/29/2023 |
3.0.0 | 115 | 12/29/2023 |
2.0.15 | 158 | 11/21/2023 |
2.0.14 | 159 | 9/21/2023 |
2.0.13 | 170 | 9/2/2023 |
2.0.12 | 142 | 9/2/2023 |
2.0.11 | 351 | 1/19/2023 |
2.0.10 | 318 | 12/30/2022 |
2.0.9 | 346 | 11/26/2022 |
2.0.8 | 351 | 11/1/2022 |
2.0.7 | 414 | 9/16/2022 |
2.0.6 | 487 | 9/13/2022 |
2.0.5 | 406 | 9/10/2022 |
2.0.4 | 404 | 9/10/2022 |
2.0.3 | 421 | 9/10/2022 |
2.0.2 | 418 | 9/9/2022 |
2.0.1 | 398 | 9/9/2022 |
2.0.0 | 411 | 9/9/2022 |
1.1.1 | 447 | 8/25/2022 |
1.1.0 | 422 | 8/25/2022 |
1.0.4 | 415 | 8/18/2022 |
1.0.3 | 397 | 8/18/2022 |
1.0.2 | 400 | 8/18/2022 |
1.0.1 | 407 | 8/18/2022 |
1.0.0 | 406 | 8/18/2022 |
0.1.76 | 414 | 8/18/2022 |
0.1.75 | 424 | 8/10/2022 |