Nuget Package - Web.config.transform Add - asp.net

I'm creating a NuGet package and want it to update the web projects Web.Config file with certain settings. I am using the web.config.transform to edit the web.config file of an application. It's working well when I simply add appSettings - like so:
<configuration>
<appSettings>
<add key="WebPToolFolder" value ="~/Tools"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images"/>
</appSettings>
</configuration>
However, if I try an add to the staticContent it doesn't seem to alter the tags. For example, here is the web.config.transform file:
<configuration>
<appSettings>
<add key="WebPToolFolder" value ="~/Tools"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images"/>
</appSettings>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" />
</staticContent>
</system.webServer>
</configuration>
It updates the appSettings, but not the staticContent tags - any ideas?

Old question but if anyone lands on it the following should work:
In your case to add/update the staticContent element:
It's an alternative solution, so you won't use the .transform file, but rather the web.config.install.xdt (and web.config.uninstall.xdt) which I find better:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!-- some other elements -->
<staticContent xdt:Transform="InsertIfMissing">
<mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Locator="Match(fileExtension)" xdt:Transform="InsertIfMissing" />
</staticContent>
<!-- some other elements -->
</configuration>
This way you don't need to do any pre-update preperations, just upgrade the package.
Check this post for XDT support from Nuget 2.6 onwards.

You need to put an empty <staticContent></staticContent> in your web.config and then use the xdt:Transform="Insert" on the element like this:
Your web.config:
<configuration>
<appSettings>
<add key="WebPToolFolder" value ="~/Tools"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images"/>
</appSettings>
<system.webServer>
<staticContent>
</staticContent>
<system.webServer>
</configuration>
And then you can insert a value in your transform file like this:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Insert"/>
</staticContent>
</system.webServer>
Took me a while to find out. Hope this helps.

Have you tried adding an xdt:Transform="Replace" attribute to the tags you want to update?
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="WebPToolFolder" value ="~/Tools" xdt:Transform="Replace"/>
<add key="ImagesFolder" value ="~/Content/themes/base/images" xdt:Transform="Replace"/>
</appSettings>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Replace"/>
</staticContent>
</system.webServer>
</configuration>
There's some good Microsoft documentation here
If you post the initial markup and what you want it to look like maybe we could help a bit more :)

Related

Cannot get ASP.NET/IIS to serve .js files with custom cache-control settings

In my ASP.NET MVC 5 app, I have used the clientCache web.config attribute to customize the caching behavior for static files.
<system.webServer>
<staticContent>
<clientCache cacheControlCustom="private,max-age-300" setEtag="true" />
</staticContent>
</system.webServer>
This is working fine for .css and image files, but I'm noticing in my browser's dev tools that .js files are not getting the custom cache-control and etag headers that the other file types are getting.
In addition, I've tried adding a custom handler, but it hasn't had any effect from what I can tell.
<handlers>
<add name="StaticHandler_js" verb="*" path="*.js" type="System.Web.StaticFileHandler" />
</handlers>
Any ideas on how I can get ASP.NET/IIS to treat .js files the same way as other static files?
I was able to get around this by adding path-specific configuration to clear out all handlers and add only the static file defaults to those paths. This is an imperfect solution because it's based on the file path, not the file type, but because all of my JavaScript files are in this singular folder, it does the job.
<location path="Scripts">
<system.webServer>
<handlers>
<clear />
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read"/>
</handlers>
</system.webServer>
</location>
If anyone has a better solution that doesn't rely on file path, I'll gladly accept that as the solution.
<system.webServer>
<staticContent>
<clear/>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:01:00" />
<mimeMap fileExtension=".jpg" mimeType="image/jpg"/>
<mimeMap fileExtension=".png" mimeType="image/jpg"/>
<mimeMap fileExtension=".css" mimeType="text/css"/>
<mimeMap fileExtension=".js" mimeType="text/javascript"/>
</staticContent>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>

How to use XDT to find an element in web.config then change it for nuget deploy

