I am having trouble doing config transform, adding app settings on nuget package install where element appSetting may or may not exist.
What I want to happen:
appSetting element does not exist
Insert appSetting element and its children
appSetting element exist
Insert children if missing
I only get one or the other to work, but not both occasions.
web.config.install.xdt
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="InsertIfMissing" >
<add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
<add key="Swagger.Contact.Email" value="some#email.address" xdt:Transform="InsertIfMissing" />
</appSettings>
</configuration>
Example 1
web.config BEFORE
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
</configuration>
appSettings element not present before transformation.
web.config AFTER
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Swagger.Contact.Name" value="Some Name" />
<add key="Swagger.Contact.Email" value="some#email.address" />
</appSettings>
</configuration>
Example 2
web.config BEFORE
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Other.Key" value="With Some Value" />
</appSettings>
</configuration>
appSettings element present before transformation.
web.config AFTER
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Other.Key" value="With Some Value" />
</appSettings>
</configuration>
Nothing happens in example 2 as the appSettings element already exist, I would like it to still evaluate its child elements and insert those if they do not exist, but it seems they are just ignored. Is there any other value for the attribute xdt:Transform I can use, or any other hacks to work around this issue?
I had a similar problem quite a while back. The workaround I applied was to have two entries with <appSettings> in the XDT file, one to check if its absent and if yes, then go ahead and insert it. The other was for the scenario when the <appSettings> element was already present. Here is short snippet to help you with your problem:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="InsertIfMissing">
</appSettings>
<appSettings>
<add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
<add key="Swagger.Contact.Email" value="some#email.address" xdt:Transform="InsertIfMissing" />
</appSettings>
</configuration>
Let me know if this works for you.
Related
I have this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="ContentServer" value="true" />
</appSettings>
<connectionStrings />
<system.web>
<trace enabled="false" requestLimit="25" pageOutput="false" traceMode="SortByTime" localOnly="false" />
<customErrors mode="Off" />
<compilation debug="false" />
<authentication mode="Windows" />
</system.web>
</configuration>
How do I set the encoding to utf-8 for files sent to client. Actually There can be other types of files sent to clients like js,css,images. But I have aspx pages, that I want to set text/html; charset=utf-8" content-type to. How can I add this to this web.config code?
Thanks
You need to set the responseEncoding attribute of the globalization element.
<globalization
responseEncoding="utf-8"
/>
In context:
<configuration>
<system.web>
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
</system.web>
</configuration>
I have read that the applicationhost.config file sets the defaults and all directories below root inherit those settings.
I know its possible to change the defaultdirectory in a web.config, but I can't seem to do the same with a virtualdirectory.
Any pointers, help is appreciated.
Note: WEB.CONFIG file below DOES NOT work... just there to explain what I want to.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" existingResponse="Auto"/>
<asp scriptErrorSentToBrowser="true"/>
<directoryBrowse enabled="false" />
<defaultDocument>
<files>
<clear />
<add value="Default.aspx" />
<add value="default.html" />
</files>
</defaultDocument>
<httpProtocol>
<customHeaders>
<clear />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.applicationHost>
<sites>
<site name="wed" id="1" serverAutoStart="true">
<application path="/alpha-beta">
<virtualDirectory path="/" physicalPath="\url.com\wwwroot\alpha\beta" />
</application>
</site>
<applicationDefaults applicationPool="DefaultAppPool" />
<virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>
</system.applicationHost>
</configuration>
I have a web.config file that links to a common.config file. common.config is being used by multiple applications. I used the aspnet_regiis.exe, but that only encrypts the web.config file. How can i encrypt the common.config file?
web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="C:\Users\naem\Documents\common.config" />
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
common.config file:
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="myKey" value="This is the Value!!!!"/>
</appSettings>
I don't have the rep to comment on the original post, but this looks like a duplicate of How to encrypt .config file Asp.net
I found workaround to solve this issue. Hope it helps.
Rename your common.config to web.config temporarily.
Add configuration as root element to this file. So your common.config will look like as follows.
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="myKey" value="This is the Value!!!!"/>
</appSettings>
</configuration>
Run encrypt command.
aspnet_regiis.exe -pef appSettings "C:\Users\naem\Documents"
Open encrypted file and remove Configuration tags.
Rename file to Common.config
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension =".wav"/>
<mimeMap fileExtension=".wav" mimeType="audio/wav" />
</staticContent>
</system.webServer>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
i try this code but not worked. Help me to solve it.
thanks
I'm using URLRewriter, but there is a problem, it re-write all addresses, even for image URLs, for example, it shows a webpage for http://localhost/images/logo.png.
It's my web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
</httpModules>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<rewriter>
<rewrite url="~/pages/(.+)" to="~/default.aspx?pn=$1" />
</rewriter>
</configuration>
I want it re-write only .aspx files.
The url attribute in the rewrite element is a .Net regular expression, at the moment the expression you have matches anything under ~/pages/. If you only want to match .aspx files the simplest thing to do is change it to:
<rewrite url="~/pages/(.+\.aspx)" to="~/default.aspx?pn=$1" />