In a project I'm working on, our MVC4 website is using a combination of Razor and Angular.js. I'm pretty new to front-end web development, so I'm confused as to if this is a good idea or not. What are the advantages and disadvantages of mixing these, and what are some scenarios that using both of these together would make sense?
1 Answer
I can share our expirience with AngularJS + MVC4/Razor: We're using Razor only for localization - so only allowed Razor feature is "@somekeyword". As result on client side we got localized AngularJS templates. This allows to do maximum caching (even using CDN).
As for MVC4 we're using ASP.NET Web API for sending JSON data. We'd developer extension for sending HAL (Hypermedia) responses.
This gives maximum decoupling and possibility to change to another backend if needed.
3 Comments
superjos
I'm working with same technology stack. Do you know if it's possible to reference a template page in Angular routes (e.g.
/somewhere/partials/mypartial.html) and then have the page generated by Razor engine out of a /somewhereelse/partials/mypartial.cshtml? This way the partial could include translated resource strings. Thanks!Dan VanWinkle
@superjos You can't reference it like that, but you can create a Controller Action.
public PartialViewResult Partial(string name) { return PartialView(name); } Then call with this: /controller/Partial?name=mypartialsuperjos
thanks, that's more or less the path I ended up following. MVC routes like
/Controllers/Templates/{action}/{templateName} where {action} is used to separate functional areas and corresponds to a folder within /Views/Templates/. Inside each folders are the different templates. A custom ViewEngine is needed to map the action to the folder name as well.