A strongly typed URL generation library for ASP.NET Core
$ dotnet add package UriGenerationA 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));
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.
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.
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}.
Install-Package UriGeneration
dotnet add package UriGeneration
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);
});
public class InvoicesController
{
private readonly IUriGenerator _uriGenerator;
public InvoicesController(IUriGenerator uriGenerator)
{
_uriGenerator = uriGenerator;
}
}
To see the default log messages, enable LogLevel.Debug in the logging configuration.