2012-10-04 3 views
1

System.Web.Http.SelfHost 등을 사용하여 어떻게 브라우저에 html 페이지를 보낼 수 있습니까?html을 C# SelfHost 콘솔 응용 프로그램으로 보내는 방법

현재 Google 크롬에서는 텍스트로 제공됩니다. 헤더를 text/html로 변경하는 방법을 찾을 수 없으며 수정할지 여부를 알지 못합니다.

첨부 파일의 여러 변형을 성공하지 못했습니다.

에피소드 데이터는 Google 크롬 브라우저의 브라우저로 전달됩니다. Json과 마찬가지로 확인은 가능하지만 IE에서는 내가 (O) 펜을 사용할지 또는 (A) 사용할지 묻습니다. 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> 

답변

1

대답은 것으로 나타납니다 response.Content.Headers.ContentType.MediaType = "text/html과 ";

관련 문제