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

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 theoutput from the test.aspx file as a static html in the Console window.

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

You have to create a bin directory assubdirectory 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 mycase 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,

 

July 10th, 2006 in ASP.NET by admin


 

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 ></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 theoutput from the test.aspx file as a static html in the Console window.

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

You have to create a bin directory assubdirectory 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 mycase 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,
脱离IIS,在控制台执行aspx文件。

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

源代码下载

相关文章:

  • 2022-12-23
  • 2021-07-08
  • 2021-07-29
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2021-11-23
  • 2021-07-28
  • 2021-11-26
  • 2021-10-02
相关资源
相似解决方案