|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAspExe is a small command line tool that will take an .aspx file and then compile and execute (=render) it. The output is saved to a specified output file. To use the tool, execute the following command on the command line: aspexe.exe driveinfo.aspx output.html
After executing driveinfo.aspx, the output.html file is generated. Opening this file in a browser should result in something as shown at the top of this article. In this particular example, the drive information is dynamically read and generated from the aspx page. Warning: this tool will work only with a subset of .aspx pages. For details, see the advantages and limitations section further on in this article. BackgroundBefore I started this project, I needed a means to generate a document from a template. Already, there are a huge number of code and document generators and tools out there, so why write another one? It seemed none of them fulfilled my needs:
So, I started to ponder, what kind of framework could fill this need? Creating XML or HTML pages as output using ASP.NET would seem ideal, if it weren't for the fact that these pages are required to be hosted on an Internet Information Server. This is hardly the kind of lightweight solution I wanted to embed inside my Windows application. So, I decided to create a lightweight template parser/compiler/executor that could read .aspx files and create an output text file (or any other text format) much like it is handled in IIS. Hence the AspExe project was born. ApproachAspExe is a small wrapper around a template class. The template class contains a parser, compiler, and executor of a template page. Basically, what the AspExe does is the following:
Advantages:Because internally C# code is generated, and since C# code can be embedded inside templates, you have access to the full .NET framework, even from within the templates. The best advantage of all, however I find, is that the template is an aspx file. This means, the template can be edited inside Visual Studio, with full support for IntelliSense! Writing templates has never been easier!! LimitationsNote, however, that AspExe does have some restrictions: You can only execute a limited set of .aspx files. AspExe, for instance, does not bother with code-behind files. All scripting must be contained within the aspx page. Also, only a few directives are supported: Using the codeUsing the code is simple. Add the Template*.cs files to your project and add the // three steps to compile and execute an ASP.NET aspx file:
// step 1: load the .aspx template file, e.g. the default page
Template template = new Template(@"Default.aspx");
// step 2: compile the template. Don't worry about
// compiler errors. If any, those will be exported to the output file
template.Compile();
// step 3: execute the compiled template. run time errors are exported to the output file.
template.Execute(@"output.html");
Optionally, it is possible to also pass parameters and runtime information to the template on execution. // define a 'domain' object to pass on to the template,
// e.g. the current user information.
System.Security.Principal.WindowsIdentity user =
System.Security.Principal.WindowsIdentity.GetCurrent();
// create a template parameter collection object
TemplateParameters tps = new TemplateParameters();
// Add a new parameter to the collection.
// Note that the "CurrentUser"
// must be declared publicly inside the template.
tps.Add(user.GetType(), "CurrentUser", user);
// execute the template passing on the collection with parameters.
template.Execute(targetFile, tps);
In order for the template to use the user information, it is required that the //inside the template declare a SCRIPT section:
<SCRIPT runat="Server">
public System.Security.Principal.WindowsIdentity CurrentUser;
</SCRIPT>
Note that it is imperative that the Points of interestUsing this concept can potentially be very powerful. Specially because it is not required to output HTML, but XML or even CSV. Creating the right output XML could result in files that can be read as Excel or Visio. As an example, I included the "driveinfo2excel.aspx" file which exports drive information to an Excel file. Run the following command: aspexe.exe driveinfo2excel.aspx output.xls
The output.xls can be opened directly in MS Excel. Another point of interest is the support for the The Hopefully, this project is useful for developers out there. I am interested in hints or suggestions for improvement, but I would also like to know the alternatives. If you have any, let me know why it would be an improvement or a better alternative. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||