Virtual Hosting

Runs on your machine

Hosts multiple websites, one per domain, on a single server.

Hosts a blog and a shop on one server, each on a domain of its own. VirtualHosts picks the website by the domain the request was sent to (its Host header), and sends the visitors of any other domain to the blog.

Browsers resolve every name ending in .localhost to your own machine, so you can try it without setting up any domain: open http://blog.localhost:8080/ and http://shop.localhost:8080/.

Runs on your machine only. It needs domains of its own.

To run it, create a console app (dotnet new console), add GenHTTP (dotnet add package GenHTTP.Full), replace Program.cs with the code below and start it with dotnet run - it answers at http://localhost:8080/.

Program.cs22 lines
using GenHTTP.Engine.Internal;

using GenHTTP.Api.Protocol;

using GenHTTP.Modules.IO;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.Redirects;
using GenHTTP.Modules.VirtualHosting;

var blog = Content.From(Resource.FromString("<h1>My Blog</h1>").Type(ContentType.TextHtml));

var shop = Content.From(Resource.FromString("<h1>My Shop</h1>").Type(ContentType.TextHtml));

var app = VirtualHosts.Create()
                      .Add("blog.localhost", blog) // http://blog.localhost:8080/
                      .Add("shop.localhost", shop) // http://shop.localhost:8080/
                      .Default(Redirect.To("http://blog.localhost:8080/", temporary: true)); // any other host

await Host.Create()
          .Handler(app)
          .Defaults()
          .RunAsync();

Try it

With the sample running on your machine:

  1. Open the blog

    PAGE http://blog.localhost:8080/ Open
  2. Open the shop

    PAGE http://shop.localhost:8080/ Open
  3. Choose the site via the Host header

    curl -H "Host: shop.localhost" http://localhost:8080/