read webConfigurationManager.AppSettings from asp.net - asp.net

I have an asp.net page where I need to read some appSetting value from web.config. After googling, I found that I can use this code:
using System.Web.Configuration;
WebConfigurationManager.AppSettings["configFile"]
but after I insert the line using System.Web.Configuration by typing in this at my aspx page
<%# Using System.Web.Configuration; %>
I got this error: ASP.net runtime error. Only content controls are allowed directly in a content page that contains Content control. So how can I read the system web configuration file from my asp page if it doesn't allow me to import System.Web.Configuration?

I think this should do the trick (I've never used WebConfigutationManager but this works for me).
Code Behind
using System.Configuration
protected string GetForgottenUrl()
{
return ConfigurationManager.AppSettings["SomeKey"];
}
Aspx Page
<a href='<%= GetForgottenUrl()%>'>Forgot Password</a>
Web.Config
<configuration>
<appSettings>
<add key="SomeKey" value="SomePage.aspx" />
</appSettings>
</configuration>
EDIT:: You may need to add a reference to System.Configuration

Related

asp.net application getting master page name from appSettings key

I'm trying to figure out how an asp.net application is setting the MasterPageFile from a config file appSettings key like this:
<appSettings>
<add key="MasterPageFile" value="~/Other.Master" >
</appSettings>
This causes it to ignore the Page directive's MasterPageFile attribute in the .aspx files (which point to a different master page). I have searched through the whole solution and can't find any ConfigurationManager or MasterPageFile calls that are loading this appSettings key. From what I understand, you would usually have to put this in a system.web section of a config file in a pages element with a masterPageFile attribute.
Anyway, how is it setting the master page from this appSettings key? Is there some other way to retrieve appSettings that I don't know about?
MaserPage File can be set at page level or pre_init level. Setting it at config level will override your page settings so avoid it.

Register custom class in assembly with web project

I'm using web project and web forms.
I create Custom Class
Namespace CustomWebControls
Public Class NoValidationDDL
Inherits DropDownList
End Class
End Namespace
and i try to register this class in assembly web.config page section
<controls>
<add tagPrefix="customDDL" namespace="CustomWebControls" assembly="ae" />
</controls>
and try to use in ascx
<customDDL:NoValidationDDL ID ="id" runat="server"></customDDL:NoValidationDDL>
How can i do this in web project?
This should work (assuming the assembly name for your project is ae) - make sure that you build the project after creating/modifying your custom control. Unless updated 'ae.dll' is present in your bin directory, intellisense etc will not work.
Instead of modify web.config, you may also try register control at page(aspx) or control(ascx) level by adding register directive after page directive:
<%# Register Assembly="ae" Namespace="CustomWebControls" TagPrefix="customDDL" %>

Custom Control in ASP.NET C#

I created a simple custom control that only inherits from the Literal control, and doesn't have any extensions yet, code is empty.
Namespace: CustomControls
Class name: Literal : System.Web.UI.WebControls.Literal
Next thing I do is registering this control in the aspx page as following:
<%# Register TagPrefix="web" Namespace="CustomControls" %>
(I read in few tutorials that this is one of the ways to register it, besides web.config etc.)
After all, no intellisence for me, and worse- I get a parse error 'unknown server tag: web' when I try to run the page with the control in it.
I used 'create new project' and not new website, in case this info is needed.
What could be my problem?
Thanks in advance.
Here is how I did it, step by step starting from nothing. This first method uses a second project/assembly. For the App_code version scroll down.
Web Application Project Method
Create a new ASP.Net Web Application. Take note of the name, mine is called WebApplication2. If you already have an existing web application, which is likely, double click the properties section of your project and check the "Assembly Name" property, make note of it.
Create a new Class in the web applcation named Literal.cs
Put in code similar to the following for the class defenition:
namespace CustomControls
{
public class Literal : System.Web.UI.WebControls.Literal
{
}
}
Add the following register tag to the top of your aspx page
<%# Register assembly="WebApplication2" namespace="CustomControls" tagprefix="web" %>
If your assembly name was different then change it here. I noticed that when I did this in VB.Net the namespace was WebApplication1.CustomControls instead of just CustomControls like it was in C#, kind of odd.
Add the new control to your page:
<web:Literal ID="Literal1" runat="server" Text="test" />
Seperate Project Method
Create a new Empty Website (ASP.Net).
Add a new ASP.Net Server Control library named CustomControls to the solution.
Add a new Class to the new project called Literal (I'm using C# so my file is named Literal.cs). Below is my super basic code, that I believe should match the code described in the question.
namespace CustomControls
{
public class Literal : System.Web.UI.WebControls.Literal
{
}
}
Add a reference to the CustomControls project to your website.
Add the assembly registration to the top of your aspx page:
<%# Register assembly="CustomControls" namespace="CustomControls" tagprefix="web" %>
Add a new instance of the control to your page:
<web:Literal ID="Literal1" runat="server" Text="test" />
In App_Code Method
Create a new Empty Website (ASP.Net).
Add a new Class to the App_Code folder Literal2 (I'm using C# so my file is named Literal2.cs). Below is my super basic code, that I believe should match the code described in the question. I called it 2 so that you can use this in conjunction with the method described above without getting compile errors
namespace CustomControls
{
public class Literal2 : System.Web.UI.WebControls.Literal
{
}
}
Register the assembly/namespace for app_code in your aspx page by adding the following line to the top
<%# Register Namespace="CustomControls" Assembly="__code" tagprefix="web" %>
Add an instance of the new control to your page:
<web:Literal2 ID="literal2" runat="server" Text="test2" />
I tested this using visual studio and it all worked fine for me.
You can also register your control in web.config if you would like to use it on other pages
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="web" namespace="MyProject.CustomControls" assembly="MyProject" />
</controls>
</pages>
For web site this will work
<add tagPrefix ="web" namespace="ASP.App_Code.CustomControls" />
The problem lies in your <%# Register TagPrefix="web" Namespace="CustomControls" %> tag: it misses the assembly name!
It must become <%# Register TagPrefix="web" Namespace="CustomControls" Assembly="YourAssemblyName" %>. If you have difficulties finding the assembly name for your class here are a few hints:
If you created the class inside the web application's Visual Studio project, or in a separate DLL (shared library) project, then right-click the project and go to Properties to get the assembly name
If you created the .cs file in App_Code directory, ASP.App_Code should work
Otherwise please give us some information about project layout!
The easiest thing to do is to drag-and-drop the Literal.ascx on to the page (if the page is in the same solution) .If the page is another solution, you could add the control to VS toolbox and drag-and-drop the control on the page.
You forgot the src attribute.
<%# Register TagPrefix="web" Namespace="CustomControls" Src="PathToYourCustomControl" %>

VB webform in IronPython asp.net website

I tried to bring a previously done webform made in vb.net to an IronPython asp.net website with no luck. After seeing it didnt work, I tried to write the simplest codebehind vb.net webform to see if there was a problem with vb.net in an IronPython website and I got the following usual error
"be sure that the defined class in this file matchs with the one in the attribute inherits and that it extends the right base page (page or control)" (sorry if the translation isnt the most accurate I get that message in spanish)
but if I create a vb.net webform in the same website, with the sourcecode in the same file (with the vb.net code between script runat="server" tags in the same page) I get no problem.
Do I have to configure something for both kind of sourcecode languages to run in such way in the same IronPython website, like configuring something in the webconfig file or is there some compatibility issue for doing that which can't be resolved?
The code between <script /> tags is compiled dynamically when the page is first run. This enables you to mix languages. However, the classes in your code-behind files are statically compiled into an assembly by VS.NET ... and a VS.NET project can only support one language at a time.
One solution is to put your VB.NET code-behinds in a separate assembly. For example:
Add a new VB Class Library project to your existing solution
Add a reference to System.Web
Create your VB.NET code-behinds. They should be normal classes inheriting from System.Web.UI.Page.
In your ASP.NET website project, add a reference to the new project
Edit the # Page directives in your *.aspx files to inherit the classes in the new project
e.g. <%# Page Inherits="YourNewVBClassLibraryProject.MyVBCodeBehinds" ... /> where the Inherits attribute contains the relevant namespace-qualified class name
Thanks for the reply Serilla. Your information was interesting but I simply solved it by creating the app_folder and adding the vb files there. Do you think I could have some future problem for doing so?
The problem with the vb files was when these lines in the web.config were enabled for Ironpython to work
<pages compilationMode="Auto" pageParserFilterType="Microsoft.Web.Scripting.UI.NoCompileCodePageParserFilter" pageBaseType="Microsoft.Web.Scripting.UI.ScriptPage" userControlBaseType="Microsoft.Web.Scripting.UI.ScriptUserControl">
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
when I removed them, vb code behind files worked but ironpython didnt. When the lines were there, Ironpython code behind files worked but vb ones didnt

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