Angular 4 application receiving ViewComponent ASP.Core server responses - asp.net

I am trying to build a single-page application with an ASP.Core + Angular 4 stack. The server is supposed to provide view components, which will be rendered client-side and populated with additional data.
The best way to create components would be utilising ViewComponent mechanic on the server, which allows me to effectively create HTML layouts and populate my HTML components with data from DB. Alas, I have not yet found a way to transfer the data to my Angular 4 client app. The ViewComponents are not static resources, meaning I (most likely) can't transfer it as JSON-data, and even if I could, I'd have to teach my client how to unwrap it back into html, which would increase the load on the client.
I have confirmed that JQuery calls can be made to receive IViewComponentResult responses, but I am yet to find a way on how to achieve same results in an Angular component. Any help would be appreciated.

Usually with this stack you use .NET Core as the backend meaning you use it to write the Web API code which interacts with the database. The Angular side of things would have services which call the Web API via http methods and present/manipulate the data in component.html/.css/.ts files.
You can read through a guide such as this to get an idea of the structure and flow of the stack. Everything you mentioned in your 2nd paragraph can be achieved through this architecture without the worry of transferring ViewComponents.

Related

Consuming external API

I'm a PHP developer, learning Umbraco and ASP.NET. I consumed an external API before using Guzzle in Laravel, however everything I found so far on Umbraco is very different to what I'm used to.
Now I'd like to consume external API in Umbraco, and return JSON objects into views. For example, one JSON object is an event (a concert or a sport event) which contains values like image, date, description. These will be passed into a view. I should be able to do this once I wrap my head around how to consume the API.
How would I consume external API on Umbraco? I'm unsure where to start. Are there any good tutorials available out there?
On server side you can look at this library Restsharp that helps you to consume external http or rest api. You can easily install it via nuget.
For client side you can use JQuery to do Ajax call but need to take into considering CORS.

Making calls to a Stand-Alone Web API to populate ViewModels in ASP.NET MVC

Lets say I have a standalone ASP.NET Core Web API as its own project (since I don't want to merge it w/ my MVC app)
I now want to create an MVC web application (another project, but same solution) that will make calls to the API. I am aware that for ajax requests I will directly call the API through JavaScript which is straight forward.
However, I am a bit confused as to how the MVC controller would make calls to the same API to populate the ViewModel and send to the View when a user requests a page i.e. http://myurl/books/1
I am aware that if I was using something like Angular which is fully client-side I wouldn't be running into this issue.
That's a good question. There's three different approaches that I can think of:
The first is to call the same API from your MVC controller with a HTTPClient object. Might seem a bit weird calling "yourself" on the server side but it works. In my opinion this isn't a great solution, because you're losing type-checking and you'll also making an unnecessary call over the network, and you also have to parse and convert the response back from WebAPI to your .net objects.
The second approach is to move all of the logic that's inside the API into a new project and compile it all down to a DLL. Your Web Api can then call the code inside the library and your MVC project can do it too. A pretty good approach.
The third approach is to do what you've suggested - don't populate the Model at all on the server side inside your MVC project and just do it all from the client side. Yes, this will work great with Angular but you can also do this yourself using bog-standard JQuery or Knockout. This approach means much more client side javascript to write (use Typescript!) but you'll end up with a much nicer separation of concerns and cleaner solution.

Creating SPA, using templates and ajax

I've just started with ASP.NET 4.0 Web Forms and I want to try create a really simple SPA - contacts application, the purpose is to enable the client to add / remove contacts and watch them.
So I've thought about what I need to this application, but I don't know if it exists in ASP.NET. Actually I need two templates which I can switch between them whenever I want to and manipulate data using AJAX. Additional part - I use only jQuery - not MVC framework.
My questions are:
Are there templates in Web Forms? I heard about something inside 'Ajax Control Toolkit' but I don't want to use any external code (only jQuery as external code). As I said before, I want to include in the page two templates which I can switch between them and manipulate data when I get data from the ajax requests.
While I want to remove / add / get data, where do the ajax requests go to? should I create a web service which handle this requests? are there another options?
1: No, webforms is a server/client intensive process which invalidates the whole premise of SPA where server interaction is kept to a minimum and most data is sent down on the initial request.
2: Yes any webservices can be used for SPA in Ajax.

Design decision between backbone.js or Spring MVC

I have to develop an application which takes XML reports stored in File System parse it and put it into database and display the reports by querying the database in various MIME types(XML,JSON,RSS and HTML). So what I have done till now is parsed the XML reports in the file system ,setup schema for database in MongoDB,put data in database using Spring-data and also managed to have a web service which shows rough draft of reports in XML,JSON and RSS feed.Now I want to display the reports in HTML as well and my supervisor suggested me to use Backbone.js to dispaly it in HTML format by calling the web service.Kindly advice me to choose b/w Backbone.js or write another Spring MVC web service which generates reports in HTML.
Thanks in advance
Swaraj
What you are asking are two different things and choosing between them.
You have to know what your overall objective is with the webapp is and what framework is best for it.
Short answer, Spring MVC integrates with Dojo.js right out of the box, which might do what you need to do, but since your supervisor is suggesting otherwise your options are:
Use Spring MVC to construct and display the data in the HTML using what ever render of your choice.
Or
Build out your Spring MVC wepapp API so that you can use a JavaScript MVC framework, such as Backbone.js or one of the many others (see TODOMVC for samples), to interact with your spring controllers.
Option 1 might be easy enough, used in conjunction with JavaScript or jQuery and plugins
Option 2 would be good if your webapp is complex and will be mostly a SPA (single page app) that utilizes a lot of JavaScript and you want a way to organize your js code better. See JavaScript MVC diagram for an example of how it works.

Question about using remoteobject in modules

I have a number of modules in my flex application and in each module I use a remoteobject to retrieve dynamic data from the server related to each module. Recently I read a couple of comments in some blogs people saying that remoteobjects should not be used in modules, instead they should be in the main application. Is this true and why? And if it's true, what would I do with the resulthandler in the main app, since the code is very specific to each module, there's a lot of data manipulation and code setting values of components in the module, how would the handler in the main app access the components inside the module?
Thanks
It seems ridiculous to me to avoid using RemoteObject in a module. The whole purpose of a Module is to be a self contained portion of an application. And there is no reason why remote service calls can't be part of the self contained piece.
Do you have links to blog posts making these claims? I'm sure any such claims would be context specific (as are most best practices).

Resources