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);
    }
 }
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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Its not supporting in ASP.NET MVC Core HttpResponseMessage
@Parshuram I've just checked your statement. I can use HttpResponseMessage in ASP.NET Core. It is located under System.Net.Http.
ohk thanks but now MediaTypeHeaderValue not supporting
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 }"
@guyfromfargo have you tried [Produces] approach?
|
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>"
    };
}

5 Comments

I could not get the "produces" answer to work at all on 2.0, this however works fine.
If you want to show a html from file, just add "var content = System.IO.File.ReadAllText("index.html");"
Yup, if you are using ASP.NET Core 2.0 this is the way to go!
What if the HTML file is in the local directory and it also has css, js linked. How do we serve the file then?
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.

Your Answer

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.