Angular + ASP.NET MVC: Makes sense?

4

I'm starting to study Angular2.

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

Reading the hello world of the Angular I noticed that the Angular has its own route mechanism, which would in a way render the same ASP.NET MVC mechanism useless, right? >

  • It makes sense to have a controller in ASP.NET MVC to display only a view ?
  • Using ASP.NET MVC, when used in conjunction with Angular, tends to be the "function" of WebApi?
  • From an architectural point of view, it makes sense to have ApiController e Controller within the same scope?
  • asked by anonymous 10.02.2017 / 03:20

    3 answers

    7

    It makes some sense. One thing does not eliminate the other. If it makes a lot of sense then it's already questionable. Perhaps more important is ASP.NET WebAPI.

    If your application really has a single page ASP.NET MVC will be very useless, it will only generate one page. But this page will make a lot of information requests to the server, and the normal one would be to respond with the WebAPI.

    If you're using .NET Core there's no need to worry about MVC and WebAPI became one thing .

    You have a question about #.

    You have another question that compare the use of MVC with SPA .

    And finally there's a question that talks about WebAPI .

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

    Obviously, routes from Angular2 will eventually lead to WebAPI routes. The routes of the Angular2 can operate completely independently. If you can solve everything in the client, it does not ask anything for the server. When it needs something from the server a request will be made, then the WebAPI routes take action on the server to determine the appropriate controller to call and provide what it needs.

    Understand that using Angular2 is to minimize access to the server, do not think of the two technologies as something that forms one thing, they are different things. Even if it is done right and one day you want to change the Angular2 by React or another technology, it is to achieve without changing anything on the server. The server does not even need to know you're using Angular2.

      

    Does it make sense to have a Controller in aspnet mvc to display only one view?

    For the page I think it does not make much sense, it does have the controller for the WebAPI.

      

    Does using mvc aspnet, when used together with angular, tend to be the "function" of WebApi?

    Exactly.

      

    From the architectural point of view, does it make sense for me to have ApiController and Controller within the same scope?

    Even if you do, if you use MVC, obviously you do not do it if you do not use MVC. If you use .NET Core you do not even have to worry about it.

        
    10.02.2017 / 11:39
    1
      

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

    Yes, you can use both, but in my view, no, it makes no sense to use MVC with SPA application - be angular, react, whatever / p>

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

  • MVC's VIEW layer, using Razor, will "conflict" with your Angular views. Of face you note that one does not match with the other, or that there is no need to coexist.
  • Architecturally is also not healthy. Every web project should be thought of Cloud First . Your front-end web application - angular - should be independent of your back-end web application - ASP.NET. So you can host and scale your web application in a different way / dynamic. Without a dependency or being hosted within the other.
  •   

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

    Well, it does not make sense to use MVC, and only ASP.NET WebAPI. Because if you use MVC, this says that you will have to load all the libs and dlls related to Views , like the Razor libraries, Optimization, WebPages, etc. And you will not use any of that. So if it's a WebAPI project, do not load anything MVC, let your project lighter.

    Even in ASP.NET Core, if it is to be used only as WebAPI, you do not need to add dependencies to View resources.

      

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

    If you are going to create an MVC application, and from inside your View you want to do only REST requests, it does make sense. But if it is to segregate this into 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.

        
    10.02.2017 / 12:02
    -1

    Hello,

    It makes sense for me to have the 2 technologies together. Content you would like to be indexed by Google needs to be rendered on the server, so MVC would help at that point. Areas of the system (website) that do not need indexing, it is interesting to use SPAs.

        
    30.07.2018 / 16:32