URL Generation
- Introduction
- Complete Example: Temporary Preview Link
- URLs for Named Routes
- The Application URL
- Forcing HTTPS
Introduction
In controllers and views, Url.Route(...) and @Route(...) build links from the current request (see named routes). The URL facade (named like Laravel's, so it does not clash with the controller's Url property) builds absolute URLs that also work outside a request, for example in a queued mail or notification, and signs URLs.
URLs for Named Routes
URL.Route("posts.show", new { post = 1 }); // https://example.com/posts/1
URL.Route("posts.show", new { post = 1, tab = "comments" }); // https://example.com/posts/1?tab=comments
URL.Route("posts.index", absolute: false); // /posts
URL.To("/about"); // https://example.com/aboutValues that are not route parameters become the query string. A missing required parameter or an unknown route name throws.
Signed URLs
A signed URL carries a signature query parameter, so any change to its path or query makes it invalid. It is how the email verification link works:
var url = URL.SignedRoute("unsubscribe", new { user = 1 });
var temporary = URL.TemporarySignedRoute("unsubscribe", TimeSpan.FromMinutes(30), new { user = 1 });Protect the route with the signed middleware (invalid or expired → 403), or check it yourself:
Route.Get<SubscriptionController>("/unsubscribe/{user}", c => c.Unsubscribe).Name("unsubscribe").Middleware("signed");
if (!URL.HasValidSignature(Request)) return StatusCode(403);The signature covers the path and query, not the host name, and is made with the application's Data Protection keys, so links survive a restart as long as the keys do.
The Application URL
Inside a request, absolute URLs use the request's scheme and host. Outside a request (queue worker, console command), they use url in config/App.cs:
public override object Values() => new
{
name = "SampleApp",
url = Env("APP_URL"),
};Building an absolute URL outside a request without it throws an error that names the setting.
Forcing HTTPS
Like Laravel's URL::forceScheme('https'), URL.ForceScheme("https") forces the scheme of generated absolute URLs. Enable it in your provider's Boot method if required; the sample and template do not enable it automatically:
// app/Providers/AppServiceProvider.cs
public override void Boot(WebApplication app)
{
if (app.Environment.IsProduction())
{
URL.ForceScheme("https");
}
}This applies to URL.To, absolute URL.Route, URL.SignedRoute, and URL.TemporarySignedRoute, including queue/console URLs based on APP_URL. Hosts, explicit ports, and base paths are preserved. Relative routes (absolute: false) are unchanged, and URL.To("http://other.example/path") still returns that already-absolute URL unchanged, like Laravel.
Use URL.ForceScheme(null) or URL.ForceScheme("") to restore the request's scheme (or APP_URL outside a request). Pass a scheme without ://; malformed scheme names throw ArgumentException. The setting belongs to the application's URL generator, so configure it at startup, not per request. It does not change ASP.NET Core's separate Url helper.
This only changes generated links. It does not redirect HTTP requests, enable TLS, change Request.IsHttps, or secure cookies. Configure trusted proxies and hosts and HTTPS at your server separately. APP_URL is still required outside HTTP requests.
Complete Example: Temporary Preview Link
This read-only demonstration uses no database. Create:
// app/Http/Controllers/SignedPreviewController.cs
using DotBlaze;
using Microsoft.AspNetCore.Mvc;
namespace Blog.Controllers;
public class SignedPreviewController : BaseController
{
public IActionResult CreateLink() => Json(new
{
url = URL.TemporarySignedRoute("preview.show",
TimeSpan.FromMinutes(5), new { document = 42 }),
});
public IActionResult Show(long document) => Content($"Preview of document {document}");
}Inside Web.Map():
Route.Get<Blog.Controllers.SignedPreviewController>("/preview-link", c => c.CreateLink);
Route.Get<Blog.Controllers.SignedPreviewController>("/preview/{document}", c => c.Show)
.Name("preview.show").Middleware("signed");Run dotblaze serve, open /preview-link, and follow its url. Expect Preview of document 42. Changing 42 to 43, removing the signature, or using the link after five minutes returns 403. This demo intentionally lets anyone generate a link to fixed example data; authorize link issuance and document access when adapting it to private data. For generation from a command/job, set APP_URL in the existing AppConfig.