I need to deploy my work using nuget and to change the web.config in the process.
I used XDT to add the following code:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="My.Module" />
</modules>
</system.webServer>
I wrote a simple XDT web.config.install.xdt which looks like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer xdt:Transform="InsertIfMissing">
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
<system.webServer>
<modules>
<add name="MyModule" type="My.Module" xdt:Transform="InsertIfMissing" />
</modules>
</system.webServer>
</configuration>
And this works great. Until I met a system that puts their module under location instead of under configuration, like this:
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="My.Module"/>
</modules>
....
So in this case, my XDT doesn't find the path and creates a new element towards the end of the file, which kills the site.
How do I search for whether system.webServer exists anywhere in the file and add my code there?
After searching for a LONG time, I finally found some code online that resolved this for me.
I am posting it here in case anyone will ever look for something similar.
First, Kevin.Wu's original code: http://git.crmclick.com:8888/kevin.wu/qa/blob/1c554bd0867de42ba360eb546d74e86ebf64af7b/packages/Microsoft.ApplicationInsights.Web.2.0.0/content/net45/web.config.install.xdt
The modified code that does what I need:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer xdt:Transform="InsertIfMissing">
</system.webServer>
<system.webServer xdt:Locator="XPath(//system.webServer[(count(parent::location) = 0) or (count(parent::location[#path != '.' and count(#path) != 0]) = 0)])">
<validation validateIntegratedModeConfiguration="false" xdt:Transform="InsertIfMissing" />
</system.webServer>
<system.webServer xdt:Locator="XPath(//system.webServer[(count(parent::location) = 0) or (count(parent::location[#path != '.' and count(#path) != 0]) = 0)])">
<modules xdt:Transform="InsertIfMissing">
<add name="MyModule" type="My.Module" preCondition="managedHandler" xdt:Transform="InsertIfMissing" xdt:Locator="Match(type)"/>
</modules>
</system.webServer>
</configuration>

Sections must only appear once - appsettings

I'm a bit confused as to how I can specify another .config file in my web.config while retaining parts of the original web config. I want to put my connection strings in another file but when I build the project I get an error about there being multiple appsettings elements.
I have this:
<appSettings file="ConnectionStrings.config">
</appSettings>
then further down, because it's a Crystal Reports application, these settings are specified. I don't want these keys in my connnectionstrings.config file as they're not relevant.
<appSettings>
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>
How do I keep my seperate config file and the Crystal settings above, without putting them all in the connectionstrings.config file?
You main configuration file(web.config) should look like this
<?xml version="1.0"?>
<configuration>
<!--other sections-->
<appSettings file="appSettings.config">
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>
<!--other sections-->
</configuration>
Further your separate appSettings.config should look like this
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="YourConnectionStringValue" value="" />
</appSettings>
This is how we have worked in our project.
Try this, maybe!
<appSettings file="ConnectionStrings.config">
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>

allow azure asp.net website to serve x3d files

I want to serve x3d files by my azure asp.net website. I tried in web.config:
<system.webServer>
<security>
<requestFiltering>
<fileExtensions applyToWebDAV="false">
<add fileExtension=".x3d" allowed="true" />
<add fileExtension=".bin" allowed="true" />
</fileExtensions>
<requestLimits maxAllowedContentLength="1048576000" />
</requestFiltering>
</security>
</system.webServer>
What is wrong?
You need to edit the web.config and add those file extensions as mime types.
Please see the following artcile: http://blogs.iis.net/richma/adding-mime-types-to-your-windows-azure-web-site
The following example is for .woff font files:
Add the following to the section of your web.Config.
<staticContent>
<mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
</staticContent>
Note : If the element already exists you just need to add the element to this section for the type you want to add

Moving <httpredirect> out of web.config in separate config file

We have many (more than 100) redirects in our web.config like
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="/a" destination="/a/dfdf/default.htm" />
<add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
<add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
<add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
<add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
..... more than 100
</httpRedirect>
</system.webServer>
</configuration>
Is there way we can have this section managed in separate web.config or any other best solution?
You can move some config elements into their own config file to reduce clutter in the web.config.
<configuration>
<system.webServer>
<httpRedirect configSource="httpRedirects.config" />
</system.webServer>
</configuration>
This is achieved by adding the configSource attribute as shown above.
And in your seperate httpRedirects.config
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="/a" destination="/a/dfdf/default.htm" />
<add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
<add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
<add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
<add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
</httpRedirect>
Note I have only tried this with other config elements.
You can store that in Separate Config file as shown here: SectionInformation.ConfigSource Property
In order to avoid cluttering the configuration file - web.config - it can be defined in a separate configuration file. That file can then be referenced from the web.config file as below:
<httpRedirect configSource="httpRedirects.config" />
The configSource attribute tells IIS configuration that the <httpRedirect> section is defined in a separate file httpRedirects.config.
EDIT:
Please make sure you have httpRedirect attribute set to enabled=true as the default value is false.
<httpRedirect enabled="true" configSource="httpRedirects.config" />

Resources