Special characters in web.config - asp.net

I've create a custom configuration section in my web.config file for my 3.5 web application. The custom configuration contains special characters listed below.
<add Key="2" String="â€"/>
<add Key="3" String="148"/>
<add Key="4" String="!X"/>
<add Key="5" String="¡§"/>
<add Key="6" String="¡¦"/>
<add Key="7" String="¡¨"/>
<add Key="8" String="’" />
<add Key="9" String="–" />
I currently have the xml type of the web.config defined as below.
<?xml version="1.0"?>
This works fine in one development environment, but when I migrate the application to another environment I get an xml parsing error on the string for Key 2. When I replace all the string definitions for each config entry with standard alpha chars, the application works fine. Is there a way to enforce the XML to be read the same way? I have looked into the encoding attribute for the xml definition tag, just not positive what to set it to. Any guidance would be well appreciated. Thanks in advance.

I recommend setting the encoding to "utf-8". Make sure the file is also actually saved as utf-8.

Are you typing these into your web.config file manually? Can't you just HTML-encode your string values?

Related

How to keep private data separate in an open source ASP.NET MVC application

In an open source ASP.NET application I'm working on, I need to keep certain data in the configuration file private, while still keeping it easy for people to build and debug it on their own machine. This is data such as API keys, Mail Settings, etc..
How would I keep this data separate and out of the git repository while still allowing people to just pull and build without having to set up a bunch of stuff?
In your config file you can define configSource:
<configuration>
<appSettings configSource="filepath1.config" />
<connectionStrings configSource="filepath2.config" />
<!--etc-->
</configuration>
Put the configurations that you need to keep private in a separate config file, then exclude them in your .gitignore.
Keep in mind that this will ignore the whole section and overwrite it with the context you have in the referenced file.
You can also do Configuration Transform, which allows you to only overwrite a small set of variables in sections. For example:
In your main Web.config:
<configuration>
<appSettings>
<add key="Key1" value="Something I dont't Care"/>
<add key="Key2" value="Something dummy"/>
</appSettings>
</configuration>
And in your Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Key2" value="Something I want to keep secret"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
In this case the "Key2" value that you want to keep private will be in a separate file, and you can exclude the Web.Release.config through .gitignore.
Also there's another approach that I never tried, which can also overwrite config using external file.

ASP.NET URL Rewriting in very easy way

I want to know how to make SEF URL for web pages. After a little googling I have found many techniques, but all of them are so complicated. I do not need them. Is there any way to make SEF URL which is very easy to implement?
First download this reference .
Then import it to your web site.
under <configuration> node type this:
<configSections>
<section name="urlrewritingnet" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter"/>
</configSections>
The next step is typing the following code under <system.web> node:
the next one is putting the following line just before </configuration> tag:
<urlrewritingnet configSource="RewriteRules.config"/>
We are almost done!
Create a new .config file which matches the tag's configResource parameter. I preferred "RewriteRules.config".
Then it is time to create rewriting rules on RewriteRules.config file:
<urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" defaultPage="Default.aspx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
<rewrites>
<add name="DetailPageRule" virtualUrl="^~/VirtualDetailPageName/(.*)/(.*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/RealDetailPage.aspx?param1=$1&param2=$2" ignoreCase=" true"/>
</rewrites>
</urlrewritingnet>
If you have more or less parameters you can change the count of (.)'s. Here there are only two parameters (param1 and param2), so there are only two (.) strings.
Final Step is giving links in according to the rules you have created.
It is simple and fast. But I do not know if there are drawbacks or any security issues.

web.config in subdirectory in mvc 4

