Custom server script - c#

I have built a small HTTP server based on the HTTPListener class.
What I would like to know is, is there any scripting language I can put in my HTML files that can be natively executed in C#?
Something similar to how PHP works when it is mixed with HTML code.
lee

You could use any of the available templating engines for .NET such as WebForms, Razor, ...

There is one you can use: T4.
If you want to have templated defined at runtime, you will need a reference to Microsoft.VisualStudio.TextTemplating to be present, you can learn how to use it pragmatically from the article Processing Text Templates by using a Custom Host (at msdn).
From the article:
To execute a text template, you call the ProcessTemplate method of
Microsoft.VisualStudio.TextTemplating.Engine:
using Microsoft.VisualStudio.TextTemplating;
...
Engine engine = new Engine();
string output = engine.ProcessTemplate(templateString, host);
In the host parameter, you must provide a class that implements
ITextTemplatingEngineHost. This is called back by the Engine.
By this method you can have make your program read the template from a file or load it from a resource.
The article Walkthrough: Creating a Custom Text Template Host (also at msdn) will help you to implement the interface ITextTemplatingEngineHost.
As an alternative, if you will have fixed templates defined beforehand, you can create the tt files and use the following method:
TestTemplate testTemplate = new TestTemplate();
string output = testTemplate.TransformText();
Where TestTemplate is a class generated by VisualStudio from a TestTemplate.tt file. Using this method the template is fixed at compile time. So you will not be able to define it at runtime, from example by reading it from a file.

If you want something simple I recommend you to take a look at T4 templates. You can start from here and here

Related

Updating an Evernote Note from HTML

Using the C# SDK I'm working on a simple application to load a note from evernote, allow the user to edit the note using a HTML wysiwyg editor and then save it back to Evernote.
I'm a little confused. I can load a ENNote from the standard ENSession and access the HTML version of the note, but to update I have to load a Edam.Type.Note which has no HTML properties to set just the standard XML doc.
I can see there is a ENHTMLtoENMLConverter class but its internal? Its counterpart ENMLtoHTMLConverter is public.
Is there an easy to convert basic HTML/ENHTML into a Edam.Type.Note so I can update without writing my own converter or compiling my own version of the SDK?
Creating a note using HTML or web content
The SDK contains a facility for supplying HTML or web content to a
note.
ENNote myFancyNote = new ENNote();
myFancyNote.Title = "My first note";
myFancyNote.Content = ENNoteContent.NoteContentWithSanitizedHTML("<p>Hello, world - <i>this</i> is a <b>fancy</b> note.</p>");
ENNoteRef myFancyNoteRef = ENSession.SharedSession.UploadNote(myFancyNote, null);
This method handles general HTML content, including external style
sheets which it will automatically inline. Please note that this is
not a comprehensive "web clipper", though, and isn't designed to work
fully on all arbitrary pages from the internet. It will work best on
pages which have been generally designed for the purpose of being
captured as note content.
Source : Getting Started with the Evernote Cloud SDK for Windows
You can do this without dipping into the "advanced" stuff. You still want to call ENSession.SharedSession.UploadNote but use the version that lets you specify one of the "replace" policies as appropriate:
public ENNoteRef UploadNote(ENNote note, ENSession.UploadPolicy policy, ENNotebook notebook, ENNoteRef noteToReplace)
You'll still need to create an ENNote with your updated content (you can use the NoteContentWithSanitizedHTML etc). And supply the note ref pointing to the original note you want to replace.

Specify language in Octokit.net for Gist

I am attempting to create a new NewGist using Octokit.net however I can't seem to find a way to specify a language (Text, C#, C++, Java, etc) so whenever it is created it simply creates it as Text.
Am I missing something or is there no functionality with this API to specify it? Thanks!
Based on a test that I just carried out, it looks as though the language for a file contained with a gist is being inferred from the extension of the filename.
For example, I just created a gist using octokit which contained a filename of install.ps1. On checking the created gist, the language dropdown was already pre-selected with PowerShell.

How to generate solutions with a template?

