应用ASP.NET Runtime
// MainForm.cs
// project created on 5/23/2003 at 1:28 PM
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using AxSHDocVw;
using System.Web.Hosting;
using System.Diagnostics;
namespace UsingAspRuntime
{
class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdOpen;
private System.Windows.Forms.TextBox txtFile;
private System.Windows.Forms.Label label;
private AxSHDocVw.AxWebBrowser m_Browser;
private static String m_outFile = "__page__.htm";
private String m_aspx = null;
MyHost m_host = null;
public MainForm(String webPage)
{
Trace.Write("MainForm.MainForm : webPage " + webPage);
InitializeComponent();
m_aspx = webPage;
}
void cmdOpenClick(object sender, System.EventArgs e)
{
if (txtFile.Text != null)
{
m_aspx = txtFile.Text;
ThreadPool.QueueUserWorkItem(new WaitCallback(InitBrowserComponent), m_aspx);
}
}
void InitializeComponent() {
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.m_Browser = new AxSHDocVw.AxWebBrowser();
this.label = new System.Windows.Forms.Label();
this.txtFile = new System.Windows.Forms.TextBox();
this.cmdOpen = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.m_Browser)).BeginInit();
this.SuspendLayout();
//
// m_Browser
//
this.m_Browser.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.m_Browser.Enabled = true;
this.m_Browser.Location = new System.Drawing.Point(0, 72);
//this.m_Browser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("m_Browser.OcxState")));
this.m_Browser.Size = new System.Drawing.Size(360, 232);
this.m_Browser.TabIndex = 0;
this.m_Browser.BeforeNavigate2 +=
new AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(OnBeforeNavigate2);
//
// label
//
this.label.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label.Location = new System.Drawing.Point(8, 24);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(64, 16);
this.label.TabIndex = 1;
this.label.Text = "File Name";
//
// txtFile
//
this.txtFile.Location = new System.Drawing.Point(80, 24);
this.txtFile.Name = "txtFile";
this.txtFile.Size = new System.Drawing.Size(152, 20);
this.txtFile.TabIndex = 2;
this.txtFile.Text = "";
//
// cmdOpen
//
this.cmdOpen.Location = new System.Drawing.Point(264, 16);
this.cmdOpen.Name = "cmdOpen";
this.cmdOpen.Size = new System.Drawing.Size(72, 32);
this.cmdOpen.TabIndex = 3;
this.cmdOpen.Text = "Open";
this.cmdOpen.Click += new System.EventHandler(this.cmdOpenClick);
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(360, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdOpen,
this.txtFile,
this.label,
this.m_Browser});
this.Text = "Using ASP.NET Runtime";
this.Load += new System.EventHandler(this.MainForm_Load);
((System.ComponentModel.ISupportInitialize)(this.m_Browser)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
public static void Main(string[] args)
{
if (args.Length > 0)
{
Application.Run(new MainForm(args[0]));
}
else
{
Application.Run(new MainForm(null));
}
}
private void MainForm_Load(Object Sender, System.EventArgs e)
{
Trace.Write("MainForm.MainForm_Load : m_axpx " + m_aspx);
if (m_aspx != null)
{
InitBrowserComponent(m_aspx);
}
}
public void InitBrowserComponent(Object sender)
{
Object obj = null;
string webPage = null;
string strFileExt = null;
string[] strExtList;
webPage = (string)sender;
if (webPage == null)
{
return;
}
Trace.Write("MainForm.InitBrowserComponent : webPage " + webPage);
if (m_host == null)
{
m_host = (MyHost)ApplicationHost.CreateApplicationHost(
typeof(MyHost),
"/foo",
Environment.CurrentDirectory);
}
strExtList = webPage.Split('.');
if (strExtList != null)
{
strFileExt = strExtList[strExtList.Length - 1];
}
// If it is an ASPX file, process it to generate the HTML and then load the
// generated HTML file
if (strFileExt != null && strFileExt.ToUpper().Equals("ASPX"))
{
Trace.Write("Will process the ASPX file and load the same");
m_host.CreateHtmlPage(webPage, null, m_outFile);
}
// Load the unprocessed file
else
{
Trace.Write("Will load the requested file: No processing is required");
File.Copy(webPage, m_outFile, true);
}
m_Browser.Navigate("file://" + Environment.CurrentDirectory + "/" + m_outFile,
ref obj, ref obj, ref obj, ref obj);
// update the test box with the file name currently open in the
// browser component
txtFile.Text = webPage;
}
void OnBeforeNavigate2(object sender, DWebBrowserEvents2_BeforeNavigate2Event e)
{
Trace.WriteLine("Before Navigate2 : " + e.uRL.ToString());
string url = e.uRL.ToString();
string curDir = Environment.CurrentDirectory;
// If its a file located in current directory, run the asp.net env on this file
if (url.ToUpper().StartsWith(curDir.ToUpper()))
{
string file = null;
int urlLen = url.Length;
int dirLen = Environment.CurrentDirectory.Length;
file = url.Substring(dirLen + 1, urlLen - dirLen - 1);
Trace.WriteLine("file is : " + file);
// If it is an ASPX file, process it to generate the HTML and then load the
// generated HTML file
if (!file.Equals(m_outFile))
{
e.cancel = true;
m_aspx = file;
ThreadPool.QueueUserWorkItem(new WaitCallback(InitBrowserComponent),
m_aspx);
}
}
}
}
}
// project created on 5/23/2003 at 1:28 PM
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using AxSHDocVw;
using System.Web.Hosting;
using System.Diagnostics;
namespace UsingAspRuntime
{
class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdOpen;
private System.Windows.Forms.TextBox txtFile;
private System.Windows.Forms.Label label;
private AxSHDocVw.AxWebBrowser m_Browser;
private static String m_outFile = "__page__.htm";
private String m_aspx = null;
MyHost m_host = null;
public MainForm(String webPage)
{
Trace.Write("MainForm.MainForm : webPage " + webPage);
InitializeComponent();
m_aspx = webPage;
}
void cmdOpenClick(object sender, System.EventArgs e)
{
if (txtFile.Text != null)
{
m_aspx = txtFile.Text;
ThreadPool.QueueUserWorkItem(new WaitCallback(InitBrowserComponent), m_aspx);
}
}
void InitializeComponent() {
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.m_Browser = new AxSHDocVw.AxWebBrowser();
this.label = new System.Windows.Forms.Label();
this.txtFile = new System.Windows.Forms.TextBox();
this.cmdOpen = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.m_Browser)).BeginInit();
this.SuspendLayout();
//
// m_Browser
//
this.m_Browser.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.m_Browser.Enabled = true;
this.m_Browser.Location = new System.Drawing.Point(0, 72);
//this.m_Browser.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("m_Browser.OcxState")));
this.m_Browser.Size = new System.Drawing.Size(360, 232);
this.m_Browser.TabIndex = 0;
this.m_Browser.BeforeNavigate2 +=
new AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(OnBeforeNavigate2);
//
// label
//
this.label.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label.Location = new System.Drawing.Point(8, 24);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(64, 16);
this.label.TabIndex = 1;
this.label.Text = "File Name";
//
// txtFile
//
this.txtFile.Location = new System.Drawing.Point(80, 24);
this.txtFile.Name = "txtFile";
this.txtFile.Size = new System.Drawing.Size(152, 20);
this.txtFile.TabIndex = 2;
this.txtFile.Text = "";
//
// cmdOpen
//
this.cmdOpen.Location = new System.Drawing.Point(264, 16);
this.cmdOpen.Name = "cmdOpen";
this.cmdOpen.Size = new System.Drawing.Size(72, 32);
this.cmdOpen.TabIndex = 3;
this.cmdOpen.Text = "Open";
this.cmdOpen.Click += new System.EventHandler(this.cmdOpenClick);
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(360, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdOpen,
this.txtFile,
this.label,
this.m_Browser});
this.Text = "Using ASP.NET Runtime";
this.Load += new System.EventHandler(this.MainForm_Load);
((System.ComponentModel.ISupportInitialize)(this.m_Browser)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
public static void Main(string[] args)
{
if (args.Length > 0)
{
Application.Run(new MainForm(args[0]));
}
else
{
Application.Run(new MainForm(null));
}
}
private void MainForm_Load(Object Sender, System.EventArgs e)
{
Trace.Write("MainForm.MainForm_Load : m_axpx " + m_aspx);
if (m_aspx != null)
{
InitBrowserComponent(m_aspx);
}
}
public void InitBrowserComponent(Object sender)
{
Object obj = null;
string webPage = null;
string strFileExt = null;
string[] strExtList;
webPage = (string)sender;
if (webPage == null)
{
return;
}
Trace.Write("MainForm.InitBrowserComponent : webPage " + webPage);
if (m_host == null)
{
m_host = (MyHost)ApplicationHost.CreateApplicationHost(
typeof(MyHost),
"/foo",
Environment.CurrentDirectory);
}
strExtList = webPage.Split('.');
if (strExtList != null)
{
strFileExt = strExtList[strExtList.Length - 1];
}
// If it is an ASPX file, process it to generate the HTML and then load the
// generated HTML file
if (strFileExt != null && strFileExt.ToUpper().Equals("ASPX"))
{
Trace.Write("Will process the ASPX file and load the same");
m_host.CreateHtmlPage(webPage, null, m_outFile);
}
// Load the unprocessed file
else
{
Trace.Write("Will load the requested file: No processing is required");
File.Copy(webPage, m_outFile, true);
}
m_Browser.Navigate("file://" + Environment.CurrentDirectory + "/" + m_outFile,
ref obj, ref obj, ref obj, ref obj);
// update the test box with the file name currently open in the
// browser component
txtFile.Text = webPage;
}
void OnBeforeNavigate2(object sender, DWebBrowserEvents2_BeforeNavigate2Event e)
{
Trace.WriteLine("Before Navigate2 : " + e.uRL.ToString());
string url = e.uRL.ToString();
string curDir = Environment.CurrentDirectory;
// If its a file located in current directory, run the asp.net env on this file
if (url.ToUpper().StartsWith(curDir.ToUpper()))
{
string file = null;
int urlLen = url.Length;
int dirLen = Environment.CurrentDirectory.Length;
file = url.Substring(dirLen + 1, urlLen - dirLen - 1);
Trace.WriteLine("file is : " + file);
// If it is an ASPX file, process it to generate the HTML and then load the
// generated HTML file
if (!file.Equals(m_outFile))
{
e.cancel = true;
m_aspx = file;
ThreadPool.QueueUserWorkItem(new WaitCallback(InitBrowserComponent),
m_aspx);
}
}
}
}
}
// MyHost.cs
using System;
using System.IO;
using System.Web;
using System.Web.Hosting;
using System.Diagnostics;
namespace UsingAspRuntime
{
public class MyHost : MarshalByRefObject
{
public void CreateHtmlPage(String webPage,
String query,
String file)
{
Trace.Write("[MyHost.CreateHtmlPage : webPage " + webPage +
" query " + query + " file " + file);
StreamWriter stream = new StreamWriter(file);
SimpleWorkerRequest swr = new SimpleWorkerRequest(webPage,
query,
stream);
HttpRuntime.ProcessRequest(swr);
stream.Close();
Trace.Write("MyHost.CreateHtmlPag]");
}
}
}
using System;
using System.IO;
using System.Web;
using System.Web.Hosting;
using System.Diagnostics;
namespace UsingAspRuntime
{
public class MyHost : MarshalByRefObject
{
public void CreateHtmlPage(String webPage,
String query,
String file)
{
Trace.Write("[MyHost.CreateHtmlPage : webPage " + webPage +
" query " + query + " file " + file);
StreamWriter stream = new StreamWriter(file);
SimpleWorkerRequest swr = new SimpleWorkerRequest(webPage,
query,
stream);
HttpRuntime.ProcessRequest(swr);
stream.Close();
Trace.Write("MyHost.CreateHtmlPag]");
}
}
}