I have a client dashboard application, written in ASP.Net MVC 4 (not bound to this version, happy to upgrade). The website is hosted in Windows Azure.

Our clients would like to programmatically access this dashboard, via an API.

Affectively they would like to be able to perform all of the same functions which they normally carry out on the dashboard website, programmatically from code using an HTTP Restful Service.

....My instant reaction was to simply build an ASP.net Web API project, and separate out the shared services/libraries/components from the existing MVC project so that both the API and the MVC website can call the same code base.

Questions

  1. Is it possible to simply create Web API controllers within my existing MVC website project, and expose them over HTTP?
  2. If it is not possible to do "1.", will Azure play nicely if I have an MVC solution, a separate Web API solution, and a shared library project of common services and models? How will I ensure that Azure copies the shared library components into the cloud when I deploy the MVC solution and the Web API solution separately?

Update

Based on the comments, the following implementation of two separate controllers (the original MVC controller within the MVC project, and an additional Web API controller within the same project), does in fact work. Please note that based on the following LINKED ARTICLE, the below implementation would be a "pre-MVC 6" implementation. MVC 6 itself provides the ability to implement both API calls and normally MVC View calls, into a single controller (as opposed to separate controllers that inherit from different base classes).

public class ProductsController : Controller
{
    //Products/Index
    public ActionResult Index()
    {
        return View();
    }
}

public class ProductsAPIController : ApiController
{
    // GET api/productsapi/getall
    public IEnumerable<string> GetAll()
    {
        return new string[] { "value1", "value2" };
    }
}
share|improve this question
1  
What are you asking exactly? Whether you can add a Web API controller ot an ASP.NET MVC problem? Just Add New Item to find that, yes you can and scaffolding is also supported. You can find tutorials at ASP.NET's site. Shared libraries? A library is just an assembly. If your project has a reference to is, it will get deployed. – Panagiotis Kanavos Apr 27 '15 at 8:29
    
So how would I ensure that I can call both of them separately? Surely the endpoints will be different? For example if I type "dashboard.com/Products/Index" normally, what would the web api call look like? I'm assuming that it would default to "dashboard.com/api/ProductsAPI/GetAll?" (assuming I had a ProductsController for the MVC page and ProductsAPIController for the WEB Api calls?" – Goober Apr 27 '15 at 8:33
    
That's a routing question and should be asked separately. You can use route configurations for both, route attributes etc. Nothing demands that the same URL be used though. In fact, it's not good REST practice to use the same resource URL for both the API and the UI that uses the API – Panagiotis Kanavos Apr 27 '15 at 8:37
    
Did you see this article from Omar al Zabir? codeproject.com/Articles/233572/… – Tom Chantler Apr 27 '15 at 8:52

The newest version of MVC (6) has now integrated both technologies into one solution. http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6

This would be ideally what you are looking for if you can start a new project.

Question 1: Yes it is, just take a look at the article above.

Question 2: Azure will be fine if you have a MVC solution and also a WebApi solution, but I think you will need to run them under different websites (I'm not 100% on this).

share|improve this answer
    
As did the old one. The OP is asking only about having Web API and ASP.NET controllers in the same project, not about using one controller for both – Panagiotis Kanavos Apr 27 '15 at 8:33
2  
As for the edit - no, you don't need to run anything under different sites. You can run them on the same site already, in fact a lot of ASP.NET MVC SPAs call Web API services on the same site – Panagiotis Kanavos Apr 27 '15 at 8:36
    
@PanagiotisKanavos Even if they are in different solutions? – Jamie R Apr 27 '15 at 8:36
1  
1) Why should they be? There's no reason to 2) You can add a reference from the MVC project to a library containing Web API controllers. No need to use separate solution, just create an assembly used by anyone who wants it – Panagiotis Kanavos Apr 27 '15 at 8:38
    
@PanagiotisKanavos The why - OP has stated in his 2nd question. – Jamie R Apr 27 '15 at 8:39

To be fair - MVC or WebAPI, as far as your consuming clients are concerned, it shouldn't matter.

Both MVC and WebAPI can create JSON or XML outputs - it's just one makes it a bit easier (WebAPI serializes the relevant response based on the client).

But you can quite easily mix the two together, as others have said, it's as simple as Add New Item

share|improve this answer
  1. Yes it will work fine. The only potential gotcha is you have to clearly separate your WebAPI routes from your MVC routes (like, for instance having all WebAPI routes under a /api/... prefix, this is the default).

  2. If you do decide to do two separate projects for whatever reason and want to have a shared library, the best solution is to keep that library in a NuGet package on a private feed. TeamCity supports this out of the box, along with other nice features (continuous integration/deployment).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.