如何将html发送到C#SelfHost控制台应用程序

如何将html发送到C#SelfHost控制台应用程序,c#,self-hosting,C#,Self Hosting,使用System.Web.Http.SelfHost等。如何将html页面发送到浏览器 目前,在谷歌浏览器中,它是以文本的形式出现的。我找不到如何将标题更改为text/html,我不知道这是否会解决它 我已经尝试了附件的一些变体,但没有成功 在Google Chrome浏览器中,插曲数据以Json的形式进入浏览器,但在IE中,它会询问我是要(O)笔还是要(S)保存它。在IE中,html具有相同的结果 我想从RAM而不是磁盘发送html 为了简洁起见,省略了错误处理 代码如下:- using Sy

使用System.Web.Http.SelfHost等。如何将html页面发送到浏览器

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

我已经尝试了附件的一些变体,但没有成功

在Google Chrome浏览器中,插曲数据以Json的形式进入浏览器,但在IE中,它会询问我是要(O)笔还是要(S)保存它。在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()},  
            };
        }
    }
}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Net.Http;
使用System.Net.Http.Formatting;
使用System.Web.Http;
使用System.Web.Http.SelfHost;
命名空间控制台015
{
班级计划
{
静态void Main(字符串[]参数)
{
HttpSelfHostServer服务器=null;
使用(StreamReader reader01=newstreamreader(“test01.html”))
{
LoginController.sPage=reader01.ReadToEnd();
}
Console.WriteLine(LoginController.sPage);
字符串sUrl=”http://localhost:8080";
var serverConfig=新的HttpSelfHostConfiguration(sUrl);
serverConfig.Formatters.Clear();
插入(0,新的JsonMediaTypeFormatter());
serverConfig.Routes.MapHttpRoute(
名称:“DefaultApiRoute”,
routeTemplate:“端点/{controller}”,
默认值:null
);
服务器=新的HttpSelfHostServer(serverConfig);
server.OpenAsync().Wait();
Console.WriteLine(“监听:“+sUrl+”/endpoints/eposion”);
Console.ReadLine();
}
}
公共类登录控制器:ApiController
{
公共静态字符串sPage=string.Empty;
公共HttpResponseMessage GetLoginPage()
{
//创建一个200响应。
var响应=新的HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
内容=新的字符串内容(sPage)
};
Console.WriteLine(“返回登录页面”);
返回响应;
}
}
公共课插曲
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串ReleasedOn{get;set;}
}
公共类代码控制器:ApiController
{
公共IList GetAllEpisodes()
{
返回新列表
{
新的插曲{Id=1,Name=“第1集”,ReleasedOn=DateTime.Now.AddDays(10.ToSortDateString()},
新的插曲{Id=2,Name=“第2集”,ReleasedOn=DateTime.Now.AddDays(-5).toSortDateString()},
新的插曲{Id=3,Name=“第3集”,ReleasedOn=DateTime.Now.AddDays(-3).ToSortDateString()},
新插曲{Id=4,Name=null,ReleasedOn=DateTime.Now.AddDays(0.ToSortDateString()},
};
}
}
}
HTML测试数据为:

<!DOCTYPE html>
<html>
<body>

    <h1>My First Heading</h1>

    <p>My first paragraph.</p>

</body>
</html>

我的第一个标题
我的第一段

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

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