Daniel van Niekerk
Daniel van Niekerk

Reputation: 168

Adding WebApi to existing website in IIS

I have an Angular website hosted in IIS. I'm adding Asp.net WebApi 2 as the server.

What I did is point my IIS default website to the root of the Angular site. All is working there. Then I added the WebApi as an application to the site. However my problem is that since the default routes of WebApi starts with /api and I have to provide an alias when adding WebApi as an application my URLs end up looking like this: (assume my alias for the WebApi application is 'api')

http://localhost/api/api/controller

How can I add WebApi to the existing site and have my routes without the double '/api/?

I can provide custom routes to my controllers like this:

[RoutePrefix("controller")]

This overrides the route from '/api/controller' to just '/controller' resulting in the correct behaviour. But this feels hacky and I'll have to do it to every controller.

Upvotes: 0

Views: 1424

Answers (1)

Daniel van Niekerk
Daniel van Niekerk

Reputation: 168

I managed to solve the problem by removing the '/api/' before '{controller}' in the routeTemplate property when configuring the WebApi routes:

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

Upvotes: 1

Related Questions