keeg keeg - 2 months ago 19
C# Question

how to setup a .net standalone app that creates and hosts a website on localhost

I have a program (.exe) file that creates a website on localhost:8080 and it opens up a bunch of commands that can be executed by going to various urls on the website. I'm not sure what this is written it, but I'm wondering if its possible to do in .net (c#). Where I launch a self contained .exe file (that will used an embedded database) and I can write to the database via the website that is created on localhost.

So basically I want the website to serve as the GUI for a standalone .exe file.

Answer

Yes, this is quite easy using an HttpListener.

As an example to help you do more, grab the class from the link below, then you can just stick this into a simple console app, run it and go to http://localhost:8080/test/

static void Main(string[] args)
{
    WebServer ws = new WebServer(SendResponse, "http://localhost:8080/test/");
    ws.Run();
    Console.WriteLine("A simple webserver. Press a key to quit.");
    Console.ReadKey();
    ws.Stop();
}

public static string SendResponse(HttpListenerRequest request)
{
    return string.Format("<HTML><BODY>My web page.<br>{0}</BODY></HTML>", DateTime.Now);    
}

There is more code here under the MIT license: https://codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server Copyright (c) 2013 David's Blog (www.codehosting.net)