I'm having a bit of trouble here.
I want to be able to generate a C# project (and solution) (let's say a C# Console Application) using a template similar to the ones VS2010 uses.
Basically I want to have a method GenerateConsoleApplication which does just that. But since I will need to generate several of these, I want to use a template to populate the Main method of the program.cs class which will be generated alongside the .csproj and app.xml files.
public void GenerateConsoleApplication()
{
var projectName = "MyConsoleApplication";
var projectLocation = "C:\temp";
// what goes here so that it creates a solution Visual Studio that I could use ?
}
I looked up the project template used by VS2010 to generate a C# Console Application and am thinking I could modify it to suit my needs. But I have no idea what I would need to write (code wise) to use the said template and generate all the files of the new solution.
Does anyone know how to do it ? Or if it is even possible ?
I know I could just write the csproj file and the others but I think a template would allow changes more easily.
Just found what I'll need to do.
The problem boils down to the parsing of the templates files.
Once I get the parser, then I can replace the tags with the appropriate values. I wanted to use Visual Studio's parser to do that, and slightly modify the templates it already uses, but it's not possible.
So one has to write its own parser and use it to generate the files.
Then Regex can be use to replace the tags of the template file with the Regex.Replace(String, String, MatchEvaluator) method.

Can I use T4 template features to generate C# classes from javascript class definitions?

I have a javascript based library and would like to generate some C# classes for it. Unfortunately, I do not know T4 at all, so I may not have included information you need to answer this question. please comment, and I will edit as needed.
Thanks!
Three Routes to try in order of niceness
a) Use CodeModel in Visual Studio 2008 or later to parse the javascript source code
b) Jscript.NET compiler to parse your code / or Eval() objects from it then walk over that code
c) Use RegEx to parse on your own
Please Drop me a line at tfischer AT tangible . DE. I am doing this for C# anyways right now an can add js for sure.
Well, with T4 templates you can generate pretty much anything.
But you'll need to parse those JavaScript files somehow by yourself within T4.
Yes, you can write a T4 template that creates C# classes from JavaScript class definitions. But there is nothing in T4 that helps you with doing so. A T4 template is essentially just a C# script that is executed by Visual Studio whenever the template file is modified.

Is there a way to use .aspx pages as templates for emails being sent from a winform application?

ASP.NET is great for creating html. We send out a lot of html email messages. In the past we've been loading .html templates with key strings that are replaced to make the email more custom (ie. [[FirstName]] would be replaced with "John Doe"). However as we are adding more and more email campaigns the logic to customize the email is starting to get out of hand. Instead of a simple [[FirstName]] string replace we are tasked with something like [[ScholarshipList]] where the ScholarshipList is different for each user we send an email to.
Ideally, at least in my mind, these email templates would be self contained .aspx pages. And would be invoked from our winform emailer application in a manner similar to
IEmailTemplate template = EmailTemplates.Load("ScholarshipList.aspx");
template.UserID=1234;
string emailMessage = template.Render();
Then the ScholarshipList.aspx page would look similar to any other asp.net page in that it can have server controls, runat="server" scripts and databind.
I've seen this article http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp which looks promising. Has anyone used it before? It uses some remoting which ideally would be avoided, and has a limited error handling mechanisms. I thought I'd get some other feedback before I went to far down that road.
You could try this approach as well. http://jamesewelch.com/2008/07/11/how-to-render-a-aspnet-user-control-within-a-web-service-and-return-the-generated-html/
If you hosted your templates in a webservice and then invoked them from your app.
A quick and dirty way to do it is to do a HttpWebRequest to the url and grab the output and use that, but thats not as nice as the anser from David...
You could also look at Smart Code, an opensource project for code Generation based on Asp.net templating.
http://www.codeplex.com/smartcodegenerator
It is integrated with Nant and Cassini so you can run it from the command line or from your WinForm app.
However for email, it is more common to use a simple String.Replace like you've mentioned above but if you need to do loops and the like then templates might be better.
To solve the [[ScholarshipList]] that you are mentioning without asp.net templates you can also use a text file like:
Hello [[MemberSimpleReplace][FIRSTNAME]],
Here is your ScholarShipList:
[[ScholarShip]]
And then use the factory method pattern to instantiate the correct factory (this is pseudo code, so bear with my mistakes)
IEmailContentGenerator gen = EmailContentGenFactory.getGenerator("MemberSimpleReplace");
var params = {"FIRSTNAME"};
var stringToSubstitute = gen.Generate(params,context);
var gen2 = EmailContentGenFactory.getGenerator("ScholarShip");
var params2 = {};
var stringToSubstitute2 = gen2.Generate(params2,context);
Then when you can easily implement new Generators, the generators get an array of parameters and the context that would have the UserId and maybe some other values.
Also remember that Asp.net is not the only template tool in .net to generate HTML, you can look at NVelocity for example or the many View Engines for Asp.net MVC.

Categories