【问题标题】:How to send html to C# SelfHost console application如何将 html 发送到 C# SelfHost 控制台应用程序
【发布时间】:2012-10-04 10:43:25
【问题描述】:

使用 System.Web.Http.SelfHost 等如何向浏览器发送 html 页面?

目前,在 Google Chrome 中,它以文本形式出现。我找不到如何将标题更改为 text/html,我不知道这是否会解决它。

我尝试了附件的多种变体,但均未成功。

情节数据以 Json 形式通过 Google Chrome 浏览器中的浏览器,但在 IE 中它询问我是要 (O)pen 还是 (S)ave 它。在 IE 中,html 的结果是一样的。

我想从 RAM 发送 html,而不是磁盘。

为简洁起见,省略了错误处理。

代码如下:-

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace Console015
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpSelfHostServer server = null;

            using (StreamReader reader01 = new StreamReader("test01.html"))
            {
                LoginController.sPage = reader01.ReadToEnd();
            }
            Console.WriteLine(LoginController.sPage);

            String sUrl = "http://localhost:8080";
            var serverConfig = new HttpSelfHostConfiguration(sUrl);
            serverConfig.Formatters.Clear();
            serverConfig.Formatters.Insert(0, new JsonMediaTypeFormatter());
            serverConfig.Routes.MapHttpRoute(
                name: "DefaultApiRoute",
                routeTemplate: "endpoints/{controller}",
                defaults: null
                );

            server = new HttpSelfHostServer(serverConfig);

            server.OpenAsync().Wait();

            Console.WriteLine("Listening At : " + sUrl + "/endpoints/episode");
            Console.ReadLine();
        }
    }

    public class LoginController : ApiController
    {
        public static string sPage = string.Empty;
        public HttpResponseMessage GetLoginPage()
        {
            // Create a 200 response.
            var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
            {
                Content = new StringContent(sPage)
            };

            Console.WriteLine("Returning Login Page");

            return response;

        }
    }

    public class Episode
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ReleasedOn { get; set; }
    }

    public class EpisodeController : ApiController
    {
        public IList<Episode> GetAllEpisodes()
        {
            return new List<Episode>
            {
                new Episode {Id = 1, Name = "Episode 1", ReleasedOn =     DateTime.Now.AddDays(10).ToShortDateString()},
                new Episode {Id = 2, Name = "Episode 2", ReleasedOn =     DateTime.Now.AddDays( -5 ).ToShortDateString()},  
                new Episode {Id = 3, Name = "Episode 3", ReleasedOn = DateTime.Now.AddDays( -3 ).ToShortDateString()},  
                new Episode {Id = 4, Name = null, ReleasedOn = DateTime.Now.AddDays( 0 ).ToShortDateString()},  
            };
        }
    }
}

HTML 测试数据为:

<!DOCTYPE html>
<html>
<body>

    <h1>My First Heading</h1>

    <p>My first paragraph.</p>

</body>
</html>

【问题讨论】:

    标签: c# self-hosting


    【解决方案1】:

    答案似乎是: response.Content.Headers.ContentType.MediaType = "text/html";

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2012-12-17
      相关资源
      最近更新 更多