asp.net C#整站静态化应怎么做,在网站找到的有一些教程,但都一知半解。配置文件如下
<?xml version="1.0"?>
<!-- 
    注意: 除了手动编辑此文件以外,您还可以使用 
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在 
    machine.config.comments 中,该文件通常位于 
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
  <configSections>
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
  </configSections>
  <RewriterConfig>
    <Rules>
      <!-- 规则1 -->
      <RewriterRule>
        <LookFor>~/d(\d+)\.html</LookFor>
        <SendTo>~/default.aspx?id=$1</SendTo>
      </RewriterRule>
      <!-- 规则2 -->
      <RewriterRule>
        <LookFor>~/k(\d+)\.html</LookFor>
        <SendTo>~/default.aspx?key=$1</SendTo>
      </RewriterRule>
    </Rules>
  </RewriterConfig>  <connectionStrings>
    <add name="webtest" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\webtest.mdb" providerName="System.Data.OleDb"/>    <!--
    用户数据库
<add name="webtest" connectionString="Data Source=61.152.93.48;Initial Catalog=test081125;Persist Security Info=True;User ID=test081125;Password=test081125" providerName="System.Data.SqlClient"/>
-->
    
</connectionStrings>
<appSettings/>
<system.web>
         <!--AjaxPro_Start-->
         <httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="*" path="*.asmx" type="Microsoft.Web.Services.ScriptHandlerFactory" validate="false"/>
            <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
     
    </httpHandlers>
    <httpModules>
      <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
    </httpModules>
        <!--AjaxPro_End-->
<!-- 
            设置 compilation debug="true" 可将调试符号插入
            已编译的页面中。但由于这会 
            影响性能,因此只在开发过程中将此值 
            设置为 true。
        -->
    <identity impersonate="false"/>
<compilation debug="true">
<assemblies>
        
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        
       </assemblies></compilation>

        <!--
            通过 <authentication> 节可以配置 ASP.NET 用来 
            识别进入用户的
            安全身份验证模式。 
        -->
<authentication mode="Windows"/>
<!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。
 -->
    <customErrors mode="Off" defaultRedirect="mycustompage.htm">
      <error statusCode="403" redirect="NoAccess.htm" />
      <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>  </system.web>
<system.codedom>
</system.codedom>
<!-- 
        在 Internet 信息服务 7.0 下运行 ASP.NET AJAX 需要 system.webServer
        节。对早期版本的 IIS 来说则不需要此节。
    -->
<system.webServer>
</system.webServer>
</configuration>用的是URLRewriter.dll,但具体的不知道怎么弄
希望高手,能给个具体的代码示例或详细说明一下。谢谢。只要能实现全整静态化就行。

