[导入]Processing A .aspx File From Console Window, Without Using IIS

Printable Version

Processing A .aspx File From Console Window, Without Using IIS
By Jamsheer E B

In this article you will learn how to execute an ASPX file from console. 

Requirements 

Platform : WIN NT 4.0 or Windows 2000 , .NET Framework Beta 2

Steps


1.Copy this code to a new file and save it as ConsoleHost.cs
// project created on 2/5/2002 at 4:43 PM
// compile csc ConsoleHost.cs /r:System.Web.dll
 System ;
 System.IO;
 System.Web;
 System.Web.Hosting;
  ConsoleHost : MarshalByRefObject // if you forget to extend this class
           // You'll get a SerializationException...!!! 
{
 public  HandleRequest( String fileName ) 
 {
  Console.WriteLine ( "The output from the {0} file" , fileName ) ;
  // Create a Worker to execute the aspx file
  HttpWorkerRequest worker = new SimpleWorkerRequest( fileName, "" , Console.Out ) ;
  // execute the page
  HttpRuntime.ProcessRequest( worker ) ;
 }
}
 class Host
{
 public   Main(String[] args) 
 {
  ConsoleHost myHost = ( ConsoleHost )ApplicationHost.CreateApplicationHost
      ( typeof( ConsoleHost ) , "/test" ,
                                        Directory.GetCurrentDirectory( ) );
   ( String fileName  args ) 
   {
    myHost.HandleRequest( fileName ) ;
   }
 }
}


2.Write a small ASPX file or copy this code and save it as test.aspx

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Console Host</title>
</head>
<script language="C#" runat="server">
void Page_Load ( Object src , EventArgs args )
{
 message.Text = "Hello World...!" ;
}
</script>
<body>
<asp:Label id="message" runat="server"/>
</body>
</html>


3.Compile ConsoleHost.cs by typing csc ConsoleHost.cs /r:System.Web.dll

4.Execute the test.aspx by typing ConsoleHost test.aspx, Now you can see the output from the test.aspx file as a static html in the Console window.

Nope....I got an error...!!!!
Unhandled Exception: System.IO.FileNotFoundException: File or assembly name ConsoleHost, or one of its dependencies, was not found.
File name: "ConsoleHost"

You have to create a bin directory as subdirectory to your current directory and move a copy of ConsoleHost.exe to it. Now you have two copies of ConsoleHost.exe, one in the current directory (in my case f:/temp/test/) and another in /bin (in my case f:/temp/test/bin) directory.

Now type again ConsoleHost test.aspx, you should see an output like this,
output.jpg

Now you are ready to create your own web server which can process ASP.NET files....!!!!


文章来源: http://computer.mblogger.cn/wucountry/posts/47781.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值