2

I want to develop and SPA using angular.

Because I have experience in ASP.NET MVC, I have come across a few implementations that use Angular with ASP.NET MVC.

My understanding is that angular routing is run on the client pointing to the html file to open.

However, with MVC, the server routes the requests based on Controller/Action and that returns an cshtml file.

I have seen two implementations for angular routing configuration:

  1. TemplateUrl is the actual html file
  2. TemplateUrl has the form Controller/Action, and controller then returns the cshtml

How does the second work? Is it making a call to the server? How these two compare? Is

den
  • 709
  • 2
  • 7
  • 19
  • 1
    Check this http://stackoverflow.com/questions/23682203/how-to-use-asp-net-mvc-and-angularjs-routing – Nitin Varpe Jun 22 '15 at 12:30
  • 1
    To put it simply, there are two routings, controllers, views going on. Client and server. So ASP.NET is concentrating on all Server Side routing, controllers etc. AngularJs is concentrating on all Client Side routing. Both scenarios need to happen independently. However, because AngularJs has partials this does stem the need for cshtml razor generated views. – Callum Linington Jun 22 '15 at 12:32
  • Use ASP.NET MVC for initial rendering of the page, bundle-ing and so on if you have a legacy application. And then after the initial rendering do the routing client-side with angular. – Razvan Dumitru Jun 22 '15 at 12:40
  • 1
    @RazvanDumitru not strictly true about the routing part. Both client and server can do routing. The server in particular needs to routing so that you can request a certain resource on an API endpoint (this endpoint is where outsiders can access your API). The client can do routing if you want to load up different views when certain actions are perform. So if you want a list of employees, you would have 2 routes, server side for the resource and client side to show the page and display results – Callum Linington Jun 22 '15 at 12:50
  • Of course. It's a spa, i think that will be a full-ajax application after the initial rendering. – Razvan Dumitru Jun 22 '15 at 12:52
  • Just to confirm I understand what you are saying. When angular routes to an action, the server performs some logic and returns the view. On the other hand if angular routes to a view then the server responds with just the html. Then the question if I need razor views? (which i guess might be slightly slower. – den Jun 22 '15 at 13:24
  • 1
    No, your last bit of understanding ` On the other hand if angular routes to a view then the server responds with just the html` is not necessarily true. You are right that when AngularJS request these static resources that the server will dish them up, but you can embed these html views into JS and push them into the `$templateCache`. No you don't need any razor views. Get a really good understanding of gulp, [link](https://github.com/clinington/shopping) shows my experiments with gulp, the `dist` folder shows both stages of gulp building – Callum Linington Jun 22 '15 at 14:19
  • @CallumLinington Thanks for your comments. So for my case that I want to give fast UI UX (mobile app like), it would be better to send all views during initial request. Should i use ASP.NET MVC to route to 1st page and then use angular to route to different partial views + build the ASP.NET Web API on the same project. Or have 2 separate projects? Any empty; just angular + an another for ASP.NET Web API. I am leaning towards the 1st – den Jun 22 '15 at 20:23
  • 1
    All you need to use is ASP.NET Web Api and AngularJS. Remember that ASP.NET sits on a web server, so you can just create your index.html and that will always be loaded up first. If you're doing mobile dev I would assume loading all the partials first would be a bad idea, I would just let AngularJS load them from the server when needed. – Callum Linington Jun 23 '15 at 07:01
  • @CallumLinington Is it true that when you route to a view it is only fetched once? (meaning next time you visit the view, you dont go back to the web server). If that is true I imagine that is not possible with razor views. Am I right? thanks – den Jun 23 '15 at 13:44
  • If you're talking about angular views, then yeah they're cached. But you can invalidate the cache. What you got to think is that to get a razor view back, you need to hit a MVC Url. But in angular js they are two separate environments. It seems to me like you need to watch a few tutorials, read up on it more – Callum Linington Jun 23 '15 at 13:55

0 Answers0