MVC 内容转为html页面

  public bool ExecMVCToHtml()
        {
            string url = aspxUrl;
            if (url.IndexOf("/") != 0)
                url = "/" + url;
            string address = Framework.Utils.RootURL + url;
            StreamWriter sw = new StreamWriter(htmlSavePath, false, encoder);
            try
            {
                string strResult = Utils.GetWebRequest(address, encoder);
                
                if (!string.IsNullOrEmpty(strResult))
                {
                    sw.Write(strResult); //将strHTML中的字符写到.html中
                    sw.Flush();
                    return true;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
            finally
            {
                sw.Close(); //关闭StreamWriter 
            }
        }
       

 

/// <summary>
        /// 通过HttpWebRequest获取结果
        /// </summary>
        public static string GetWebRequest(string url, Encoding encoder)
        {
            Stream myStream = null;
            StreamReader sr = null;
            try
            {
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                myStream = HttpWResp.GetResponseStream();
                sr = new StreamReader(myStream, encoder);
                return sr.ReadToEnd();
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                if (sr != null)
                    sr.Close();
                if (myStream != null)
                    myStream.Close();
            }
        }

 

posted @ 2019-09-28 15:28  wonderfulviews  阅读(625)  评论(0编辑  收藏  举报