Reputation: 22251
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());
Upvotes: 1
Views: 1347
Reputation: 22578
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.
Upvotes: 3