In Web.config Is it possible to register all user controls in the specified directory

I am currently registering each user control separately in Web.config

<pages validateRequest="false">
  <controls>
    <add tagPrefix="cc1" src="~/Controls/MyUserControl1.ascx" tagName="MyUserControl1"/>
    ...    
    <add tagPrefix="cc1" src="~/Controls/MyUserControlN.ascx" tagName="MyUserControlN"/>
  </controls>
</pages>

But from time to time I forget to check web.config. In fact, I usually forget that he changed to skip it because he often breaks the settings set by others to connect to their local copy of DB DB.

I was wondering if you can simply specify the entire Controls directory and get all the controls registered there automatically

+5
source share
4 answers

Yes and no.

, web.config. , .

, usercontrols, . , , :

  • web.config, , , , .
  • , web.config.
  • , web.config, ... .
  • , , .

, . .

.

+8

web.config, . ... , web.config.

web.config . web.config.

+1

Web.Config Global.asax:

protected void Application_Start(object sender, EventArgs e)
{

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
    PagesSection webSection = config.GetSection("system.web/pages") as PagesSection;

    List<TagPrefixInfo> toRemove = new List<TagPrefixInfo>();
    foreach (TagPrefixInfo info in webSection.Controls)
    {
        if (info.TagPrefix != "asp")
            toRemove.Add(info);
    }

    foreach (TagPrefixInfo list in toRemove)
    {
        webSection.Controls.Remove(list);
    }

    DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Controls"));
    foreach (FileInfo file in di.GetFiles("*.ascx"))
    {
        TagPrefixInfo newtag = new TagPrefixInfo("PREFIX", null, null, file.Name.Replace(".ascx",""), string.Concat("~/Controls/", file.Name));
        webSection.Controls.Add(newtag);

    }

    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("system.web/pages");

}

... web.config.

+1

VB.NET @Jeff Fritz. , , . ( , SVN, , - .)

    Sub RegisterUserControls()
        Dim Config As Configuration = WebConfigurationManager.OpenWebConfiguration("~/")
        Dim WebSection As PagesSection = TryCast(Config.GetSection("system.web/pages"), PagesSection)
        Dim ToRemove As List(Of TagPrefixInfo) = ( _
                From t As TagPrefixInfo In WebSection.Controls _
                Select t Where t.Source IsNot Nothing AndAlso t.Source.EndsWith(".ascx") _
            ).ToList
        For Each t As TagPrefixInfo In ToRemove
            WebSection.Controls.Remove(t)
        Next
        Dim SiteRoot As New DirectoryInfo(Server.MapPath("~"))
        For Each f As FileInfo In SiteRoot.GetFiles("*.ascx", SearchOption.AllDirectories)
            Dim Source As String = Path.Combine("~/", f.FullName.Replace(SiteRoot.FullName, "")).Replace("\", "/")
            Dim TagName As String = Path.GetFileNameWithoutExtension(f.Name)
            Dim NewTag As New TagPrefixInfo( _
                tagPrefix:="YOURPREFIX", _
                nameSpace:=Nothing, _
                assembly:=Nothing, _
                TagName:=TagName, _
                Source:=Source)
            WebSection.Controls.Add(NewTag)
        Next
        Config.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection("system.web/pages")
    End Sub

(, Application_Start, .)

0

All Articles