I have a self-hosted WCF service running as a windows service using the WebAPI to handle the REST stuff and it works great.
I realise that I should really use IIS or similar to dish out actual web pages, but is there ANY way to get a service call to return "just" html?
Even if I specify "BodyStye Bare", I still get the XML wrapper around the actual HTML, ie
<?xml version="1.0" encoding="UTF-8"?>
<string> html page contents .... </string>
[WebGet(UriTemplate = "/start", BodyStyle = WebMessageBodyStyle.Bare)]
public string StartPage()
{
return System.IO.File.ReadAllText(@"c:\whatever\somefile.htm");
}
Is there any way to do this or should I give up?
The bodystyle attribute has no effect on WCF Web API. The example below will work. It's not necessarily the best way of doing it, but it should work assuming I didn't make any typos :-).
[WebGet(UriTemplate = "/start")]
public HttpResponseMessage StartPage() {
var response = new HttpResponseMessage();
response.Content = new StringContent(System.IO.File.ReadAllText(@"c:\whatever\somefile.htm"));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
It would probably make more sense to read the file as a stream and use StreamContent instead of StringContent. Or it is easy enough to make your own FileContent class that accepts the filename as a parameter.
And, the self-host option is just as viable way to return static HTML as using IIS. Under the covers they use the same HTTP.sys kernel mode driver to deliver the bits.