惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Forbes - Security
Forbes - Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
W
WeLiveSecurity
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
Martin Fowler
Martin Fowler
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
WordPress大学
WordPress大学
S
Security Affairs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
SegmentFault 最新的问题
P
Privacy International News Feed
IT之家
IT之家
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY
Hacker News: Ask HN
Hacker News: Ask HN
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
美团技术团队
Security Latest
Security Latest
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
爱范儿
爱范儿
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
I
InfoQ
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog

博客园 - Tobin

41岁的大龄程序员,苟着苟着,要为以后做打算了 UC_Center整合单点登录后远程注册不激活问题的解决办法 在as3中Embed(绑定)flash动画元素 - Tobin - 博客园 Embedding Resources with AS3 Configure the max limit for concurrent TCP connections [转]翻译:使用.net3.5的缓存池和SocketAsyncEventArgs类创建socket服务器 强制将IE,Chrome设置为指定兼容模式来解析(转) - Tobin - 博客园 工商银行,千万别用,转账不成功一样收手续费 MySQL vs NoSQL 效率与成本之争(转) 使用ASP.NET Global.asax 文件(转) [MySql识记]create utf8 database - Tobin npgsql连接postgresql数据库 由浅到深了解JavaScript类[转过来的收藏] javascript改变this指针 哪个美女最漂亮,自己写的js图片自适应切换 javascript操作cookie实例 [图解] 你不知道的 JavaScript - “this”(转) 对与list<>泛型的一些操作方法 关于游戏开发中的A*/A-star的寻路算法的问题
AspExe - a small ASP.NET compiler and executor for document generation
Tobin · 2010-12-08 · via 博客园 - Tobin

Introduction

AspExe 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:

Collapse | Copy Code

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.

Background

Before 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:

  • The generator must be a library or a small set of source code files to be consumed by a .NET Windows application.
  • The generator must be lightweight/small footprint.
  • The generator would preferably allow templates that contain scripting to be executed while generating.
  • It should be possible to insert domain specific information into the template while generating, allowing documents to be generated that would contain (formatted) domain information. E.g., see the sample image on the top of this page that contains 'domain information' about my hard drives.
  • It must be easy to track and solve problems in erroneous template files.
  • The library must be free.
  • Preferably, templates could be written in an existing language, allowing a steep learning curve.

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.

Approach

AspExe 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:

  1. Reads the ASP.NET file.
  2. Parses the file into sections. There are four types of sections: directives, declarations, code blocks and text (or HTML).
  3. Generate full c# source code out of these sections.
  4. Compile the source code into an in-memory assembly.
  5. On execution, assign the text code blocks as strings to the compiled page to be printed on execution to the Response object.
  6. Write the Response object to an output file.

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!!

Limitations

Note, 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: @Page, @Assembly, and @Import. All other directives are ignored. So, for instance, including Web controls inside the page will not work.

Using the code

Using the code is simple. Add the Template*.cs files to your project and add the AspExe namespace. Then, execute the following code example to load, parse, compile, and execute an .aspx template and save it to an output file:

Collapse | Copy Code



Template template = new Template(@"Default.aspx");



template.Compile();


template.Execute(@"output.html");

Optionally, it is possible to also pass parameters and runtime information to the template on execution.

Collapse | Copy Code



System.Security.Principal.WindowsIdentity user = 
   System.Security.Principal.WindowsIdentity.GetCurrent();


TemplateParameters tps = new TemplateParameters();




tps.Add(user.GetType(), "CurrentUser", user);


template.Execute(targetFile, tps);

In order for the template to use the user information, it is required that the CurrentUser is declared in the template. This can be done as follows:

Collapse | Copy Code


<SCRIPT runat="Server">
public System.Security.Principal.WindowsIdentity CurrentUser;
</SCRIPT>

Note that it is imperative that the runat="Server" attribute is included inside the <SCRIPT> tag. Otherwise, the code block will not be included by the parser and the compiler. Note that this is also required by ASP.NET. The script tag allows you to declare variables and also to define your own methods and functions.

Points of interest

Using 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:

Collapse | Copy Code

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 @Assembly and @Import directives. This allows the template to reference third party or your own libraries (it is required to be a .dll though). Code in the template will be able to access the functionality in the referenced assembly. E.g.: <%@ Assembly Name="MyLibrary" %>. Note that you must exclude the .dll extension.

The @Import directive allows you to declare a namespace to use inside the template. E.g., you can import your own assembly and then declare the namespaces used in the assembly. Example: <%@ Import Namespace="System.IO" %>

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.