Explain Codes LogoExplain Codes Logo

Return HTML from ASP.NET Web API

web-development
content-result
http-status-code
web-api
Anton ShumikhinbyAnton Shumikhin·Aug 30, 2024
TLDR

ASP.NET Web API can return HTML via an HttpResponseMessage with StringContent. By specifying the response's MediaTypeHeaderValue as text/html, the browser will properly render the HTML. Here's a quick code illustration:

public HttpResponseMessage GetHtml() { var html = "<html><body><h1>Welcome!</h1></body></html>"; return new HttpResponseMessage() { Content = new StringContent(html, Encoding.UTF8, "text/html") }; }

The client receives this HTML ready for immediate display.

Tailoring responses for Legacy ASP.NET MVC Web API and ASP.NET Core

Content delivery with ASP.NET Core

ASP.NET Core simplifies HTML response generation with ContentResult. By assigning text/html to the ContentType property, ASP.NET Core will correctly interpret the HTML without the need for further serialization. Example:

public ContentResult GetHtmlCore() { var htmlContent = "<html><body><h1>Hello, ASP.NET Core!</h1></body></html>"; //Step aside gordon ramsay, chef ASP.NET Core is in the house! return new ContentResult { Content = htmlContent, ContentType = "text/html", StatusCode = (int)HttpStatusCode.OK }; }

Here, the built-in Content method in controllers facilitates direct and hassle-free HTML delivery.

HTML embedding in Legacy ASP.NET MVC Web API

For the Legacy ASP.NET Web API, HTML embedding in the Content property of a HttpResponseMessage should be cautious. Ensuring a correct MediaTypeHeaderValue is like telling webpage what clothes to wear to the party. Here's how:

public HttpResponseMessage GetHtmlLegacy() { var htmlContent = "<html><body><h1>Welcome to Legacy ASP.NET!</h1></body></html>"; // Look ma, embedding HTML in HttpResponseMessage var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(htmlContent, Encoding.UTF8, "text/html") }; return response; }

Making the right content choice

HttpResponseMessage VS ContentResult

While HttpResponseMessage is a tried-and-tested choice for Web API, ContentResult offers a simplified path in ASP.NET Core 2.0 and beyond. So, choose wisely.

Content Negotiation and Serialization

ContentResult skips the regular content negotiation and serialization process. Thus, this direct approach optimizes performance when returning HTML or plain text, bypassing the need for JSON or XML serialization.

Media Type Setting

Regardless of your approach, setting the correct media type is pivotal for smooth client-side operations. Use text/html for HTML content to ensure the browser displays it as a webpage.

Watch out for pitfalls and adhere to best practices

Encoding Matters

Always use proper encoding, i.e., UTF-8, when creating StringContent. Ignoring this may invite gobbledygook or even security issues.

StatusCode Management

Be explicit with your StatusCode. An accurate status code partnered with the HTML tells the client-side application a complete story.

Compatibility Concerns

Be it Legacy Web API or ASP.NET Core, solutions must be compatible across versions. Follow practices that ensure seamless transitions.