-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
39 lines (28 loc) · 1.13 KB
/
Program.cs
File metadata and controls
39 lines (28 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Microsoft.EntityFrameworkCore;
using WebRedirector.Database;
using WebRedirector.Database.Entities;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// Prefer connection string from connected service / configuration.
string? connectionString = builder.Configuration.GetConnectionString("AppsDatabase");
builder.Services.AddDbContext<AppsContext>(options =>
{
// Enable transient error resiliency and pass the connection string.
_ = options.UseSqlServer(connectionString, sqlOptions =>
{
_ = sqlOptions.EnableRetryOnFailure();
});
});
WebApplication app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapGet("/{alias:regex(^[A-Za-z0-9]+$)}", async (string alias, AppsContext db) =>
{
UrlEntity? entry = await db.Urls.AsNoTracking().FirstOrDefaultAsync(u => u.Alias == alias);
return entry is null ? Results.Redirect("PageNotFound", permanent: false) : Results.Redirect(entry.Destination, permanent: false);
});
app.MapStaticAssets();
app.MapRazorPages().WithStaticAssets();
app.Run();