How to return HTML from ASP.NET MVC Web API controller?
I tried the code below but got compile error since Response.Write is not defined:
public clas
If your Controller extends ControllerBase or Controller you can use Content(...) method:
[HttpGet]
public ContentResult Index()
{
return base.Content("Hello", "text/html");
}
If you choose not to extend from Controller classes, you can create new ContentResult:
[HttpGet]
public ContentResult Index()
{
return new ContentResult
{
ContentType = "text/html",
Content = "Hello World"
};
}
Return string content with media type text/html:
public HttpResponseMessage Get()
{
var response = new HttpResponseMessage();
response.Content = new StringContent("Hello World");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}