title: How to serve relative files from HttpListener
body: <p>I am learning the <code>HttpListener</code>.  I am creating a small app that is a webserver by using the <code>HttpListener</code> (following
  <a href="http://msdn.microsoft.com/en-us/library/system.net.httplistener%28v=vs.110%29.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.net.httplistener%28v=vs.110%29.aspx</a> and <a href="https://www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx" rel="noreferrer">https://www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx</a>).  Note, no ASP.NET stuff.</p>
  
  <p>In my function that is called from <code>_responderMethod</code>, I basically return HTML (that is read from a physical file on disk) which contains something like:</p>
  
  <pre><code>   ...
     &lt;link href="css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet"&gt;
     &lt;script src="js/jquery-1.10.2.js"&gt;&lt;/script&gt;
     &lt;script src="js/jquery-ui-1.10.4.custom.js"&gt;&lt;/script&gt;
     ...
  </code></pre>
  
  <p>However, as to be expected, the <code>.css</code> and <code>.js</code> files don't seem to be served (I can tell because there's no styles or behaviors as expected on the client after the html has been served).</p>
  
  <p>How do I go about serving this files, do I need to do something with <code>HttpServerUtility.MapPath</code>? if so, could you point me to some examples on that?</p>
  
  <p>Or do I need to scan the HTML I am about to serve and open those files up (recursively) read and return those?  I hope not.</p>
  
  <p>BTW, the C# code that serves this is as follows, where my <code>_responderMethod</code> just returns the string of the HTML file as it is stated above:</p>
  
  <p>I initialize and start the server as follows:</p>
  
  <pre><code>WebServer ws = new WebServer(program.SendResponse, "http://&lt;myip&gt;:8080/");
  ws.Run();
  </code></pre>
  
  <p>the class constructor is pretty much:</p>
  
  <pre><code>public class WebServer
  {
      private readonly HttpListener _listener = new HttpListener();
      private readonly Func&lt;HttpListenerRequest, string&gt; _responderMethod;
  
      public WebServer(string[] prefixes, Func&lt;HttpListenerRequest, string&gt; method)
      { 
          // A responder method is required
          if (method == null)
              throw new ArgumentException("method");
  
          foreach (string s in prefixes)
              _listener.Prefixes.Add(s);
  
           _responderMethod = method;
           _listener.Start();
  }
  public WebServer(Func&lt;HttpListenerRequest, string&gt; method, params string[] prefixes)
      : this(prefixes, method) { }
  </code></pre>
  
  <p>the <code>.Run()</code> is:</p>
  
  <pre><code>public void Run()
  {
      ThreadPool.QueueUserWorkItem((o) =&gt;
      {
          Console.WriteLine("Webserver running...");
          try
          {
              while (_listener.IsListening)
              {
                  ThreadPool.QueueUserWorkItem((c) =&gt;
                  {
                      var ctx = c as HttpListenerContext;
                      try
                      {
                          string rstr = _responderMethod(ctx.Request);
                          byte[] buf = Encoding.UTF8.GetBytes(rstr);
                          ctx.Response.ContentLength64 = buf.Length;
                          ctx.Response.OutputStream.Write(buf, 0, buf.Length);
                      }
                      catch { } // suppress any exceptions
                      finally
                      {
                          // always close the stream
                          ctx.Response.OutputStream.Close();
                      }
                  }, _listener.GetContext());
              }
          }
          catch { } // suppress any exceptions
      });
  }
  </code></pre>
  
  <p>My <code>SendResponse()</code>:</p>
  
  <pre><code>public string SendResponse(HttpListenerRequest request)
  {
        return File.ReadAllText(@"static\index.html");
  }
  </code></pre>