asp.net应用程序html化的一个想法
《asp.net应用程序html化的一个想法》比较完成示例代码
MyHtmlModule.cs
------------------
http://www.cnblogs.com/kevin-y/archive/2006/03/20/354226.html
MyHtmlModule.cs
public class MyHtmlModule:IHttpModule
{
public MyHtmlModule()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpModule 成员
public void Init(HttpApplication context)
{
// TODO: 添加 HtmlModule.Init 实现
context.BeginRequest+=new EventHandler(context_BeginRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
if(context.Request.Path == "/HttpModuleTest/WebForm1.aspx")//跳过不执行转换的页面
return;
string strUrl = context.Request.RawUrl;//读取请求的Url
string str = MyHtmlModule.GetHtmlPage(strUrl);//获取对应的html文件名
string strFile = context.Server.MapPath(str);//获取html的绝对路径
if(System.IO.File.Exists(strFile))//检查html文件是否存在
{
context.RewritePath(str);//跳转到html
}
else
{
if(BuildHtmlPage(strUrl,strFile))//尝试生成html文件
{
context.RewritePath(str);//生成成功跳转到html
}
else
{
context.RewritePath("msg.aspx");
context.Response.Write("你访问的页面" + strUrl + "不存在");
}
}
}
public void Dispose()
{
// TODO: 添加 HtmlModule.Dispose 实现
}
#endregion
private void context_EndRequest(object sender, EventArgs e)
{
}
public static string GetHtmlPage(string strAspxPage)
{
string pattern =@"([\w/]*)(.aspx)([?]*)([\w=&]*)";
Regex reg = new Regex(pattern,RegexOptions.IgnoreCase);
string str = reg.Replace(strAspxPage,"$1/_$4.htm");
str = str.Replace("=","_");
str = str.Replace("&","_");
return str;
}
public static bool BuildHtmlPage2(string strAspxPage,string strHtmlFile)
{
return false;
}
public static bool BuildHtmlPage(string strAspxPage,string strHtmlFile)
{
bool bRet = false;
StreamWriter writer = null;
try
{
writer = new StreamWriter(strHtmlFile,false,System.Text.Encoding.GetEncoding("GB2312"));
HttpContext.Current.Server.Execute(strAspxPage,writer);
bRet = true;
}
catch(Exception ee)
{
string str = ee.Message;
}
finally
{
if(writer!=null)
writer.Close();
}
return bRet;
}
}
{
public MyHtmlModule()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpModule 成员
public void Init(HttpApplication context)
{
// TODO: 添加 HtmlModule.Init 实现
context.BeginRequest+=new EventHandler(context_BeginRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
if(context.Request.Path == "/HttpModuleTest/WebForm1.aspx")//跳过不执行转换的页面
return;
string strUrl = context.Request.RawUrl;//读取请求的Url
string str = MyHtmlModule.GetHtmlPage(strUrl);//获取对应的html文件名
string strFile = context.Server.MapPath(str);//获取html的绝对路径
if(System.IO.File.Exists(strFile))//检查html文件是否存在
{
context.RewritePath(str);//跳转到html
}
else
{
if(BuildHtmlPage(strUrl,strFile))//尝试生成html文件
{
context.RewritePath(str);//生成成功跳转到html
}
else
{
context.RewritePath("msg.aspx");
context.Response.Write("你访问的页面" + strUrl + "不存在");
}
}
}
public void Dispose()
{
// TODO: 添加 HtmlModule.Dispose 实现
}
#endregion
private void context_EndRequest(object sender, EventArgs e)
{
}
public static string GetHtmlPage(string strAspxPage)
{
string pattern =@"([\w/]*)(.aspx)([?]*)([\w=&]*)";
Regex reg = new Regex(pattern,RegexOptions.IgnoreCase);
string str = reg.Replace(strAspxPage,"$1/_$4.htm");
str = str.Replace("=","_");
str = str.Replace("&","_");
return str;
}
public static bool BuildHtmlPage2(string strAspxPage,string strHtmlFile)
{
return false;
}
public static bool BuildHtmlPage(string strAspxPage,string strHtmlFile)
{
bool bRet = false;
StreamWriter writer = null;
try
{
writer = new StreamWriter(strHtmlFile,false,System.Text.Encoding.GetEncoding("GB2312"));
HttpContext.Current.Server.Execute(strAspxPage,writer);
bRet = true;
}
catch(Exception ee)
{
string str = ee.Message;
}
finally
{
if(writer!=null)
writer.Close();
}
return bRet;
}
}
------------------
http://www.cnblogs.com/kevin-y/archive/2006/03/20/354226.html