Web.config register user control in another project - asp.net

I have a .Net project called:
Whatever.Web.UI: I use this project to put all the custom and user controls that will be reused in all the website projects. These are organized in two folders:
- CustomControls folder (with all the custom controls)
- UserControls folder (with all the user controls)
This are future examples of web projects that should use these user and custom controls
Whatever.Web.UI.WebSite1
Whatever.Web.UI.WebSite2
....
....
....
Whatever.Web.UI.WebSiteN
To use the custom controls in each WebSite project is not a problem, I registered it in web.config using the following way:
<add tagPrefix="CustomControls" namespace="Whatever.Web.UI.CustomControls" assembly="Whatever.Web.UI"/>
But how could I register the user controls taking in count that they have an ascx part that I need to reference, in order to show each website?
Thanks in advance.
Kind Regards,
Josema.

<system.web>
<pages>
<controls>
<add src="~/UserControls/MyControl.ascx" tagName="MyControl" tagPrefix="MyControls"/>
</controls>
</pages>
</system.web>

Related

Web.config add namespace not applied at file level

I am referencing a class library in a web application project (both in same solution). Within the web pages of the web application, if I do this:
If MyValidation.CorrectEmailFormat(email) Then ...
...Visual Studio 2013 underlines the method, and suggests I import MyCompany.EmailMethods at the top of the page. If I do the Import, the page compiles and the method works okay.
However, because these methods are used extensively across the application, I don't want to add them at page level every time. So I headed for web.config, and did this:
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />
</namespaces>
</pages>
However, VS is still prompting me to perform the Import at the top of every page, and the method is not recognised in the page without doing this. What I am doing wrong please? I assumed from MSDN and other sources this was the correct way to achieve this.
Web application is ASP.Net web pages (4.6).
The reference must be added to the Imported Namespace as described in the following SO post
add-a-namespace-reference-to-all-web-application-web-pages-in-a-different-project
It must be added in the project properties page at the bottom part titled Imported Namespaces
The <pages> directive applies to ASPX files only.
You need to use the equivalent directive for Razor:
<system.web.webPages.razor>
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />

need advice on referencing my libraries from the WebApp

I have 3 Class Libraries that my WebApp references. One of these libraries has folders to organize the classes in it. When creating classes in these sub folders I kept all namespaces as default.
I always thought that if you add the namespaces to the web.config you do not have to add them in 'using statements' at the top of individual aspx.cs pages. In a VB.NET Website I think it works but not in a C# WebApp. Is there anything additional that I must do or is it not possible?
<namespaces>
<add namespace="Library1" />
<add namespace="Library2" />
<add namespace="Library3.Admin" />
<add namespace="Library3.CRM" />
</namespaces>
Also I never knew that you couldn't use the App_Code folder in a Web Application until now. Where would you recommend creating a BasePage.cs file.

Why I can declare only one <page> in ASP.NET web.config file?

I'm new to ASP.NET and just start with Beginning ASP.NET 4 in C# and VB. I'm doing exercise on UserControl and I stuck in site-wide registration UserConrols that add registration syntax in web.config like below.
<pages theme="Monochrome">
<controls>
<add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx" />
</controls>
</pages>
After I added this section to web.config everything work fine, since I have two themes in my website so I add new section for other theme.
<pages theme="DarkGrey">
<controls>
<add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx" />
</controls>
</pages>
This time I can't compile my website, my user control doesn't show up at design-time. I want to know why I can't have two <page> section?
The <pages> node refers to all pages in your Website, so you can only have one of it. The theme attribute sets the default theme for all your pages here. You don't have to register the control twice for different themes.
You can set different themes for different areas, but not the way you are trying to do it.
Read How to: Apply ASP.NET Themes:
A theme setting in the Web.config file applies to all ASP.NET Web pages in that application. Theme settings in the Web.config file follow normal configuration hierarchy conventions. For example, to apply a theme to only a subset of pages, you can put the pages in a folder with their own Web.config file or create a element in the root Web.config file to specify a folder. For details, see Configuring Specific Files and Subdirectories.

add default namespaces

Is it possible to somehow add default namespaces to each code file that I am editing in Visual Studio 2010 web site project? I am tired of specifying them each time I edit a page because they are pretty much the same on each page.
You can add namespaces in web.config, in the <pages> section in order to not have to include them in every page.
You add them like this:
<pages>
<namespaces>
<add namespace="System.Web.UI"/>
</namespaces>
</pages>
There are no namespaces in Web site projects. If you want that, you have to use the Web Project template.

Visual Studio 2008 user controls registered in the web.config not registering with intellisense

I have a C# Web application built in Visual Studio 2008 where we rely heavily on user controls for 'pre-built' blocks of ASP.NET.
Instead of registering a big stack of user controls on each page, we register all the controls in a local (to a specific folder) web.config file. The advantage is that we can use the controls and the pages look 'cleaner' in source view. However, neither the VS2008 Design view nor Intellisense recognize the fact the controls are registered in web.config. However, the application itself works as expected.
Normally at the top of each page we'd have a tag like this:
<%# Register src="~/CommonControls/Foo.ascx" tagname="Foo" tagprefix="Bar" %>
And we register the controls in a local web.config like so:
<?xml version="1.0"?>
<configuration>
<system.web>
<pages>
<controls>
<add src="~/CommonControls/Foo.ascx" tagName="Foo" tagPrefix="Bar"/>
</controls>
</pages>
</system.web>
</configuration>
Does anyone know of a fix for getting Intellisense to recognize these custom controls? Or, is there a 'better' way of doing this?
Whilst this doesn't work in a local Web.Config file you can use the Location tag in your root web.config file to achieve the same result.
For example:
<?xml version="1.0"?>
<configuration>
<location path="MyPath">
<system.web>
<pages>
<controls>
<add src="~/CommonControls/Foo.ascx" tagName="Foo" tagPrefix="Bar"/>
</controls>
</pages>
</system.web>
</location>
</configuration>
It probably has to do with the fact that it isn't registered in the main web.config file. Test it out by putting one or two references in the main web.config file.
I tried your sample code and Intellisense works in both the code view of the aspx page and the code behind page. You might check the class names of your user controls, visual studio includes the folder name in the class name by default. So your control may be named CommonControls_Foo instead of Foo as you would expect.
Either way the web.config file has nothing to do with the Intellisense of the code behind. Check the name and namespace of the class.
Could it because you are registering the control twice and intellisense is unable to resolve the reference?
I would exepect that the control should be registered in either a config file or in the page directive

Resources