Exemplo n.º 1
0
        /// <summary>
        ///  通过实现 System.Web.IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。
        /// </summary>
        /// <param name="context">System.Web.HttpContext 对象,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session  和 Server)的引用。</param>
        public void ProcessRequest(HttpContext context)
        {
            //取得原始URL屏蔽掉参数
            string Url = context.Request.RawUrl;

            System.Xml.XmlDocument Dom = new System.Xml.XmlDocument();
            Dom.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["urlRewriter"]));//从web.config读取设置文件URLRewriterSettings.xml的路径
            System.Xml.XmlNodeList ItemList = Dom.SelectSingleNode(@"//URLRewriters").ChildNodes;
            foreach (System.Xml.XmlNode Node in ItemList)
            {
                //if(!Node.HasChildNodes) continue;
                String Ph = "";
                Ph = Node.Attributes.GetNamedItem(@"Path").InnerText;
                Rh = Node.Attributes.GetNamedItem(@"RealPath").InnerText;
                if (Ph.StartsWith(@"~"))
                {
                    Ph = HttpContext.Current.Request.ApplicationPath + Ph.Substring(1);
                }
                if (Rh.StartsWith(@"~"))
                {
                    Rh = HttpContext.Current.Request.ApplicationPath + Rh.Substring(1);
                }
                //建立正则表达式
                System.Text.RegularExpressions.Regex Reg = new System.Text.RegularExpressions.Regex(Ph, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Match m   = Reg.Match(Url); //匹配
                if (m.Success)                                             //成功
                {
                    for (int i = 1; i <= m.Groups.Count; i++)
                    {
                        Rh = Rh.Replace("$" + i.ToString(), m.Groups[i].Value);
                    }

                    //Context.Response.Write(Context.Request.Url.PathAndQuery);
                    //Context.RewritePath(RealPath);//(RewritePath 用在无 Cookie 会话状态中。)

                    context.Server.Execute(Rh);
                    //Context.Response.Write(Rh);
                    context.Response.End();
                    break;
                }
            }
            try
            {
                context.Server.Execute(context.Request.Url.PathAndQuery);
            }
            catch
            {
                TongUse TextData = new TongUse();
                // TextData.Err("在获取文件时出现错误,文件没有找到"+ex.Message);
                TextData.Res.Response.Write(context.Request.Url.PathAndQuery);
                context.Response.End();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 写入或替换XML文件 方法名: WriteXml 返回 bool值
        /// </summary>
        /// <param name="XmlString">要写入的XML文件代码 已格式化好的</param>
        /// <param name="Path">要写入的新文件名 要加Server.Path</param>
        /// <returns>返回 bool值</returns>
        public static bool WriteXml(string XmlString, string Path)
        {
            try
            {
                FileStream XmlFle = new FileStream(Path, FileMode.Create, FileAccess.ReadWrite);//对文件的引用后


                StreamWriter WrXml = new StreamWriter(XmlFle); //StreamWriter 可以向文件写入字符串,而 FileStream 则不能。这将使您的工作变得容易许多。
                WrXml.Write(XmlString);                        //写入文件 cc 为写入文件的内容
                WrXml.Close();                                 //关闭
                XmlFle.Close();                                //关闭
                return(true);
            }
            catch (Exception ex)
            {
                TongUse TextData = new TongUse();

                TextData.Err(ex.Message);
                return(false);
            }
        }