ASP.NET生成静态文件的一个静态类

简介: 某网友刚刚传给我的,稍微看了一下,感觉还不错吧,先记下来,备用,今天早上有段时候JAVAEYE上去好慢。不知道是不是服务器被攻击了。。呵呵。 using System;using System.
      某网友刚刚传给我的,稍微看了一下,感觉还不错吧,先记下来,备用,今天早上有段时候JAVAEYE上去好慢。不知道是不是服务器被攻击了。。呵呵。
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Net;
using  System.IO;
using  System.Text;
using  System.Web.UI.HtmlControls;
using  System.Text.RegularExpressions;

///   <summary>
/// CreateHtml 的摘要说明
///   </summary>
public   class  CreateHtml:System.Web.UI.Page
{
    
public  CreateHtml()
    {
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }
    
///   <summary>
    
///  生成静态页面,生成位置是本项目下
    
///   </summary>
    
///   <param name="strURL"> 请求的URL </param>
    
///   <param name="strRelPath"> 创建的路径及文件名,路径为相对路径 </param>
     public   bool  Nei_Create( string  strURL,  string  strRelPath)
    {
        
string   strFilePage;
      
        strFilePage 
=  HttpContext.Current.Server.MapPath(strRelPath);
        StreamWriter sw 
=   null ;
        
// 获得aspx的静态html
         try
        {
            
            
if  (File.Exists(strFilePage))
            {
                File.Delete(strFilePage);
            }
            sw 
=   new  StreamWriter(strFilePage,  false , System.Text.Encoding.GetEncoding( " gb2312 " ));
            System.Net.WebRequest wReq 
=  System.Net.WebRequest.Create(strURL);
            System.Net.WebResponse wResp 
=  wReq.GetResponse();
            System.IO.Stream respStream 
=  wResp.GetResponseStream();
            System.IO.StreamReader reader 
=   new  System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding( " gb2312 " ));
            
string  strTemp  =  reader.ReadToEnd();

            Regex r1 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __EVENTTARGET\ " .*/> " , RegexOptions.IgnoreCase);
            Regex r2 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __EVENTARGUMENT\ " .*/> " , RegexOptions.IgnoreCase);
            Regex r3 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __VIEWSTATE\ " .*/> " , RegexOptions.IgnoreCase);

            Regex r4 
=   new  Regex( " <form .*id=\ " form1\ " > " , RegexOptions.IgnoreCase);
            Regex r5 
=   new  Regex( " </form> " );

            Regex r6 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __EVENTVALIDATION\ " .*/> " , RegexOptions.IgnoreCase);
            strTemp 
=  r1.Replace(strTemp,  "" );
            strTemp 
=  r2.Replace(strTemp,  "" );
            strTemp 
=  r3.Replace(strTemp,  "" );
            strTemp 
=  r4.Replace(strTemp,  "" );
            strTemp 
=  r5.Replace(strTemp,  "" );
            strTemp 
=  r6.Replace(strTemp,  "" );

            sw.Write(strTemp);
        }
        
catch  (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            
return   false ; // 生成到出错
        }
        
finally
        {
            sw.Flush();
            sw.Close();
            sw 
=   null ;
        }

        
return   true ;
    }
    
///   <summary>
    
///  生成静态页面,生成位置不在本项目下
    
///   </summary>
    
///   <param name="strURL"> 请求的URL </param>
    
///   <param name="strRelPath"> 创建的路径及文件名,路径为绝对路径 </param>
     public   bool  Wai_Create( string  strURL,  string  strRelPath, string  filename)
    {
        
string  strFilePage;
        strFilePage 
=  strRelPath  +   " \\ "   +  filename;
        StreamWriter sw 
=   null ;
        
// 获得aspx的静态html
         try
        {
            
if  ( ! Directory.Exists(strRelPath))
            {
                Directory.CreateDirectory(strRelPath);
            }
            
if  (File.Exists(strFilePage))
            {
                File.Delete(strFilePage);
            }
            sw 
=   new  StreamWriter(strFilePage,  false , System.Text.Encoding.GetEncoding( " gb2312 " ));
            System.Net.WebRequest wReq 
=  System.Net.WebRequest.Create(strURL);
            System.Net.WebResponse wResp 
=  wReq.GetResponse();
            System.IO.Stream respStream 
=  wResp.GetResponseStream();
            System.IO.StreamReader reader 
=   new  System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding( " gb2312 " ));
            
string  strTemp  =  reader.ReadToEnd();

            Regex r1 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __EVENTTARGET\ " .*/> " , RegexOptions.IgnoreCase);
            Regex r2 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __EVENTARGUMENT\ " .*/> " , RegexOptions.IgnoreCase);
            Regex r3 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __VIEWSTATE\ " .*/> " , RegexOptions.IgnoreCase);

            Regex r4 
