0

this is probably daft in many ways but I'm just starting with MVC as the asp .net web api looks really interesting.

Just thinking can a 'web api' also serve as a front end internet application? I'm guessing that the answer is 'no' but it could save lots of time to somehow get the API working with the front-end in the same place, what's the other option - create 1 project for the API and an another for the front end?

hyp
  • 1,370
  • 1
  • 9
  • 21
  • You mean create an api as a backend and then your front end calls the api? So technically your frontend could be javascript, or even windows or anything. Sounds sensible based on the **need** though. – gideon Mar 07 '12 at 16:51
  • Yes mostly that's what I mean but is there a way to do it all in a clever MVC style? Even more - 'can it be done from one project' ? Is it sensible is a different matter, is it possible? – hyp Mar 07 '12 at 16:53
  • I think it is. I mean even right now you can make a controller return `json` or `xml` and use it in the UI in the same project. So your `ApiController` should behave no different. About _a clever mvc way_ I'm not sure. – gideon Mar 07 '12 at 16:57

1 Answers1

0

Since MVC separates the view from the business logic and uses REST for data retrieval, you can build a MVC application and then just document the API standard for how other applications can interface with your application.

For example, your application has a view called Gadgets. Behind the scenes the view is really calling http://myapplication.com/gadgets/1 to get the first page of available gadgets and then loops through them to display the data in html format to the browser. In reality since http://myapplication.com/gadgets/1 should always return the first page of gadgets, you can say in your API documentation that calling http://myapplication.com/gadgets/1 returns the first page of gadgets. If I am the calling application then all I have to do then is call this url and parse out the result for display.

This is the beauty of MVC if you implement it correctly.

evasilchenko
  • 1,862
  • 1
  • 13
  • 26