ASP.NET MVC or Webapi?

Asked

Viewed 7,874 times

22

What is the advantage of using MVC and Webapi (using Visual Studio and C#)?

I think that developing in MVC is easier to assemble a form with validations, because just put special attributes for this purpose and use the Wizard that already mounts the form automatically. Does Webapi not have this feature or am I mistaken? When to use Webapi and/or MVC?

3 answers

28


They are separate things, so it is not a matter of being advantageous to use one or the other. They should be used for different purposes. Although they could be used together.

So much so that the ASP.NET Webapi has become an integral part of the ASP.NET Core. You no longer use one or the other because essentially they have different operations when you want to do one or the other (essentially the Webapi does not have view), but the basis is the same.

The MVC assumes that the user will access websites which are collections of pages to view. Webapi only returns results, it obviously have no views, the V of MVC.

Webapi has always needed a simpler structure, but if you think about it they need models (M) and they need, if not the controllers (C), something very similar. That’s why it makes more sense to use one framework single.

Working with Webapi is easier for a simple reason, it does much less. And precisely why if you want to present pages, Webapi is not an option. At least not alone.

Of course it is possible to use Webapi to pick up the results and assemble the pages with another technology. But I doubt it makes any sense to you. It can do to third party consumers of your API. By third party, understand other programmers or other applications made by you.

As the name says Webapi is for creating access Apis in the templates of web. API is for programmers to consume and as it suits them best. It is not to build websites whole.

You can even make the form entirely on the client side (type SPA). Then I wouldn’t need the MVC, but this doesn’t work well for websites, works best for applications.

So Webapi doesn’t assemble a form for you because it’s not your goal.

I think in this case you should use MVC.

Or if you’re going to do something simpler consider ASP.NET Webpages (ASP.NET Razor Pages even better). It’s curious how almost no one knows this. I see many programmers killing themselves to use ASP.NET MVC to do it websites very simple, simply because they do not know everything that exists in ASP.NET. This technology allows to do websites analogous to what is done in pure PHP using only what is already available by default in the language. Webpages/Razor Pages gives a whole infrastructure that helps a lot without imposing a development model.

Also consider using Blazor on the server side. Avoid using the classic ASP.NET MVC used in .NET Framework that died.

  • 3

    The next time I see a cute MVC defender with DDD or something I’m going to pass the Webpages link, then he mounts the entire site in hand. Great recommendation. + 1.

  • What is the Webpages? How can I create a project of the kind ASP.Net Webpages?

  • 1

    @Denercarvalho is what’s in link. A fairly extensive question to put in a comment. It is a simpler way to make websites that do not require great complexity. Basically you produce the individual pages without much ceremony or development model. It’s very free. It has the vew which is almost equal to ASP.Net MVC. It has some facilities to manipulate what comes from the customer, to assemble what he needs, but nothing as ready as in MVC. But it is more flexible and simple to use. It does not impose a model, but you can have if you want. The link of everything you do as you want.

  • 1

    @Denercarvalho can start here: https://www.asp.net/web-pages/overview/getting-started/introducing-aspnet-web-pages-2/getting-started

7

To Maniero’s answer, which I consider globally correct, I would just like to add that when he says:

Of course it is possible to use Webapi to pick up the results and assemble the pages with another technology. But I doubt it makes any sense to you. Can do for third party consumers of your API.

I would say that is not true in the situation where it is intended to use a rich FW in the frontend as Angular or React. Also, when you say:

API is for programmers to consume and do whatever you want, not to build entire websites.

I would also say that more and more Developers are building the whole server-side based on Web API (or any other type of webservices). In such cases, the frontend is typically drawn using Angular/React.

0

Well, I in my humble opinion agree with Maniero when we are referring to the Webapi, I believe we are unifying a service platform with a global access to it.

One builds a layer of services that N distinct platforms can query is a web application, or mobile platform, or platforms of different technologies. The fact that you are mounting a form using a MVC application only, you have established a unique access to your form and records are not shared with other platforms.

If you want to extend your access to data globally, a Webapi structure is necessary, thus making it a unique and global access, reducing the effort on application development

Browser other questions tagged

You are not signed in. Login or sign up in order to post.