aspx页面静态化

IIS设置:站点右键属性,自定义错误,找到404,修改为url,将url设置成/Default.aspx

测试一下:随便敲个地址,比如:http://localhost/123.html,应该跳转到Default.aspx

然后在Default.aspx中写如下逻辑:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.RawUrl.IndexOf(".html") == -1)
            {
                return;
            }


            string filePath = Server.MapPath("/");
            string[] temp = Request.RawUrl.Split('/');
            string htmlFile = temp[temp.Length - 1];
            string aspxFile = htmlFile.Replace(".html", ".aspx");


            bool b = false;


            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://" + Request.Url.Host + ":" + Request.Url.Port.ToString() + "/" + aspxFile);
                httpWebRequest.Method = "GET";
                using (Stream stream = httpWebRequest.GetResponse().GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8))
                    {
                        FileStream fileStream = new FileStream(filePath + htmlFile, FileMode.Create);
                        byte[] contents = Encoding.Default.GetBytes(streamReader.ReadToEnd());
                        fileStream.Write(contents, 0, contents.Length);
                        fileStream.Close();


                        b = true;
                    }
                }
            }
            catch (Exception exception)
            {
                Response.Write(exception.ToString());
            }
            finally
            {
            }


            if (b)
            {
                Server.Transfer(htmlFile, true);
            }
        }


大概意思是,如果输入的是index.html,那么就会读取index.aspx的动态内容,然后创建index.html

这样第二次访问http://localhost/index.html,就是访问静态页了,然后再做个反向代理(nginx)呗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值