提供HTML的C#SelfHost控制台应用程序 [英] C# SelfHost Console app serving HTML

查看:437
本文介绍了提供HTML的C#SelfHost控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System.Web.Http.SelfHost等如何发送HTML页面?

目前,在Google Chrome浏览器中,它以文本形式显示.我找不到如何将标题更改为text/html的方法,也不知道是否能解决该问题.

我尝试了许多附件的变体,但没有成功.

Episode数据以Json的身份通过Google Chrome浏览器确定器进入浏览器,但在IE中,它询问我是否要(O)pen或(S)ave.

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

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

代码如下:-

Using System.Web.Http.SelfHost etc. how can I send a html page?

Currently, in Google Chrome it comes through as text. I cannot find how to change the header to text/html, and I do not know whether that will fix it.

I have tried a number of variations of the attached without success.

The Episode data comes through to the browser in Google Chrome Browser OK as Json, but in IE it asks whether I want to (O)pen or (S)ave it.

I want to send the html from RAM, not disk.

Error-handling omitted for brevity.

Code as follows :-

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测试数据为:


The HTML test data is :

<!DOCTYPE html>
   <html>
   <body>

       <h1>My First Heading</h1>

       <p>My first paragraph.</p>

   </body>
   </html>

推荐答案

答案似乎是:
response.Content.Headers.ContentType.MediaType ="text/html";
The answer appears to be :
response.Content.Headers.ContentType.MediaType = "text/html";


这篇关于提供HTML的C#SelfHost控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