Hi Team
am learing the MVC Recently . can any one give the clarification for the below things:
1) difference between WebApi and action method.
2) can we call action method without webapi?
3) web api is only need to call while passing or getting info
it would be great if any one explain me on "SUMBIT" button click example.
Please help me.
Sachin SinghPosted Jan 24, 2022, 1:51 PM
See,
MVC is an architectural pattern , that helps us to organize the project. Means, classes/business logic can go to model folder, html can go to view folder and data-binding logic can go to controller.
for example spring MVC is used by java guys to develop web apps, which is based on MVC pattern.
similarly, Microsoft has provided us ASP.NET MVC/ ASP.NET Core MVC to develop web apps using MVC pattern.
Now, come to web API
First, tell me what do you mean by the interface in general? Ok, I give you examples of the interface, GUI is a graphical user interface. Why it came into the picture? because normal users are very different from technical developers of any application. Why windows operating system is famous? Cause anybody can use it easily, it has a start button, and everything is visible, the best part is the user does not need to know the internal mechanism, they just click and boom gets the result. This is the purpose of any interface in the world. An interface hides the complexity from a normal user.
Now come to the developer's world,suppose you are a java developer and I am a dot net developer, I have developed a wonderful application where a user logs in and by his location the app gives him the nearest bars location and related details. You liked this app and you want to use such a service in your java application, what will you do? You ask a favor to me to give my code. But you can't integrate it in your application cause it's written in .Net, and this is the first problem, the second problem is, why should I give you my code, I will simply give you an interface but I know you are a programmer, not a nontechnical person, so I will give you an interface which at least you as a programmer could understand and my code will still be hidden from you, you can only use the methods that I had created but can not see how actually it is working, and you really don't need it as well, you just need an interface though not graphical , a simple interface that you understand, such interfaces for programmers by programmers are called Application programming interface. No matter which device, no matter which programming language. An interface for all. For example, google map service is an API, If you want to integrate it into your application, then you do not need to know how is it working, you just need to call the methods by simple URLs like http://googlemap/nearestbars. Here nearest bars is actually a method which is representing some resources but for you googlemap is an interface and every method is like buttons on windows.
To create web APIs easily, Microsoft provided us the Web API framework, which is again based on MVC pattern, as an API can't have view, so the view folder is not used. you will only find the model and controller here.
follow my blog to learn MVC/web API
https://www.sharpencode.com/
Murali PPosted Jan 24, 2022, 3:40 PM
Ranganath PrasadPosted Jan 24, 2022, 2:38 PM
Hi Lakshmi,
I believe you are pretty new to MVC/ WEB API world. So, let me try to explain you in layman's terms.
You might have seen stackoverflow website right ( ofcourse, without this website, a developer's life is hell ). That website is built using ASP.NET MVC ( just google "technologies behind stack overflow" ).
ASP.NET MVC is just one of the many ways you can create a web application. People like me and the guys who built stackoverflow might have chosen ASP.NET MVC to build a website because of the clean architecture it is based on ( MVC architecture ). You dont have to spend a lot of time understanding this architecure at this point of time. Just create a basic MVC application and observe how the data flows between ( MODEL - VIEW - CONTROLLER ).
Now, coming to WEB API. This is like a small brother to ASP.NET MVC which is used to create Microservices ( some people call it WebApi , some call it Web-based services, some call it RESTful APIs ). Now earlier you have opened stackoverlow in your browser and saw that beautiful webpages right? Now, try this url - https://catfact.ninja/fact. What did it return ? A web page or just some json data ? It returned Json data right? why? because it is a Microservice. A microservice returns the data that can be used anywhere ( in a web app, mobile app, ..... ). So, you chose ASP.NET MVC to build websites. You chose ASP.NET WEB API to build microservices. And, someone might tell you that you can return just data in MVC and no need of a Web Api. Well you can, because as i said MVC is like a big brother to WebApi and you can either return a web page or just data from MVC. Now, from your submit button, you can call anything.. a MVC action method or Web Api action method.
Build some small web apps and microservices - you will understand the differences better