=   new  Regex( " <form .*id=\ " form1\ " > " , RegexOptions.IgnoreCase);
            Regex r5 
=   new  Regex( " </form> " );

            Regex r6 
=   new  Regex( " <input type=\ " hidden\ "  name=\ " __EVENTVALIDATION\ " .*/> " , RegexOptions.IgnoreCase);
            strTemp 
=  r1.Replace(strTemp,  "" );
            strTemp 
=  r2.Replace(strTemp,  "" );
            strTemp 
=  r3.Replace(strTemp,  "" );
            strTemp 
=  r4.Replace(strTemp,  "" );
            strTemp 
=  r5.Replace(strTemp,  "" );
            strTemp 
=  r6.Replace(strTemp,  "" );

            sw.Write(strTemp);
        }
        
catch  (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
            
return   false ; // 生成到出错
        }
        
finally
        {
            sw.Flush();
            sw.Close();
            sw 
=   null ;
        }

        
return   true ;

    }
    
public   void  FilePicDelete( string  path)
    {
        System.IO.FileInfo file 
=   new  System.IO.FileInfo(path);
        
if  (file.Exists)
            file.Delete();
    }
}
目录
相关文章
|
8月前
|
开发框架 前端开发 .NET
ASP.NET Core 基础知识之​Startup 类配置
Startup 类配置服务和应用的请求管道。
113 0
|
开发框架 前端开发 JavaScript
ASP.NET Core静态文件的使用方法
静态文件(HTML,CSS,图片和Javascript之类的资源)会被ASP.NET Core应用直接提供给客户端。 静态文件通常位于网站根目录(web root) <content-root>/wwwroot文件夹下。通常会把项目的当前目录设置为Content root,这样项目的web root就可以在开发阶段被明确。
ASP.NET Core静态文件的使用方法
|
开发框架 前端开发 安全
ASP.NET Core Startup类Config gure()方法|ASP.NET Core 中间件详细说明
目录 Startup 类 Configure() 方法 中间件 使用中间件 Configure 方法 的参数 IApplicationBuilder Extension Methods(拓展方法)--微软提供的中间件
146 0
ASP.NET Core Startup类Config gure()方法|ASP.NET Core 中间件详细说明
|
XML 开发框架 JSON
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(五)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
194 0
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(五)
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(四)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
134 0
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(四)
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(三)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
323 0
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(二)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
251 0
|
JSON 开发框架 .NET
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(一)
上一章的结尾留下了一个问题:同样是ObjectResult,在执行的时候又是如何被转换成string和JSON两种格式的呢? 本章来解答这个问题,这里涉及到一个名词:“内容协商”。除了这个,本章将通过两个例子来介绍如何自定义IActionResult和格式化类。
271 0
ASP.NET Core: 二十一. 内容协商与自定义IActionResult和格式化类(一)
|
开发框架 中间件 .NET
ASP.NET Core : 十四.静态文件与访问授权、防盗链(下)
我的网站的图片不想被公开浏览、下载、盗链怎么办?本文主要通过解读一下ASP.NET Core对于静态文件的处理方式的相关源码,来看一下为什么是wwwroot文件夹,如何修改或新增一个静态文件夹,为什么新增的文件夹名字不会被当做controller处理?访问授权怎么做?
291 0
|
开发框架 前端开发 JavaScript
ASP.NET Core : 十四.静态文件与访问授权、防盗链(上)
我的网站的图片不想被公开浏览、下载、盗链怎么办?本文主要通过解读一下ASP.NET Core对于静态文件的处理方式的相关源码,来看一下为什么是wwwroot文件夹,如何修改或新增一个静态文件夹,为什么新增的文件夹名字不会被当做controller处理?访问授权怎么做?
209 0

相关实验场景

更多