Question

Which of these is most efficient? Assuming they all output the same string.

Does load .LoadControl use WebClient as well?

b  = new StringBuilder();

// ascx
new UserControl().LoadControl("/_includes/test1.ascx").RenderControl(new HtmlTextWriter(new StringWriter(b)));    
Console.Write(b.ToString());

// ashx
b = new StringBuilder(new WebClient().DownloadString(site.Url + "/_includes/test2.ashx"));
Console.Write(b.ToString());

// aspx
b = new StringBuilder(new WebClient().DownloadString(site.Url + "/_includes/test3.aspx"));
Console.Write( b.ToString());
Was it helpful?

Solution

Order of efficiency (most to least, given your scenario):

User Control, Handler (ashx), Web Page (aspx).

The UserControl will be handled by the same request on IIS. The other two scenarios require 2 requests, one for the initial page and one for the secondary handler or web page.

Loading external data in the case of the Handler and Web Page will have subtle differences but a Handler is lighter weight than a Web Page so it wins out in that case.

Lastly, I am not sure what purpose the use of a StringBuilder serves in the latter two cases.

Bottom line, you should likely test these different methods. My "answer" is based on some broad assumptions. Your millage may very.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top