Cemre Mengü
Cemre Mengü

Reputation: 18754

How do I create pages in a web api project?

I want to display a default status page for my web api project (where instead some IIS message is displayed when I start the project). However it seems like I cannot create views in web api (there is no support for ActionResult).

In addition to a status page I will also use this information to create an api documentation page.

How can I achieve displaying html pages in this situation ?

Upvotes: 2

Views: 3643

Answers (2)

MichaelS
MichaelS

Reputation: 3831

If your default status page is static html, you don't have to use MVC. Just tell WebApi in your Startup.cs that you want to support static resources:

app.UseFileServer();

For creating an API documentation, maybe you could write that file on startup dynamically?

Upvotes: 2

Stephen Brickner
Stephen Brickner

Reputation: 2602

You can create regular controllers and views in a webAPI project the same as any MVC project. Just create a normal controller that does not inherit from ApiController. In your startup.cs make sure to configure at least a default route.

 configuration.Routes.MapHttpRoute(
    name: "someName",
    routeTemplate: "{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Right click and select Add, then you should see controller at the top. Select one of the mvc controllers.

enter image description here

Upvotes: 0

Related Questions