본문으로 바로가기

[펌]비하인드 코드에서 웹페이지나 웹서비스를 html로 읽어 오고 싶을때..

category .NET/ASP.NET 2014.03.06 16:59

환경 : Visual Studio 2010

예제 파일 : ContentManager.zip

출처 : http://fivewind.tistory.com/m/post/view/id/91


public static string SeverControlToHtml(Control control) { System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw); control.RenderControl(hw); return sw.ToString(); }
Boost ASP.NET performance with deferred content loading
[WebMethod]
public string GetRSSReader()
{
  // Create a new Page and add the control to it.
  Page page = new Page();
  UserControl ctl = 
    (UserControl)page.LoadControl("~/RSSReaderControl.ascx");
  page.Controls.Add(ctl);
 
  // Render the page and capture the resulting HTML.
  StringWriter writer = new StringWriter();
  HttpContext.Current.Server.Execute(page, writer, false);
 
  // Return that HTML, as a string.
  return writer.ToString();
}
웹서비스를 이용한 페이지 부분 렌더링
public class AjaxPresenterService : WebServie ...
  public string Render(string viewName) {
        string viewPath = String.Format("~/Views/{0}.ascx", viewName);
        Type type = BuildManager.GetCompiledType(viewPath);
        ViewControl control = Activator.CreateInstance(type) as ViewControl

         Page dummyPage = new Page();
         dummyPage.Controls.Add(control);

         StringBuilder response = new StringBuilder();
         StringWriter responseWriter = new StringWriter(response);
         Server.Execute(dummyPage, responseWriter, true);

         return response.ToString();
  }
  ...



댓글을 달아 주세요