Thursday, January 24, 2008

share ASP.NET user controls between applications (aspnet_merge)

You want to create some shared ASP.NET controls for use in multiple applications.
Probably the only way you think of is custom control:

the only way to share the user control between applications is to put a separate copy in each application, which takes more maintenance if you make changes to the control (MSDN)

But it isn't actually true.

You can use "Web Deployment Projects" add-in to compile your user controls project into dll which can be referenced from any web application.

Now let's take it step by step:

(make sure you have installed the "Web Deployment Projects" add-in)

  1. Create a new web project (SharedUserControls.csproj).
  2. Remove the "Default.aspx" page and web.config file.
  3. Add a user control (let's call it MyControl.ascx).
  4. Add what ever web controls you want to your user control.

    image 
  5. Add Web Deployment Project by standing on the project and selecting "Add Web Project Project..." from the Build menu

    image 
  6. Keep the default name, and click OK.

     image
    A new project was created with out any file inside

    image 
  7. Double click the new project to get the project property pages.

     image
  8. Uncheck the last check box - "Allow this precompiled site to be updatable".

    This is an important step - If you'll skip it, you'll get: ASPNETMERGE : warning 1013: Cannot find any assemblies that can be merged in the application bin folder.
  9. Go to the "Output Assemblies" tab
  10. Select "Merge all pages and control outputs to a single assembly"
  11. Call the assembly name: "SharedUserControlMerged"

    image 
  12. Build the solution, and see what you get in the Output:

    aspnet_compiler.exe...
    Running aspnet_merge.exe ...
    aspnet_merge.exe...
    Successfully merged...

    What happens is that the "Deployment Web Project" actually compiles the aspx and ascx files - using aspnet_compiler.

    Then merge all dlls (currently we have only one - because we have only one control) using aspnet_merge (which uses ILMerge behind the scenes).
  13. Now add another Web Project (WebApp.csproj).
  14. Add reference to the SharedUserControlMerged.dll from the bin folder of the SharedUserControls.csproj_deploy project.

    image 

  15. Register this assembly inside a web page on WebApp project.

    <%@Register
    tagprefix="xyz"
    namespace="ASP" 
    assembly="SharedUserControlMerged"%>

  16. Now you can use your control on this page:

    You need first to find out what is the name of the control in the merged dll.
    I have used Reflector to do it:

    image
    and just put it in the page:

    <xyz:mycontrol_ascx runat="Server"></xyz:mycontrol_ascx>


    So what we have is a reusable dll contains our User Control.


    Next time we will find out how to create user control with images and JavaScript files.

14 comments:

Anonymous said...

this is a great article for how to generate dll and reuse it another application

please can you tell me how can i generate dll with javascript and images

thanks a lot in advance

Anonymous said...

Thanks a lot yaar , great article,
everything is working fine but I couldn't view the output I mean no controls has been load on page, what might be the issue.
could u provide me the source code r what else I need to do
Thanks in advance,
mail: sendil.bonanzo@gmail.com

Anonymous said...

Thanks, but also
for me elements of control can't load.

Anonymous said...

*bump*
anyone getting this to work, so the controls is actually visible on the host page??

Does VS2008 anything to do with it?

Anonymous said...

We have been getting that warning forever...

aspnet_merge : warning 1013: Cannot find any assemblies that can be merged ...

Thank you!

Vladislav Bogomolny said...

It's a really great article. Much better than MSDN :). Everything works as magic. Controls are visible in hosting web application. One issue I had that easily resolved. Hosting application needs references to original and merged assembly.

hannimed said...

you promised to write about how to use images and JavaScript, when you write about it? :)

Jon said...

So long time...
Anyway - the concept is to emmbbed the javascript and images into the dll, then call it from there.

See MSDN for more info:
http://msdn.microsoft.com/en-us/library/bb398930.aspx

Max said...

Great post! More than two years have passed but it still helps people! Thank you so much!

Jason Dufair said...

Thank you!

Anonymous said...

Thanks for a great article, still valid with VS 2010

Anonymous said...

como puedo compartir mis user control a mas de un website. en visual basic 2005.

Anonymous said...

cant find this "web deploy ad-in" in VS 2010 :(

Anonymous said...

see also:

ASP.NET: Reusing Web User Controls and Forms

http://www.cmswire.com/cms/tips-tricks/aspnet-reusing-web-user-controls-and-forms-000915.php