3

I would like to convert a C# API I wrote, and that generates (static) html pages (mostly for rendering tables of data), into using angularjs. The goal is to better decouple the data and the html, allowing interactivity (for instance, sorting by a column) / re-use of the data, that go beyond what a static html page could ever offer.

Now the issue is that as soon as I use JS to generate part of my page, I cannot anymore send the html document as-is via email, because the JS will not execute from an email client. Yet, this is a useful feature of my API.

Is there a way around that?

I think I heard once a mention of a virtual browser (in node?), that could execute all the javascript (without GUI), and then dump the html into a file.

Otherwise, the only solution I could think of is to have C# generate the tables, hardcoding the values in the html (as is currently the case), and have angularjs still do all the json post-processing to allow the user to interact with the data. The annoying thing with that is that it will require duplicating some of the table construction logic in C# and JS, which is not great.

joelhoro
  • 765
  • 7
  • 19
  • You can do this by using dot liquid or straight by Razor. That a look at this, might help: http://dotliquidmarkup.org/try-online – brduca Jul 13 '15 at 16:12
  • I don't think dotliquid is as powerful as angular. I am after more than just templates, for instance json queries. – joelhoro Jul 14 '15 at 15:30
  • Dot liquid is template render engine while Angular is MVVC framework :-) – brduca Jul 15 '15 at 13:32

2 Answers2

1

If you want to send email, then implement a service in Angular that calls some server side function to send the email. The data should be passed as some sort of view model. In ASP.NET, this server side function is often exposed via Web API or a generic handler (.ashx).

If you want the user to be able to download a file directly from the page, have a look at How to trigger a file download when clicking an html button or javascript.

It should be noted that directly using HTML intended for a webpage is often not a good idea to include in an email. The HTML parser in email clients vary widely, and you should likely use only a small subset of HTML features to ensure compatibility. HTML intended for a browser also often contain extra stuff (such as navigation menus) that isn't appropriate for an email.

Community
  • 1
  • 1
mason
  • 28,517
  • 9
  • 66
  • 106
  • I am actually not using an ASP webserver to generate my pages. The main reason is that the pages are generated on demand in a desktop application that is way too complex to be migrated to a webserver (not least because it requires some computing power which you would not expect from a webserver). In my prototype that uses angularjs, I am generating static html pages + some js/json files, that are then accessible through some webserver for rendering. – joelhoro Jul 13 '15 at 16:02
  • @joelhoro Okay fine. But you still need a server to handle the mail. It can be hosted in ASP.NET or WCF or even just a custom console app. Or you can use something commercially available, such as [SendGrid](https://sendgrid.com/). The point is, if you want to send an email, you'll need a server side component. – mason Jul 13 '15 at 16:04
  • As to the ability to download a file, this is precisely why I want to switch to angular. But that does not solve my problem, i.e. if the user can already see the html, they can as well use the 'save' button in their browser to save the doc. If I send them this page through email then the link will not work. Of course, I could send a link to the webserver, but as it stands the server is not accessible from outside the intranet, so that means people would not be able to read the content of the html page from their mail client or smartphone, they need to remote into our system. – joelhoro Jul 13 '15 at 16:07
  • thx for the clarification, but at the moment we do have a mail server, that my API is relying on. The key issue is to generate the static html that would go in the email. – joelhoro Jul 13 '15 at 16:08
  • @joelhoro I explained that. You pass a view model to a server. The server is then responsible for generating the email and sending it to the mail server. – mason Jul 13 '15 at 16:09
  • right, but the issue is that I still have the code duplication issue I mentionned if the server is in C# - unless the server is in nodejs but that's a lot of extra setup just for my little problem. – joelhoro Jul 13 '15 at 16:12
  • @joelhoro Like I pointed out, your HTML for your browser pages/email probably needs to be different anyway, so you'll need similar but different code to generate it. So that's a non-issue. – mason Jul 13 '15 at 16:14
  • the email html would not be so different from the browser one, it would just be a subset (i.e. the data as-is, without any ways to interact with it). And anyway you are referring to the layout/contents, whilst I am referring to the API. If I now want to add a feature to my API that allows formatting certain numbers a given way, depending on what column they are in, for instance, I will have to code it both in C# (for my hardcoded html), and in js as well. After that, I may want to add a feature to have flotjs charts on my page, and would like these ones in my emails as well... – joelhoro Jul 13 '15 at 16:18
  • @joelhoro As described [here](http://stackoverflow.com/questions/3054315/is-javascript-supported-in-an-email-message) you can't run JavaScript in email clients. Using flotjs simply wouldn't work. You can certainly generate the HTML in the client and send it to the server rather than a view model. But your HTML will still probably end up needing to be different, meaning you'll need different code to generate it. – mason Jul 13 '15 at 16:23
1

Would using a Mailing API be something to consider? In that case you could just grab the contents and use angular to send it to e. g. Sendgrid.

Julia Will
  • 586
  • 2
  • 7
  • Thx but the issue is not the mailing API, which I already have. It's to get the html that is generated by javascript. – joelhoro Jul 13 '15 at 16:15
  • You could use Angular's element API (https://docs.angularjs.org/api/ng/function/angular.element) to retrieve the html content. That won't include any styles, though. – Julia Will Jul 13 '15 at 16:26
  • Yes, but how do use this API outside of a browser? – joelhoro Jul 14 '15 at 01:33
  • Oh, maybe I misunderstood the question. So you need to be able to generate it while not seeing the page (like a scheduled report)? In that case you should probably move the code generation to a separate Javascript class so you can use it in node on the server side and frontend as well. – Julia Will Jul 14 '15 at 08:16
  • Yeah that's right. Well I think node is the way to go, but it seems like this has wider ramifications than I hoped. That is, if I can install a node.js utility that does what I need, it's fine, but I don't want to start installing a node server and having to maintain it. – joelhoro Jul 14 '15 at 10:53