如何使用C#窗体创建一个简单的本地网页

Jes*_*aze 4 c# hosting localhost endpoint

我希望使用C#Windows窗体应用程序或C#控制台应用程序创建一个简单的网页.

运行该应用程序将开始在以下位置托管网页:

http://localhost:3070/somepage
Run Code Online (Sandbox Code Playgroud)

我在MSDN上读了一些关于使用端点的内容,但是自学成才,这对我来说没什么意义......

简而言之,该程序在运行时将在localhost:3070的网页上显示一些文本.

对于这样一个模糊的问题感到抱歉,但是我搜索一个体面的教程的时间并没有产生任何可以理解的结果......

谢谢你的时间!

Dal*_*rzo 5

Microsoft 发布了一个名为 OWIN 的开源项目,它类似于 Node,但最重要的是它允许您在控制台应用程序中托管 Web 应用程序:

您可以在这里找到更多信息:

但是如果你坚持创建你的个人监听器,你可以在这里找到一些帮助:

  • 每次我用一句话看到 Microsoft 和 Open Source 时,我都需要重新阅读以确保。:> (2认同)

Oxy*_*ron 5

您将需要查看创建一个HttpListener,您可以向侦听器添加前缀,例如Listener.Prefixes.Add("http://+:3070/")将它绑定到您想要的端口.

一个简单的控制台应用程序:计算所做的请求

using System;
using System.Net;
using System.Text;

namespace TestServer
{
    class ServerMain
    {
        // To enable this so that it can be run in a non-administrator account:
        // Open an Administrator command prompt.
        // netsh http add urlacl http://+:8008/ user=Everyone listen=true

        const string Prefix = "http://+:3070/";
        static HttpListener Listener = null;
        static int RequestNumber = 0;
        static readonly DateTime StartupDate = DateTime.UtcNow;

        static void Main(string[] args)
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("HttpListener is not supported on this platform.");
                return;
            }
            using (Listener = new HttpListener())
            {
                Listener.Prefixes.Add(Prefix);
                Listener.Start();
                // Begin waiting for requests.
                Listener.BeginGetContext(GetContextCallback, null);
                Console.WriteLine("Listening. Press Enter to stop.");
                Console.ReadLine();
                Listener.Stop();
            }
        }

        static void GetContextCallback(IAsyncResult ar)
        {
            int req = ++RequestNumber;

            // Get the context
            var context = Listener.EndGetContext(ar);

            // listen for the next request
            Listener.BeginGetContext(GetContextCallback, null);

            // get the request
            var NowTime = DateTime.UtcNow;

            Console.WriteLine("{0}: {1}", NowTime.ToString("R"), context.Request.RawUrl);

            var responseString = string.Format("<html><body>Your request, \"{0}\", was received at {1}.<br/>It is request #{2:N0} since {3}.",
                context.Request.RawUrl, NowTime.ToString("R"), req, StartupDate.ToString("R"));

            byte[] buffer = Encoding.UTF8.GetBytes(responseString);
            // and send it
            var response = context.Response;
            response.ContentType = "text/html";
            response.ContentLength64 = buffer.Length;
            response.StatusCode = 200;
            response.OutputStream.Write(buffer, 0, buffer.Length);
            response.OutputStream.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要获得额外的功劳,请尝试将其添加到计算机上的服务中!