荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Mic (酷鱼), 信区: DotNET
标  题: 抛开iis,在console下运行.aspx
发信站: 荔园晨风BBS站 (Tue Sep 24 12:53:01 2002), 站内信件

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
using System ;
using System.IO;
using System.Web;
using System.Web.Hosting;
public class ConsoleHost : MarshalByRefObject // if you forget to extend
 this class
           // You'll get a SerializationException...!!!
{
 public void 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 ) ;
 }
}
public class Host
{
 public static void Main(String[] args)
 {
  ConsoleHost myHost = ( ConsoleHost )ApplicationHost.
CreateApplicationHost
      ( typeof( ConsoleHost ) , "/test" ,
                                        Directory.GetCurrentDirectory( )
 );
  foreach ( String fileName in 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.



--
你“唰”的一声抽出一个饭盒握在手中.
>kill da chu
你对大厨喝道:“臭贼!今天不是你死就是我活!放聪明点给我四两!”
看起来大厨似乎想杀死你!
你往南落荒而逃了。
仓皇之间,你丢下一张金龙卡。

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.52.230]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店