Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# 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()},
                };
            }
        }
    }
The HTML test data is :
 <!DOCTYPE html>
    <html>
    <body>
 
        <h1>My First Heading</h1>
 
        <p>My first paragraph.</p>
 
    </body>
    </html>
Posted 4-Oct-12 19:34pm
Edited 4-Oct-12 19:35pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The answer appears to be :
response.Content.Headers.ContentType.MediaType = "text/html";
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 595
1 OriginalGriff 295
2 CPallini 210
3 PIEBALDconsult 150
4 Fredrik Bornander 140
0 OriginalGriff 5,835
1 Sergey Alexandrovich Kryukov 5,263
2 CPallini 4,750
3 George Jonsson 3,227
4 Gihan Liyanage 2,487


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100