2

I'm working on a webApi application, and the api I want to get is the following:

  • api/v1/lists/?params...
  • api/v1/meta/?params...
  • api/v1/debug

There are ApiControllers that work fine for "lists" and "meta" routes, but api/v1/debug should return a static html page.

I could think of two ways of implementing it:

  1. via special controller that would return something like View("mypage.html") or
  2. configure routes in WebApiConfig to redirect requests like api/v1/debug to mypage.html

However, I couldn't get working version of any of these ways: there is no content type for HttpResponseMessage like HtmlContent or similar, so I can't do smth like

config.Routes.MapHttpRoute(
    name: "Debug",
    routeTemplate: "api/v1/debug/",
    defaults: new { controller = "Debug" }
    );

...

public class DebugController:ApiController
{
    public HttpResponseMessage Get()
    {
        return new HttpResponseMessage()
        {
            Content = new HtmlContent("mypage.html");
        };
    }
}

and I can't get right redirection in WebApiConfig:

//this results into 400 status code:
config.Routes.MapHttpRoute("Default", "api/v1/debug/", "~/mypage.html");

Could you please tell me what would be the right solution for this problem? And is it possible at all to combine static html pages with Action Results in WebApi application?

ZuoLi
  • 383
  • 2
  • 14
  • I think HtmlContent expects HTML text, not a file. You could read the file with a streamreader, and return the contents... I don't know of anything that expects to return HTML content directly like you expect... but I'm not a Web API expert. – Brian Mains Dec 10 '14 at 16:48
  • @BrianMains, thanks for comment, but as I mentioned, there is no such a class as HtmlContent at all, I just made it to show what I want to get ideally :) – ZuoLi Dec 10 '14 at 16:50

2 Answers2

1

The best option is to use MVC for rendering html pages. WebAPI and MVC can live together. Note that they use different route types.

As a quick hack (using WebAPI), you can read a file and write its contents to HttpResponseMessage like this:

// this is a controller method
public HttpResponseMessage CreateResponseFromFile()
    {
        var content = File.ReadAllText("yourfile.html");
        if (content == null)
            throw new HttpResponseException(HttpStatusCode.NoContent);

        var response = new HttpResponseMessage
        {
            Content = new StringContent(content)
        };
        response.Content.Headers.Add("Content-Type", "  text/html");  // note this line to set content type
        return response;
    }
Rast
  • 2,341
  • 4
  • 20
  • 29
0

Well, I've found another way to get what I want, so I'll just leave it here in case it could help someone.

Kind of dirty hack that could help to follow general route rules is to add the following section in your web.config:

<configuration>
  <system.web>
    <urlMappings enabled="true">
      <add url="~/api/v1/debug/" mappedUrl="~/mypage.html" />
    </urlMappings>
  </system.web>
</configuration>
ZuoLi
  • 383
  • 2
  • 14