网站改版时404错误

来源:互联网 发布:dota1618牛蛙淘宝店 编辑:程序博客网 时间:2024/05/21 10:37
 

1、在IIS中先看一下  404错误的页面,然后再根目录下创建 404.aspx

<%@ Page Language="C#"%>  
<%@ Import Namespace="System.IO" %>  
<%@ Import Namespace="System.Text" %>  
<%@ Import Namespace="System.Net" %>  
<%@ Import Namespace="System.Web" %>  
<script runat="server">  
    public string strUrl{  
        get{return Request.Url.ToString().ToLower();}  
    }  
 
    string strSoureUrl = "http://www.sosuo8.com/";   //目的站地址  
    string strTemp = string.Empty;  
 
    protected void Page_Load(object sender, EventArgs e){  
 
         if (strUrl.Contains("/article/show.asp?id=")){  
            int id = 0;  
            //对整型必须尝试转换,防止SQL注入  
            try 
            {  
                id = int.Parse(GetParam("id"));  
            }  
            catch 
            {  
              
            }  
            if (id != 0){   
                strTemp = GetSourceTextByUrl(strSoureUrl + "/article/show-"+id+".aspx");  
                outputResult(strTemp);  
            }                 
        }  
 
 
    }  
      
    //根据返回东西输出结果  
    public void outputResult(string str){  
       if (str == ""){  
           strTemp = GetSourceTextByUrl(strSoureUrl + "/error.html");  
           outputResult(strTemp);  
          //Response.Write("有错误发生");  
       }else{  
            Response.Write(str);  
            Response.End();  
       }  
    }  
      
    public string GetSourceTextByUrl(string url)  
    {  
        WebRequest request = WebRequest.Create(url);  
        request.Timeout = 200000;//20秒超时      
        WebResponse response = request.GetResponse();  
        Stream resStream = response.GetResponseStream();  
        StreamReader sr = new StreamReader(resStream);  
        string tempstr = sr.ReadToEnd();  
        return tempstr;  
 
    }  
 
    //取得参数值  
    //add by ahuinan 2009-12-18  
    public string GetParam(string parameName)  
    {  
         
            //取得从问号后面开始的部分  
            string[] strs = strUrl.Split('?');  
            if (strs.Length >= 3)  
            {  
                string[] strParams = strs[2].ToString().Split('&');  
                string[] strParamValues = new string[2];  
                for (int i = 0; i < strParams.Length; i++)  
                {  
                    strParamValues = strParams[i].Split('=');  
                    if (strParamValues[0] == parameName)  
                    {  
                        return strParamValues[1];  
                    }  
                }  
            }  
            return "";  
         
    }  
</script>