Returning HTML or any file using Web API

I will start by mentioning that Web API is not meant to deliver HTML pages. If you wish to build a website please use regular MVC - or something else. With that being said, you can have different reasons for having to do this.

Below is an example of a controller that returns the content of a html file.

public HttpResponseMessage HelloWorld()
   var fileContents = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Content/HelloWorld.html"));
   var response = new HttpResponseMessage();
   response.Content = new StringContent(fileContents);
   response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
   return response;

The above example is getting the path for the file through the HttpContext using MapPath. If you are not using IIS, then maybe you will only need to use Directory.GetCurrentDirectory().

var fileContents = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "Content/HelloWorld.html"));

For this project I have the below structure in my project. I can get the html file by using the endpoint http://localhost:52774/api/web/helloworld

Structure of the project