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);
}
}
-
4Why are you using WebAPI if you want to return HTML? I mean this is what ASP.NET MVC and ASP.NET WebForms are for.Stilgar– Stilgar2014年11月08日 22:06:34 +00:00Commented Nov 8, 2014 at 22:06
-
Thank you, excellent. I changed controller to regular controller.Andrus– Andrus2014年11月09日 21:29:43 +00:00Commented 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.Patrick Desjardins– Patrick Desjardins2015年01月22日 16:20:27 +00:00Commented 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 emailanaitslimane– anaitslimane2017年06月26日 10:43:16 +00:00Commented 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.8plykkegaard– plykkegaard2023年08月28日 14:18:13 +00:00Commented Aug 28, 2023 at 14:18
2 Answers 2
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;
}
-
1Its not supporting in ASP.NET MVC Core HttpResponseMessageParshuram Kalvikatte– Parshuram Kalvikatte2016年11月23日 10:45:52 +00:00Commented 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.Andrei– Andrei2016年11月23日 13:08:15 +00:00Commented Nov 23, 2016 at 13:08
-
ohk thanks but now MediaTypeHeaderValue not supportingParshuram Kalvikatte– Parshuram Kalvikatte2016年11月23日 13:35:25 +00:00Commented Nov 23, 2016 at 13:35
-
5When 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– guyfromfargo2017年10月03日 19:54:11 +00:00Commented Oct 3, 2017 at 19:54
-
1@guyfromfargo have you tried
[Produces]
approach?Andrei– Andrei2017年10月03日 21:41:39 +00:00Commented Oct 3, 2017 at 21:41
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>"
};
}
-
4I could not get the "produces" answer to work at all on 2.0, this however works fine.philw– philw2017年11月01日 13:09:56 +00:00Commented Nov 1, 2017 at 13:09
-
1If you want to show a html from file, just add "var content = System.IO.File.ReadAllText("index.html");"Pavel Samoylenko– Pavel Samoylenko2018年02月25日 15:13:24 +00:00Commented Feb 25, 2018 at 15:13
-
4Yup, if you are using ASP.NET Core 2.0 this is the way to go!James Scott– James Scott2018年05月03日 14:19:49 +00:00Commented 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?Lingam– Lingam2020年04月08日 09:40:54 +00:00Commented 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.carlin.scott– carlin.scott2020年06月06日 00:39:28 +00:00Commented Jun 6, 2020 at 0:39
Explore related questions
See similar questions with these tags.