168

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 class MyController : ApiController
{
 [HttpPost]
 public HttpResponseMessage Post()
 {
 Response.Write("<p>Test</p>");
 return Request.CreateResponse(HttpStatusCode.OK);
 }
 }
Andrei
44.9k39 gold badges163 silver badges227 bronze badges
asked Nov 8, 2014 at 21:25
5
  • 4
    Why are you using WebAPI if you want to return HTML? I mean this is what ASP.NET MVC and ASP.NET WebForms are for. Commented Nov 8, 2014 at 22:06
  • Thank you, excellent. I changed controller to regular controller. Commented Nov 9, 2014 at 21:29
  • 23
    @Stilgar One reason could have been that he does not use the MVC stack, neither any rendering engine but still want to provide a server facade to some Html. A use case can be that you have a Web Api that give some Html with a client side templating engine that will render everything in a later stage. Commented Jan 22, 2015 at 16:20
  • 3
    @Stilgar Another use case I encountered is returning an html page to provide feedback for an account creation confirmation, when the user clicks on the link you provide through email Commented Jun 26, 2017 at 10:43
  • Own use case: Provide styled / transformed XML document in HTML format Could be XSLT 2.0 transforms which is not support on NET 7 but is on NET Framework 4.8 Commented Aug 28, 2023 at 14:18

2 Answers 2

351

ASP.NET Core. Approach 1

If your Controller extends ControllerBase or Controller you can use Content(...) method:

[HttpGet]
public ContentResult Index() 
{
 return base.Content("<div>Hello</div>", "text/html");
}

ASP.NET Core. Approach 2

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 = "<div>Hello World</div>"
 };
}

Legacy ASP.NET MVC Web API

Return string content with media type text/html:

public HttpResponseMessage Get()
{
 var response = new HttpResponseMessage();
 response.Content = new StringContent("<div>Hello World</div>");
 response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
 return response;
}
answered Nov 8, 2014 at 22:03
6
  • 1
    Its not supporting in ASP.NET MVC Core HttpResponseMessage Commented Nov 23, 2016 at 10:45
  • @Parshuram I've just checked your statement. I can use HttpResponseMessage in ASP.NET Core. It is located under System.Net.Http. Commented Nov 23, 2016 at 13:08
  • ohk thanks but now MediaTypeHeaderValue not supporting Commented Nov 23, 2016 at 13:35
  • 5
    When I do this using ASP.NET MVC 5 I get the response. I don't get any of the HTML content back. All I receive is "StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers: { Content-Type: text/html }" Commented Oct 3, 2017 at 19:54
  • 1
    @guyfromfargo have you tried [Produces] approach? Commented Oct 3, 2017 at 21:41
66

Starting with AspNetCore 2.0, it's recommended to use ContentResult instead of the Produce attribute in this case. See: https://github.com/aspnet/Mvc/issues/6657#issuecomment-322586885

This doesn't rely on serialization nor on content negotiation.

[HttpGet]
public ContentResult Index() {
 return new ContentResult {
 ContentType = "text/html",
 StatusCode = (int)HttpStatusCode.OK,
 Content = "<html><body>Hello World</body></html>"
 };
}
answered Oct 18, 2017 at 3:27
5
  • 4
    I could not get the "produces" answer to work at all on 2.0, this however works fine. Commented Nov 1, 2017 at 13:09
  • 1
    If you want to show a html from file, just add "var content = System.IO.File.ReadAllText("index.html");" Commented Feb 25, 2018 at 15:13
  • 4
    Yup, if you are using ASP.NET Core 2.0 this is the way to go! Commented May 3, 2018 at 14:19
  • What if the HTML file is in the local directory and it also has css, js linked. How do we serve the file then? Commented Apr 8, 2020 at 9:40
  • For Razor Pages, you can call the PageModel Content() method instead of creating ContentResult directly. I'm not sure if this is available for Controllers as well. Commented Jun 6, 2020 at 0:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.