0

I've done a lot of searching on this, and I know it's a common discussion point on this site, but none of the things I've found answer my (seemingly simple) question.

My situation is that I have an MVC web application running in IIS and I've recently started to build a WebAPI to provide a CRUD interface to the underlying data model.

These are separate projects in Visual Studio and run fine in isolation.

My issue is how to deploy them to IIS and how to configure it so

http;//myhost/Customers/Report

calls the MVC stuff but

http//myhost/api/Customers/123

calls the WebAPI.

I've seen various examples of deploying a WebAPI but they quietly include a different port number into the URL. I wanted to avoid something like this ...

http//myhost:8080/api/Customers/123

Is there a common pattern for this in terms of IIS config (nested application in the MVC tree, or a virtual directory)?

and/or

Do I need to put some code in the routing of the MVC application to direct to the WebAPI one?

Any help or referrals appreciated.

(FYI : Just two examples of what I've read are below but I've read about 20 articles so far)

How to add Web API to an existing ASP.NET MVC 4 Web Application project?

Problems (retroactively) adding Web API to asp.net MVC project

Community
  • 1
  • 1
chrisward
  • 45
  • 8

1 Answers1

0

Putting up the Route configuration should help you. Register WebApi route path first then the MVC application routes so that API is handled with web API and if its not API then it goes to MVC controller. This works for me.

This link might help on how to create virtual directories.

Guanxi
  • 3,103
  • 21
  • 38
  • Thank you for the quick reply. This actually narrows my question a bit. I have quite a lot of routing entries in the WebApiConfig.xml of my WebAPI project, do I need to "transplant" that into the core MVC project config? It certainly feels like I need to let the MVC stuff know how to deal with the "/api" URLs. Didn't know if this got sussed out by IIS by publishing projects (i.e. did it somehow maintain an master routing config) – chrisward May 13 '14 at 15:22
  • WebApiConfig.xml => WebApiConfig.cs – chrisward May 13 '14 at 15:40
  • Yes, keep one route configuration if both are hosted on same server, as the route that is registered first will handle your request first. – Guanxi May 13 '14 at 15:47
  • Thanks. I'll go and poke around and declare let see if it sorts it. Would there be an issue with running the WebAPI stuff in a separate Application Pool? If this is the only way I can go it means that the two project are bound together which is a shame. – chrisward May 13 '14 at 16:05
  • This is not the only way. You can work with creating different virtual directory / application on your server. All this will be IIS configuration. And yes you can keep this in separate application pool. – Guanxi May 13 '14 at 17:37
  • Thanks again. It's exactly that sort of thing that I want to do, just wasn't sure it was possible. It's precisely that sort of IIS configuration help I am looking for. Can't seem to find it but I could be phrasing my searches incorrectly. Any additional pointers would be very much appreciated. – chrisward May 14 '14 at 08:15
  • I have added one link in answer, if that doesnt help, that can atleast give you idea of what to look for. – Guanxi May 14 '14 at 18:22