• asp.net静态页面实例


    protected void Button1_Click(object sender, EventArgs e)
    {
    string url = "http://" + Request.Url.Authority + "/default.aspx";
    new System.Net.WebClient().DownloadFile(url, Server.MapPath("~/default.html"));
    Response.Redirect(
    "default.html");
    }

    接前一文章:asp.net网站生成静态页面演示示例

    这个代码方案也是参考了很多人的代码写出的。在此对这些无名的奉献者表示感谢。

    贴一个核心的代码吧:

    using System;
    using System.Web;
    using System.Web.UI;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Configuration;

    namespace BLL
    {
    /// <summary>
    /// PageBase 的摘要说明。
    /// </summary>
    public class PageBase : Page
    {
    private static object obj = new object();

    //// <summary>
    /// 重写默认的HtmlTextWriter方法,修改form标记中的value属性,使其值为重写的URL而不是真实URL。
    /// </summary>
    /// <param name="writer"></param>
    protected override void Render(HtmlTextWriter writer)
    {
    string url = Request.RawUrl.ToLower();
    if (Request.RequestType.ToLower().Equals("get"))
    {
    URLRewrite u
    = (URLRewrite)HttpContext.Current.Items["siteurl"];
    if (u != null && u.Cache > 0)
    {
    string CacheFile = Server.MapPath("/Cache" + url + ".html");

    FileUtil.CreateDirectory(CacheFile);

    lock (obj)
    {
    using (StreamWriter sw = new StreamWriter(CacheFile, false, Encoding.UTF8))
    {

    base.Render(new FetchHtmlWriter(writer, sw));
    sw.Flush();
    sw.Close();
    sw.Dispose();
    }
    Cache.Add(url,
    string.Empty, null, DateTime.Now.AddSeconds(u.Cache), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
    return;
    }
    }
    }
    if (writer is System.Web.UI.Html32TextWriter)
    {
    writer
    = new FormFixerHtml32TextWriter(writer.InnerWriter);
    }
    else
    {
    writer
    = new FormFixerHtmlTextWriter(writer.InnerWriter);
    }

    base.Render(writer);
    }
    }

    public class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
    {
    public FormFixerHtml32TextWriter(TextWriter writer)
    :
    base(writer)
    {
    }

    public override void WriteAttribute(string name, string value, bool encode)
    {
    // 如果当前输出的属性为form标记的action属性,则将其值替换为重写后的虚假URL
    if (string.Compare(name, "action", true) == 0)
    {
    value
    = HttpContext.Current.Request.RawUrl;
    }
    base.WriteAttribute(name, value, encode);
    }
    }


    public class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
    {
    public FormFixerHtmlTextWriter(TextWriter writer)
    :
    base(writer)
    {
    }

    public override void WriteAttribute(string name, string value, bool encode)
    {
    if (string.Compare(name, "action", true) == 0)
    {
    value
    = HttpContext.Current.Request.RawUrl;
    }

    base.WriteAttribute(name, value, encode);
    }
    }
    }

    详细代码下载:asp.net生成静态页面

    有不清楚的地方,可以加QQ群:49745612一起探讨。

    跟前一种,生成静态页面的方案比,这种复杂得多,个人更喜欢前面的实现方式。

  • 相关阅读:
    larave5.6 引入自定义函数库时,报错不能重复定义
    laravel获取当前认证用户登录
    淘宝免费ip地址查询导致服务堵死的坑
    this关键字
    Jsp Spring Security 权限管理系统
    spring secrity
    spring bean何时实例化
    Spring Security3 页面 权限标签
    Spring常用注解,自动扫描装配Bean
    java继承时,实例化子类,是否会默认调用父类构造方法
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175917.html
Copyright © 2020-2023  润新知