Execute an asp function within an HTML page using #exec

While looking for more information on how Server Side Includes are implemented on IIS servers, I ran across the #exec directive.

<!-- #exec CommandType=CommandDescription-->

#exec allows an HTML page to run an application (ASP application, CGI script, ISAPI application, etc.) or shell command (this is a security risk, use with caution). The output is then displayed on the HTML page.

The standard filename extension which will process this directive is “.stm” but if you configure your server to process .htm or .html for server side includes, it will run the #exec directive also.

This would be useful only in a situation where you really really can’t use an asp page for some reason but need to run some server-side code. And can’t use AJAX.. Well, maybe this is pretty useless but it’s sort of cool.

Picture 14.jpg

Example

The set of pages below will execute a simple ASP command and print it to the HTML page.

HTML page “index.htm”


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Execute asp code and print result to browser</title>
</head>

<body> <H3>Inside index.htm</H3> <!-- #exec CGI="/test/ssi/test.asp?test=this_is_the_request_string" --> </body> </html>

HTML page “test.asp”

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>This is the ASP page</title>
</head>

<body>

<% Response.Write "<BR>Inside test.asp.<BR>" Response.Write "Test = " & Request.QueryString("Test") & ".<BR>" %>

</body> </html>

for more information

  • MSDN page on #exec directive