Skip to content

Instantly share code, notes, and snippets.

@mabster
Created January 20, 2021 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mabster/0d55b2887471bae3da1d8fd6010f109c to your computer and use it in GitHub Desktop.
Save mabster/0d55b2887471bae3da1d8fd6010f109c to your computer and use it in GitHub Desktop.
Tiny HTTP Redirect
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