More than one App_Code Folder in asp.net project - c#

I have a asp.net website and i want to test it on a domain.
The problem is I have already a site hosted on it and i cannot change it.
So I tried to create a sub-directory and uploaded my project in it.
The problem here arises with the web.config file as i want the inner project to link to another some other database.
Also the app_code folder is different for the inner project.
I thought of creating a sub-domain, but cannot do so, as i have no permissions.
Please help with some solution.

You can create app_code and web.config file in every folder. There is no issue in doing that.
But for web.config file there are some restriction about tag used in sub folder web.cofig file. A useful link
http://weblogs.asp.net/jongalloway//10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides

It sounds like you need to clear your connection strings on the inner project web.config and add the new connection string.
In the "inner" project, add <clear /> inside the <connectionStrings> node and then add your connection string in with test db connection string after they are cleared. This way it will not pick up the outer (live) site web.config connection string.
Warning - testing on a live site can always be tricky. Be careful!

Related

Configuring multiple web.config files

At the moment I have a solution with multiple projects, 2 of those are wcf services with their own web.config files. Everything worked well but stopped working at existing code after I added some new values to one of the web.config files. After some research I found out the service did not refer to his old web.config (his own) but to the other one. My solution to the problem: I add the key settings to the other webconfig aswell so they both have the same appsettings. This worked but as you might understand i am not satisfied with this problem. There is no certainty it will magically switch from file again and i need to make sure both files equals all the time.
Is it possible i use one web.config file or 1 appSettings block for 2 different projects? They both use the same values
Maybe pre-build action and copy web.config from shared folder in to all projects will be workable for you.
Something like this: https://stackoverflow.com/a/54925165/3434393

Trouble accessing folder path from web config file

I was trying to use the solution found in this post:
How to access the folder path in web config using c#
but I get a null reference exception (System.Collections.Specialized.NameValueCollection.this[string].get returned null)?
I have this in my web config file (it's inside the configuration like shown in the post linked above):
<appSettings>
<add key="SessionTest" value="C:\\Settings\\XmlDir\\Session\\20180824.xml"/>
</appSettings>
and retrieve it like so:
string path = System.Web.Configuration.WebConfigurationManager.AppSettings["SessionTest"].ToString();
Is there something simple that I'm missing here? The file exists in the folder (and I copied the path from the file explorer, so I'm having trouble understanding what is causing the null exception >.<")
Thanks in advance :)
As far as I'm aware this is the standard way to access web.config <appSettings>
System.Configuration.ConfigurationManager.AppSettings["SessionTest"]
There was a second web config file that I wasn't aware of (I'm working on an old project so I didn't realize the first team that wrote this had added one to the folder I was working in, which in hindsight is probably a little strange)
When I added it to the web config file that is associated with the entire solution and not the one inside the folder I was working in, I was able to retrieve the value successfully!
I want to also note I included SLaks suggestion from the comment he made (which is not to say it doesn't work with the double slashes, but I didn't include them when I found success so I can't confirm that).

Asssembly with EF context class connection string in an assembly

Hope you can help with this one, although so for here does seem to b a resolution.
I have a class library which connects to a SQL Server database using an EF context class with a connection string in the app.config file.
I then reference this dll in an ASP.NET app but have to add the connection sting again in the web.config file
I would have though by referencing the dll and thus the connection string, I would not require to add the connection string again.
Perhaps I am missing something or has anyone else found a resolution to this ?
The connection string is read from the executables app.config (i.e. windows forms/WPF/console applications). For web applications the connection string is taken from the web.config file of the startup project (i.e. where the global.aspx.cs) is located.
You cannot use the app.config from your model DLL directly. You have to put the connection strings in the "main" projects.
What might help is some kind of scripting, but that's up to you.
You could try to use the configSource attribute and configure and external file:
<ConnectionString configSource="shared.config" />
The content of the file replaces the content of the element though. Another thing is that you need to include the file in your web app project and toggle copy to output.
And another warning:
the external connection strings file must be in the same directory as
the root web.config file, so you'll have to take precautions to ensure
you don't check it into your source repository.
More about that and best practices about deploying sensitive data can be found here:
http://www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure#con

ASP.Net - use seperate configuration file for database and app settings

I have an asp.net project that is using Entity Framework that is used on several different client servers. This means a different web.config for each for connectionstrings and app settings.
Hasn't been an issue but I changed something that altered the web.config file recently and I manually had to adjust this for each client, I also have to exclude the web.config file in updates to ensure their own one is not overwritten.
What I would like to achieve is store these settings in maybe another config file that the project can pick up and use. Maybe that on Globals Application_Start gets these and imports them/overwrites the current web.config file perhaps.
Essentially I don't want to affect the current code that uses the connection string and ConfigurationManager.AppSettings used throughout the project but I do want to be able to let the web.config file update for each client and use a seperate file for some settings.
NOTE: I do not have access to publish directly to each server so I can't simply write a different deploy web.config for each one. I have to publish the files locally, store as zip and automated routine on servers downloads and extracts accordingly.
EDIT:
Please do say if this is considered a bad idea but an idea I had was to put something similar in Global.asax Application_Start method:
Does config file exist?
If no, create file and append all current settings in web.config
If yes, open and import those settings to the current web.config overwriting the original values
Hopefully then in a few weeks time, after I have asked all clients to perform a manual update they will have this code and I can begin to include the web.config in updates.
In VS, inside the Build menu, the last item is Configuration Manager.
In here you can specify various different release environments which can each have their own web.config transforms.
This is normally used for production/staging/test environments. However, I can see no reason why you could not use this and have a configuration file for each of your servers/environments.
You will then need to create the transformations for each environment, by rightclicking the web.config, then selecting Add Config Transform.
each of the environments you had setup can then override the settings in the main web.config. (That now acts as a template/default settings)
e.g.
In Web.EnvironmentA.Config
<add key="ConnectionString" value="ConStringA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
In Web.EnvironmentB.Config
<add key="ConnectionString" value="ConStringB" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
Then when you do a publish, you simply setup which config file to use. If you use Azure or the VS publish method, it will save each of these settings, so then you can simply push to the relevant environment easily.
Just make sure you test this works as you intent first, before you start publishing everywhere ;)

ConfigurationManager.ConnectionStrings["XXX"] is returning null

I have a console application in which website project is added as dll. Inside dll ConfigurationManager.ConnectionStrings["XXX"] is called which is always returning null.
But web.config in dll has connectionstring named 'XXX'. Can any one suggest what is going wrong?
I don't think .config file of the .dll is added to your console project together with the dll. You need to have the .config in your executing project.
Try copying the config file to your console project. Or at least the connection strings section. Probably in an App.config.
I had a slightly different problem that I haven't seen an answer to. I was using.
ConfigurationManager.ConnectionStrings["XXXX"].ConnectionString;
When I opened up web.config I saw that the connection string I had created was prefixed with "WebApplicationAPI.Properties.Settings." - putting the entire string in worked for me:
ConfigurationManager.ConnectionStrings["WebApplicationAPI.Properties.Settings.XXXX"].ConnectionString;
Your config file is not in the .dll. So you're referencing nothing I would suspect! Unless you've manually copied the web.config file to the correct location or references it via an absolute path?
web.config is for web application.
For console application you have to use app.config.
Well, actually that's the default configuration for C#, I won't be surprised some advanced user could make it go the other way arround.

Categories

Resources