How to Register an Assembly or DLL in Web Applications

Question

I have a System.Web.DataVisualization.dll present in my bin folder. Now I have added the registration tag to my page as below:

<%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

Now how to deermine the Assembly and Namespace from System.Web.DataVisualization.dll?

Answer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Reflection;

using System.Threading;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();

 

        Assembly myAssembly = null;

        for (int i = 0; i < myAssemblies.Length; i++)

        {

            if (String.Compare(myAssemblies[i].GetName().Name,

                              "System.Web.DataVisualization") == 0)

            {

                myAssembly = myAssemblies[i];

                break;

            }

        }

 

        if (myAssembly != null)

        {

            //This will give the Version Information.

            var myVersion = myAssembly.GetName().Version;

            //This will give the Namespace Infromation.

            var namespaces = myAssembly.GetTypes()

                         .Select(t => t.Namespace)

                         .Distinct();

        }

    }

}

1. You can find the Version Information from the following code statement.

var myVersion = myAssembly.GetName().Version;

Image 1.jpg

2. You can also find the Namespace Information from the following code statement:
 

var namespaces = myAssembly.GetTypes()

                         .Select(t => t.Namespace)

                         .Distinct();


Image 2.jpg

3. So I can generate the register tag as follows:

<%@ Register Assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>