This question seems to have been asked quite a bit but none actually solve the problem for me. I have an ASP.NET MVC4 application with an area. Inside this area I have a web.config with overridden appsettings. These settings don't seem to be picked up when attempting to read them in a controller within the area.
-MyApp
-Areas
-MyArea
-Controllers
-MyController
-Web.config
-Web.config
In MyApp\Web.config I have:
<appSettings>
<add key="DefaultDisplayMode" value="Configuration" />
</appSettings>
In MyApp\Areas\MyArea\Web.config I have:
<appSettings>
<add key="DefaultDisplayMode" value="Review" />
<add key="SubSetting" value="Testing" />
</appSettings>
In MyController if I query the app settings, I see:
DefaultDisplayMode: Configuration
I expect to see:
DefaultDisplayMode: Review
SubSetting: Testing
I've tried with both:
System.Configuration.ConfigurationManager.AppSettings["..."]
System.Web.Configuration.WebConfigurationManager.AppSettings["..."]
I even tried with the following (over the top) structure with no luck:
-MyApp
-Areas
-MyArea
-Controllers
-MyController
-Web.config
-Web.config
-Web.config
Does anybody know why my subdirectory appSettings aren't working?
IIS will load the web.config based on the URL, not based on the code that your routed request executes.
You can create an empty folder structure that mimics the required route and add the config file there. If that does not work, you can use <location> tag in the main configuration file - but the path in the location tag also has to match the URL and not the code.

Specify the active connection string to use in Web.Config

Is there a quick and easy way to set a connection string in Web.Config to be the active connection string?
I basically want to name my connection strings appropriately and then set one as active without having to switch out the names or re-compile my application.
Something like this:
<add name="Current" connectionString="{Local}"/>
<add name="Local" connectionString=[...]" />
<add name="RemoteOnMyServer" connectionString=[...]" />
<add name="RemoteAzure" connectionString=[...]" />
I don't think that is possible the way you asked. But you can move the connection strings block to a separate file and then control which FILE is the active one:
<connectionStrings configSource="LocalDb.config"/>
Then you can have separate config files:
LocalDb.config
RemoteOnMyServer.config
RemoteAzure.config
<etc>
Each one of there would hold something like this:
<?xml version="1.0"?>
<connectionStrings>
<add name="namedConnectionString" connectionString="Data Source=..." providerName="..." />
</connectionStrings>
Swithcing between them then becomes a matter of changing the configSource on the <connectionStrings /> element.
Scott Hanselman has a good article describing using different configurations for different environments using the build setting in the compiler. I've used this with great success in some of my projects.
Have a look

Reading config file without Configuration section

Little embarrassed to ask, but since I never had to do it, here it goes. I have a config file that has connection strings for the app stored in this format.
<connectionStrings>
<clear />
<add name="MyConnectionStringName" connectionString="{here is a connection string}"/>
</connectionStrings>
I tried ConfigurationManager but it requires that the file has "configuration" root element. Is there something else in Configuration name space, or should I just use LinqToXml?
Update: I cannot change anything in the file, file is already in the use for years by some in home grown framework. All content that is in the file is shown above.
Update2: Second look reveals that this file is referenced from App.Config as
<connectionStrings configSource="Config\connectionstrings.config"/>
which allows me to use ConfigurationManager.ConnectionStrings.
There is more to a configuration file than just the connectionStrings section. If you are using Visual Studio, the easy way to fix your problem is to use the IDE to add a new Application Configuration file. The IDE will create a shell file for you, which you can then add your connectionStrings section to (within the configuration section).
By hand, at bare minimum, you need this:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<clear />
<add name="MyConnectionStringName" connectionString="{connection string}"/>
</connectionStrings>
</configuration>
What's wrong in
<Configuration>
<appSettings>
<add key="MyConnectionStringName" value="{here is a connection string}"/>
</appSettings>
</Configuration>
Well you could load it into an XmlDocument and the Load method.
Once you have it loaded you can use SelectNodes with an xpath query to get back the nodes you want.
Off the top of my head something like
string conn;
XmlDocument xdoc = new XmlDocument();
xdoc.Load("path_to_file");
var configs = xdoc.SelectNodes("ConnectionStrings/Add");
foreach(XmlNode n in configs)
{
conn = n.Attributes["connectionString"];
}
Try that and see. you may need to play around with the xpath, been a while since i wrote one.

Resources