解决方案 »

  1.   

    seehttp://www.cnblogs.com/huobazi/archive/2007/12/31/UrlRewriteAndHttpHanderAndMakeStaticHtmlFiles.html
      

  2.   


     我用过的using System;
    using System.IO;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using System.IO;/// <summary>
            /// 根据虚拟路径获取物理路径
            /// </summary>0
            /// <returns></returns>
            private void ChangeAspxToHtml()
            {
                // try
                // {
                Boolean boolUrl = false;
                String strRequestUrl = System.Web.HttpContext.Current.Request.RawUrl;//获取请求的原始Url
                String strRedirtUrl = System.Web.HttpContext.Current.Request.Url.ToString();//获取当前请求的Url
                String strPhysicsDirectoryFiles = System.Web.HttpContext.Current.Request.PhysicalPath;//获取当前请求的Url的文件的物理路径
                String strOldRequestFileName = "";
                String strNewRequestFileName = "";
                String strHtmlFileName = "";
                String strRRedirtUrl = "";
                if (CheckUrl(strRequestUrl) == false)
                {
                    Response.Redirect(strRequestUrl + "/default.aspx");
                }
                int intFilePostion = strRequestUrl.LastIndexOf('/');
                strOldRequestFileName = strRequestUrl.Substring(intFilePostion + 1, strRequestUrl.Length - intFilePostion - 1);
                String strPhysicsDirectory = System.IO.Path.GetDirectoryName(Page.Request.PhysicalPath);
                strNewRequestFileName = strOldRequestFileName.Replace(".aspx", "");
                strHtmlFileName = strNewRequestFileName.Replace('?', '_').Replace('&', '_').Replace('=', '_') + ".html";//要转向的html文件            int intRedirtUrl = strRedirtUrl.LastIndexOf('/');
                strRRedirtUrl = strRedirtUrl.Substring(0, intRedirtUrl) + "/" + strHtmlFileName;
                if (File.Exists(strPhysicsDirectory + "\\" + strHtmlFileName))//判断是存在有该html文件,如果存在,则转向,如果不存在则创建
                {
                    //尝试读取已有文件   
                    Boolean bl = GetFileStream(strPhysicsDirectory + "\\" + strHtmlFileName);
                    //如果文件存在并且读取成功 
                    if (bl == true)//没有过期
                    {
                        //using (st)
                        //{
                        // StreamToStream(st, HttpContext.Current.Response.OutputStream);
                        Response.Redirect(strRRedirtUrl);
                        //}
                    }
                    else //写文件
                    {
                        File.Delete(strPhysicsDirectory + "\\" + strHtmlFileName);
                        WriteHtml(strRequestUrl, strPhysicsDirectory + "\\" + strHtmlFileName);
                        Response.Redirect(strRRedirtUrl);
                    }
                }
                else
                {
                    if (WriteHtml(strRequestUrl, strPhysicsDirectory + "\\" + strHtmlFileName) == true)//写文件
                    {
                        Response.Redirect(strRRedirtUrl);
                    }
                }
                // }
                // catch
                // {
                //     Response.Redirect("http://www.xxxx.com");
                // }
            }
             /// <summary>
            /// 生成Html文件
            /// </summary>
            /// <param name="strUrl"></param>
            /// <param name="strHtml"></param>
            /// <returns></returns>
            private bool WriteHtml(String strUrl, String strHtml)
            {            StringWriter sw = new StringWriter();
                if (strUrl.Contains("?") == false)
                {
                    HttpContext.Current.Server.Execute(strUrl + "?w=1", sw);
                }
                else
                {
                    HttpContext.Current.Server.Execute(strUrl + "&w=1", sw);
                }
                string content = sw.ToString();
                try
                {
                    using (FileStream fs = new FileStream(strHtml, FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        using (StreamWriter stw = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))
                        {
                            stw.Write(content);
                        }
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
            }        /// <summary>
            /// 一小时后过期
            /// </summary>
            /// <param name="filename"></param>
            /// <returns></returns>
            private Boolean GetFileStream(string filename)
            {
                try
                {
                    DateTime dt = File.GetLastWriteTime(filename);
                    TimeSpan ts = DateTime.Now - dt;
                    if (ts.TotalHours > 1) //一小时后过期
                    {                    return false;
                    }
                    else
                    {
                        return true;
                    }
                    //return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); 
                }
                catch
                {
                    return false;
                }
            }   
    /// <summary>
            /// 写文件
            /// </summary>
            /// <param name="src"></param>
            /// <param name="dst"></param>
            private void StreamToStream(Stream src, Stream dst)
            {
                byte[] buf = new byte[4096];
                while (true)
                {
                    int c = src.Read(buf, 0, buf.Length);
                    if (c == 0)
                        return;
                    dst.Write(buf, 0, c);
                }
            }        /// <summary>
            /// 判断Url地址
            /// </summary>
            /// <param name="strUrl"></param>
            /// <returns></returns>
            private Boolean CheckUrl(String strUrl)
            {
                if (strUrl.ToLower().EndsWith("com") == true)
                {
                    return false;
                }
                else if (strUrl.ToLower().EndsWith("com/") == true)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
      

  3.   

    LZ现在用的是伪静态,就是在地址栏里打.html访问其实访问的还是.ASPX页面.
    要做到真正的静态页可以参考:http://topic.csdn.net/u/20080624/10/26a45062-572c-47e9-bb38-d9aa05b6c2ed.html
    不过要说下伪静态比这种生成静态要快.
      

  4.   

    晕,你这也算是静态化?这个叫url重写,静态化是不需要.netframework都可以运行的html页面,8楼有写文件代码,你也可以在网上找,有这样的静态引擎的
      

  5.   

    现在一般动态网站要实现真正的静态还是很复杂的,要考虑得很周全,个人觉得用URL重写就好,维护开发都方便。性能方面现在也不会差很多。