1

I am developing a web application , and the idea is to use ASP.Net MVC. The various web pages will rely on the data fetched from the DB , like list of employees , list of products.

  1. One way is we create a webAPI project , and expose the methods like GetEmployees(), and where ever on the view we need ,we directly fetch it through this web api.

But what if we don't create any web api project at all ? Instead in my MVC controller classes , i write methods that return a list of employees/products , which are then used on views through the AJAX .

What could be the possible drawbacks of this approach ?

Thanks

Munish
  • 59
  • 5
  • 1
    Possible duplicate of [Difference between MVC 5 Project and Web Api Project](https://stackoverflow.com/questions/22589245/difference-between-mvc-5-project-and-web-api-project) – Oscar Mar 05 '18 at 14:58
  • Don't know if I understood you correctly. but with ASP.NET MVC you return Views from your controller, which means it's the whole HTML-markup, whereas Web API can return business data as JSON or XML, which is way more processable than HTML – schlonzo Mar 05 '18 at 14:58
  • I think that this question is not suitable for stackoverflow (primarily opinion based) but, to add a comment, you do not want to be adding methods to your controller classes. Controllers should be lightweight and, typically, we want at least one level of abstraction between controllers and model data. See articles about the Repository Pattern and Service Layer, etc.. – Andy G Mar 05 '18 at 15:02
  • I agree but i can return JSON data also through the controller methods...in that case it behaves just like WebAPI. – Munish Mar 05 '18 at 15:03
  • I would definitely keep the distinction between ASP.NET MVC controllers returning views, and WebAPI returning data/resources. Microsoft might allow/enable the blurring of the lines, but we shouldn't. – Andy G Mar 05 '18 at 15:05
  • I agree that the distinction between MVC and WebAPI should be maintained. – Munish Mar 06 '18 at 11:26

2 Answers2

4

Yes you could create an APIController with GetEmployees() actions returning json or xml in your Asp.NET MVC application but it's not made for that.

I advise you to make a solution with tree projects :

  • a class library who contains entities and access methods
  • an ASP.net MVC application referencing the class lib, and rendering Html webpages.
  • an ASP.net API application referencing the class library too, and rendering entities dumps

Making this, you can make lots of calls on your API without disturbing your web application and vice versa

GGO
  • 2,678
  • 4
  • 20
  • 42
1

Learn About ASP.NET MVC

ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable, agile development. ASP.NET MVC includes many features that enable fast, TDD-friendly development for creating sophisticated applications that use the latest web standards.

Learn About ASP.NET Web API

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

The Core difference here is the intended use-case. Yes you can implement a RESTful service with MVC and you can return HTML Views from Web API. Especially in .net Core they have become more similiar.

None the less Microsoft's developers haven't done this for nought and staying with the intended use will mainly be convenient for you.

So concerning your question:

If you want to actively return Websites which make use of ajax calls use mvc.

If you want to separate Client / Server for example by deploying a Single Page Application use a Web API as your backbone.

Severin Jaeschke
  • 661
  • 7
  • 22