Reusable ASP.NET User Control Library : Virtual path provider or ascx/aspx copy

asp.netuser-controlsvirtualpathprovider

I have a ASP.Net web application that I want to use as a reusable user control library in other web applications.

One solution for this problem is to use what Scott Guthrie has described here:

http://weblogs.asp.net/scottgu/archive/2005/08/28/423888.aspx

that is to copy ascx/aspx files (without their code-behind) in the web applications which use the control library.

I actually see another solution: to embed the ascx/aspx in the User control library and then use a custom virtual path provider to get them.

Does anybody know which solution is the best ?

From the deployment point-of-view, the virtual path provider seems to be better.
However the 'ascx/aspx copy' solution is easier to implement (no need to create a custom virtual path provider).

Best Solution

I really do not like Scott's solution. I believe it is ungly and creates overhead that is not necessary. I have also seen something similar in action and it just didn't work for me.

I too had the same requirement as you describe and I used the virtual path provider option. This way I could reuse all my user controls between web applications easily and without work arounds.

The virtual path provider has a couple of issues though:

  • If your ascx file has any server tags (in javascript or any other place for that matter) that cause an issue you will get a run time error that is not that helpful.
  • You have to rebuild your application every time you do a change to the ascx's mark up or to Javascript that exists on the file it self in order to see the results. That can be a real pain if you are trying to change the design of an existing control.

I have used the solution as described in this article and it worked very well:

http://www.codeproject.com/KB/user-controls/EmbeddedUserControl.aspx

I hope that helps...

Related Question