I'm trying to obtain a reference to the web.config customErrors section. When I use the following code I always get a null. I don't have this problem when I get a reference to a custom section that I've created so I'm a bit dumbfounded why this won't work.
CustomErrorsSection customErrorSection =
ConfigurationManager.GetSection("customErrors") as CustomErrorsSection;
I've also tried this:
CustomErrorsSection customErrorSection =
WebConfigurationManager.GetSection("customErrors") as CustomErrorsSection;
I've also tried this:
CustomErrorsSection customErrorSection =
WebConfigurationManager.GetWebApplicationSection("customErrors") as CustomErrorSection;
EDIT:
ARGH! Such is the case with most things I figured out the answer right after asking the question.
This works for me:
System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("/");
CustomErrorsSection customErrorsSection = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");
Or more simply like this:
CustomErrorsSection customErrors = (CustomErrorsSection) WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
This also works:
CustomErrorsSection customErrorsSection = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
So I guess I understand now why I had the problem in the first place. I had incorrectly thought that I could get a reference to the customErrors section by trying to GetSection("customErrors") but I had failed to tell it what root section it lived in and I was basing my attempts on the fact that I knew how to get a custom section when I failed to realize that my custom section was the root of the section so I did not have to prepend something like system.Web/ in front of it when I called GetSection().
Try this:
var configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
// Get the section.
CustomErrorsSection customErrors =
(CustomErrorsSection)configuration.GetSection("system.web/customErrors");
More on the subject here: CustomError Class
Related
I want to read the patched attributes that are in Sitecore.ItemWebApi.config to determine whether the Sitecore Item API is enable in my site. This value I'm looking for is itemwebapi.mode, and I want to see whether it's set to 'Off' or not.
<site name="mysite">
<patch:attribute name="itemwebapi.mode">StandardSecurity</patch:attribute>
<patch:attribute name="itemwebapi.access">ReadOnly</patch:attribute>
<patch:attribute name="itemwebapi.allowanonymousaccess">false</patch:attribute>
</site>
I tried getting ConfigurationManager.AppSettings["itemwebapi.mode"] but it returns null. How do I get this value?
I think you need to use sitecore configuration factory to read the configuration stuff.
(http://sitecore-community.github.io/docs/documentation/Sitecore%20Fundamentals/Sitecore%20Configuration%20Factory/#config_factory)
Try as below. If not working experiment in similar way.
var refObj = Sitecore.Configuration.Factory.CreateObject("site/patch:attribute", true) as itemwebapi.mode;
If not useful, put a comment. I will delete it.
You can access the attributes on the site node using the Properties accessor,
if the attribute is not set then the value will be empty. For the current Context site:
string mode = Sitecore.Context.Site.Properties["itemwebapi.mode"];
string access = Sitecore.Context.Site.Properties["itemwebapi.access"];
string anon = Sitecore.Context.Site.Properties["itemwebapi.allowanonymousaccess"];
If you are checking within an ItemWebApi request (beginning with -/item/) then you can check the ItemWebApi.Context:
if (Sitecore.ItemWebApi.Context.Current != null)
{
Mode mode = Sitecore.ItemWebApi.Context.Current.Settings.Mode;
AccessType access = Sitecore.ItemWebApi.Context.Current.Settings.Access;
bool anon = Sitecore.ItemWebApi.Context.Current.Settings.AnonymousAcessAllowed;
}
This will give you strongly typed access to the settings using enums. Unfortunately the overloaded constructor for RuntimeSettings() is marked as internal so it is not possible to new this up yourself in a normal web request but could check the code and do something similar if you need.
On IIS, I have a site on which I wish to edit the SslFlags.
I want to have these flags being set in the web.config at the site level instead of applicationHost.config.
I managed to have the UI of IIS to behave as expected by declaring the access section in the web.config, and allowing the override of the access section by editing applicationHost.config with the following element:
<section name="access" overrideModeDefault="Allow" />
Editing the SslFlags through the UI will edit the web.config file as expected. The section is not locked and the overridden value is considered.
However, when using the Microsoft.Web.Administration assembly to read and edit these flags by using the following code, the values which are considered are the ones of applicationHost.config, both when reading and editing.
In that first example, I used GetWebConfiguration to get the Configuration.
var serverManager = ServerManager.OpenRemote(serverName);
// Try with GetWebConfiguration
Configuration config = serverManager.GetWebConfiguration(sitename);
ConfigurationSection accessSection = config.GetSection(
"system.webServer/security/access",
sitename);
also, same applies if I retrieve the configuration with GetApplicationHostConfiguration:
config = serverManager.GetApplicationHostConfiguration();
accessSection = config.GetSection(
"system.webServer/security/access",
sitename);
I feel like I'm missing something obvious here, but I can't seem to access the values of the SslFlags in Web.config, how can I achieve that?
The first thing I would recommend is to only unlock sections for the specific web site or application that you want to allow overriding the values. For that you can do it quite easily using AppCmd.exe, for example:
C:\Windows\System32\inetsrv\appcmd.exe unlock config "Default Web Site/" /section:system.webServer/security/access -commit:apphost
Once you do that, then you can use the following code:
using(ServerManager serverManager = new ServerManager()) {
Configuration config = serverManager.GetWebConfiguration("Default Web Site");
ConfigurationSection accessSection = config.GetSection("system.webServer/security/access");
accessSection["sslFlags"] = #"SslRequireCert";
serverManager.CommitChanges();
}
I want to change the "system.net" section in web.config. I want to add or remove defaultProxy tag according to a variable in runtime.
<defaultProxy enabled="true" useDefaultCredentials="false">
<module type = "XXX.Utils.YYProxy, XXX" />
</defaultProxy>
I know, there are related posts editing web.config, but they all related with ConnectionStringsSection or AppSettingsSection. There are specific classes about them in System.Configuration package, but I did not find any class related with "system.net".
Do you know any quick way to handle this? Thanks in advance
I found a way to do this. I enable or disable the defaultProxy tag with following code:
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
NetSectionGroup netSection = (NetSectionGroup)config.GetSectionGroup("system.net");
if (string.IsNullOrEmpty(model.ProxyUrl))
netSection.DefaultProxy.Enabled = false;
else
netSection.DefaultProxy.Enabled = true;
The key point was casting the SectionGroup to NetSectionGroup class.
Instead of removing the defaultProxy element from web.config, I recommend that, in your code, you override the proxy that is used based on the value assigned to the variable to which you refer.
For example:
WebRequest request = WebRequest.Create("http://stackoverflow.com/");
if(variable == "some expected value to override default proxy") {
//by setting the Proxy property of the Request object to a new WebProxy class, you override the default
request.Proxy = new WebProxy("http://someproxyserver.com:80", true);
}
Of course, I'd need to see more of your code to give a more complete answer.
Hope this helps!
I'm accessing a config file thusly:
var map = new ConfigurationFileMap(configPath);
var config = ConfigurationManager.OpenMappedMachineConfiguration(map);
config.AppSettings.Settings.Add("SomeSetting", "SomeValue");
It works fine for any .exe.config file, but not for any web.config.
Note: I am not trying to access the web.config of the current application, I am attempting to modify the web.config in an arbitrary path.
(I've tried WebConfigurationManager instead of ConfigurationManager, but it gives identical results)
The exception is thrown by the AppSettings property accessor - trying to GetSection("appSettings") and cast it to an AppSettingsSection of course gievs the same exception. Either way, here it is:
System.InvalidCastException: Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'.
I have obviously searched around, but have found only people accessing the web.config for the 'current web app' or using XmlDocument/XDocument.
My guess is .exe.config files automatically get some configSections-type information inferred which means it correctly knows how to deal with appSettings. However I have no idea why, based on the filename, it wouldn't work with web.config.
Ah. For app.config I'm using OpenExeConfiguration:
// works fine for .exe.config
var config = ConfigurationManager.OpenExeConfiguration("some-assembly-here.exe");
config.AppSettings.Settings.Add("SomeSetting", "SomeValue");
config.Save();
Here I'm using OpenMappedMachineConfiguration which appears to be for machine.config, however I can't see another way of opening an arbitrary web.config file - anyone?
My mistake - I can use OpenMappedExeConfiguration just fine when opening web.config files:
var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = configPath;
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
Is it possible to check weather custom errors is turned on or off in the code on web application runtime.
I've figured out how to do it it's in...
HttpContext.Current.IsCustomErrorEnabled
You can use WebConfigurationManager.OpenWebConfiguration to obtain the configuration for the website, then use that to get the custom errors block:
Configuration configuration =
WebConfigurationManager.OpenWebConfiguration(null);
CustomErrorsSection customErrorsSection =
configuration.GetSection("system.web/customErrors") as CustomErrorsSection;
Response.Write(customErrorsSection.Mode.ToString());
OpenWebConfiguration(null) or HttpContext.Current.IsCustomErrorEnabled(true) gave me false information, this however worked great
Copy paste:
public static CustomErrorsMode GetRedirectMode()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
return ((CustomErrorsSection)config.GetSection("system.web/customErrors")).Mode;
}