Angular + ASP.NET MVC: Does it make sense?

Asked

Viewed 8,500 times

4

I’m starting to study Angular2.

As I am working a lot with . NET I decided to do a project with ASP.NET MVC 4 and Angular2.

Giving a read on hello world of Angular I noticed that Angular has its own route mechanism, which in a way would render useless the same mechanism of ASP.NET MVC, certain?

  1. It makes sense that I have an ASP.NET MVC controller to display only one view?
  2. the use of ASP.NET MVC, when used together with Angular, tends to be the "function" of`Webapi?
  3. From an architectural point of view, it makes sense that I have ApiController and Controller within the same scope?
  • A detail: Angular2 is not Angularjs.

  • I think there’s been a little mix-up. ASP NET will backend, so the backend routes will be the routes of the localhost/Rest/user/register services that will receive a json *(or xml or whatever you prefer) to make the persistence in the database. In the front the angular will have its route localhost/site/user/registration where will be displayed the html page with the cute form for the user to fill, with the angular javascript validating the form...

  • But he meant about using ASP.NET MVC itself, not Webapi @Bartolomeus.Gusella

3 answers

7


It makes some sense. One thing does not eliminate the other. If it makes a lot of sense there is already questionable. Maybe it is more important to ASP.NET Webapi.

If your application actually has a single page, ASP.NET MVC will be quite useful, it will only generate one page. But this page will make numerous requests for information to the server, and the normal thing would be to answer them with the Webapi.

If you are using the .NET Core then you don’t even have to worry because the MVC and Webapi became one thing.

There’s a question about web technologies. NET.

There’s another question that compares the use of MVC with SPA.

And finally there’s a question that talks about the Webapi.

Note that these Angular2 routes and ASP.NET MVC routes have no direct link. One will run on the client (browser) and the other on the server. They are different purposes. One technology does not compete with the other, they are complementary.

Obviously, Angular2 routes will eventually lead to Webapi routes. Angular2 routes can work completely independently. If he can fix everything on the client, he doesn’t ask for anything on the server. When he needs something from the server a request will be made, then the Webapi routes go into action on the server to determine the controller suitable to call and provide what you need.

Understand that the use of Angular2 is to minimize access to the server, do not think of the two technologies as something that forms a single thing, are separate things. Even if you do it right and one day you want to trade Angular2 for React or other technology, it is to achieve without changing anything on the server. The server doesn’t even need to know you’re using Angular2.

It makes sense that I have a Controller in ASP.NET MVC to display only one view?

For the page I think doesn’t make much sense, makes having the controller for the webapi.

The use of ASP.NET MVC, when used together with angular, tends to be the "function" of the Webapi?

Exactly.

From an architectural point of view, it makes sense that I have ApiController and Controller within the same scope?

It does, if you use MVC, obviously it doesn’t if you don’t use MVC. If you use . NET Core you don’t have to worry about it.

Consider the use of Blazor.

  • [...] One will run on the client (browser) and the other on the server [...]. Big, there is no difference in the specific case of targeting and display of views, there is?

  • @Marllonnasser did not understand your doubt.

  • I guess I’m not getting the difference between the routes run on the client and the server. Do I need both? Is one not enough? I can’t understand the purpose of mvc’s route along of the angle route2.

  • @Marllonnasser needs both. I edited it to make it clearer

  • Okay, big.. It’s clearer. Thank you!

1

It makes sense that I have a Controller in aspnet mvc to display only a view?

Yes, you can use both, but in my vision, No, it doesn’t make any sense use MVC with SPA app - be angular, React, whatever.

If you analyze a little, you will see clearly two problems - I believe you have already seen them, that’s why you came here to ask.

  1. The layer of VIEW MVC, using Razor, will "conflict" with your Angular views. At first you notice that one does not suit the other, or that there is no need to coexist.
  2. Architecturally is also not healthy. Every web project should be thought of in a Cloud First. Your front-end - angular - web application must be independent of your back-end web application - ASP.NET. So you can host and scale your web application differently/dynamically. Without one depending on or being hosted within the other.

The use of aspnet mvc, when used together with angular, tends to to be the "function" of the Webapi?

Yeah, it doesn’t make sense to use MVC, but only ASP.NET Webapi. Because if you use MVC, it says that you have to load all libs and dlls referring to Views, like the libs of Razor, Optimization, Webpages, etc. And you will not use any of this. So if it’s a Webapi project, don’t load any MVC, make your project lighter.

Even in ASP.NET Core, if you’re only going to use as a Webapi, you don’t need to add dependencies to View features.

From an architectural point of view, it makes sense that I have Apicontroller and Controller within the same scope?

If you are going to create an MVC application, and from within your View you only want to make REST requests, it does make sense. But if you are going to segregate this in a SPA web application, and the backend is just a Restful API, then there is no need, it is even better to remove as explained above.

-1

Hello,

It makes sense to me to have both technologies together. Content that you would like to be indexed by Google needs to be rendered on the server, so MVC would help at this point.
System areas (website) that do not need indexing, it is interesting to use Spas.

  • If you can clarify a little more I think it will help who asked the question

Browser other questions tagged

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