Created
January 20, 2021 21:40
-
-
Save mabster/0d55b2887471bae3da1d8fd6010f109c to your computer and use it in GitHub Desktop.
Tiny HTTP Redirect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Routing; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Hosting; | |
using System.Threading.Tasks; | |
var config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: false, reloadOnChange: false).Build(); | |
WebHost.CreateDefaultBuilder().Configure(app => { | |
app.UseRouting().UseEndpoints(endpoints => | |
{ | |
endpoints.MapGet("/", context => context.Response.WriteAsync("Hello")); | |
foreach (var r in config.GetSection("Aliases").GetChildren()) | |
endpoints.MapGet("/" + r.Key, context => Task.Run(() => context.Response.Redirect(r.Value))); | |
}); | |
}).Build().Run(); | |
/* appsettings.json: | |
"Aliases": { | |
"foo": "https://www.google.com/", | |
"bar": "https://www.microsoft.com/", | |
"www": "https://www.wodongatafe.edu.au" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment