Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return html page from WebApi action?

I'm looking for a WebApi example where the default route will return a given html page to the caller. I've got the route and action set up as follows. I just want to send him the index.html page, not redirect, because he's in the right place.

http://localhost/Site      // load index.html

// WebApiConfig.cs
config.Routes.MapHttpRoute(
    name: "Root",
    routeTemplate: "",
    defaults: new { controller = "Request", action = "Index" }
);

// RequestControlller.cs
    [HttpGet]
[ActionName("Index")]
public HttpResponseMessage Index()
{
    return Request.CreateResponse(HttpStatusCode.OK, "serve up index.html");
}

If I"m using this wrong, what's the better approach and can you point me to an example?

WebApi 2 with .NET 4.52

Edit: Hmm, improved it, but getting json header back instead of page content.

public HttpResponseMessage Index()
{
    var path = HttpContext.Current.Server.MapPath("~/index.html");
    var content = new StringContent(File.ReadAllText(path), Encoding.UTF8, "text/html");
    return Request.CreateResponse(HttpStatusCode.OK, content);
}

{"Headers":[{"Key":"Content-Type","Value":["text/html; charset=utf-8"]}]}
like image 573
disassemble-number-5 Avatar asked Jun 29 '16 16:06

disassemble-number-5


People also ask

How do I return HTML page in Web API?

Use ControllerBase. Content() method returns a ContentResult object. This method has several overloads, and we will be using an overload that accepts two string parameters. The first string represents the content of the HTML while the last is the content-type which for HTML is "text/html" .

CAN REST API return HTML?

One of the core benefits of REST is its separation of representation (encoding) from the underlying resource being accessed. It's perfectly fine to return HTML if the client requests it as a preference via the Accept header.

How do I return a view from Web API action?

So, if you want to return a View you need to use the simple ol' Controller . The WebApi "way" is like a webservice where you exchange data with another service (returning JSON or XML to that service, not a View). So whenever you want to return a webpage ( View ) for a user you don't use the Web API.


2 Answers

One way to do this is to read the page as a string and then send it in a response of content type "text/html".

Add namespace IO:

using System.IO;

In the controller:

[HttpGet]
[ActionName("Index")]
public HttpResponseMessage Index()
{
    var path = "your path to index.html";
    var response = new HttpResponseMessage();
    response.Content =  new StringContent(File.ReadAllText(path));
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    return response;
}
like image 71
Marcus Höglund Avatar answered Oct 19 '22 01:10

Marcus Höglund


For ASP.NET Core (not ASP.NET Standard) then if it's a static html file (which it looks like), use the static resource options:

Static files in ASP.NET Core

like image 40
Vivek Avatar answered Oct 19 '22 02:10

Vivek