NuGet Package transform a config transform file - nuget

Is there a way to make a NuGet package transform a config transform file? For example, when I want my NuGet package to edit a web.config file, I create a web.config.install.xdt file. But what if I want my NuGet package to edit a web.config.debug file?
I tried making a web.config.debug.install.xdt file, but ran into one issue: I cannnot get the transformation to insert attributes that are themselves attributes of xdt transformation. Something like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt1="http://schemas.microsoft.com/XML-Document-Transform">
<system.serviceModel >
<client xdt1:Transform="Insert">
<endpoint address="http://blah.blah" binding="basicHttpBinding" contract="Test.Contract"
name="TestWs" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</client>
</system.serviceModel>
</configuration>
(I tried changing the namespace of xdt, but that didn't help either.)

Although this isn't perhaps the best answer, it did get the job done for me when I found myself in this situation:
Use the "old" method of doing transforms, not the xdt way.
https://docs.nuget.org/create/Transforming-Configuration-Files-Using-dotTransform-Files.md
This seems to work well, just make sure the appropriate xmlns attribute is in the .transform file.
For example, if you wanted to transform your web.qa.config file that currently looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Tier" value="qa" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
You could add an element:
<add key="RedirectUri" value="yourRedirectUriForQA" xdt:Transform="Replace" />
By adding the following web.qa.config.transform file to your Nuget package:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="RedirectUri" value="yourRedirectUriForQA" xdt:Transform="Replace" />
</appSettings>
</configuration>
Just make sure also to add it to the .nuspec file so it gets picked up when packaged.

Related

IIS 10 - web.config - how to enable default document without script access

We have a folder which contains only static html and images etc. No scripts should be allowed to execute from within this folder. However we would still like to be able to use html default documents.
What is the correct way to configure this?
This is the web.config file...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read"/>
<defaultDocument enabled="true">
<files>
<clear />
<add value="default.html" />
<add value="default.htm" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
If I attempt to access http://mysite/mystaticfolder/ it fails with the error...
HTTP Error 403.1 - Forbidden
However the URL http://mysite/mystaticfolder/default.html works fine.
Surely it shouldn't be nescessary to allow dynamic scripts, just to be able to serve static html default documents?
In case it helps anyone, I've been able to solve it with the following...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read">
<clear/>
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule" resourceType="Either" requireAccess="Read" />
</handlers>
<defaultDocument enabled="true">
<files>
<clear />
<add value="default.html" />
<add value="default.htm" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
I'm not entirely sure though why this doesn't work by default though.

Configure package source of nuget package manager in Visual Studio Code?

I would like to change the package source of nuget package manager in visual studio code. Please guide me to change the package source. Any help is appreciated.
You need to add nuget.config file in to the root of your project and specify the package source in there like this.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="sample" value="https://www.sample.org/index.json" />
</packageSources>
</configuration>
For password protected package sources :
<packageSourceCredentials>
<sample> <!--package src name-->
<add key="Username" value="something" />
<add key="ClearTextPassword" value="thepassword" />
</sample>
</packageSourceCredentials>

Nuget ignores credentials for network share

I am using network windows share as nuget packages source. And everything works fine except our admin wants to set some authentification on this share.
Problem: configure nuget to use share with user/pass.
What I've tried: changing my nuget.config, adding packageSourceCredentials for this share. Here is my changed nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageRestore>
<add key="enabled" value="true" />
</packageRestore>
<packageSources>
<add key="Custom" value="\\some\share\nuget\" />
<add key="NuGet" value="https://nuget.org/api/v2/" />
</packageSources>
<packageSourceCredentials>
<Custom>
<add key="Username" value="test" />
<add key="Password" value="somelongencodedvalue" />
</Custom>
</packageSourceCredentials>
</configuration>
But nuget tries to login using currently logged in windows user and is not using credentials from config. What am I doing wrong? Does packageSourceCredentials work only with http package sources?
You make nothing wrong.
I got today same problem. I found out that nuget first tries to authenficate with the current credentials of the user. In case this failed, the credentials of the config file are will be taken.

Deploying from WebMatrix for AppHarbor

I am trying to deploy to AppHarbor a website. Of course, i had to disable precompilation and other stuff, but appharbor builds it fine and deploys it to http://nitelists.apphb.com/.
However if you enter, you see a forbiden acess error, don't know why.
(note that i am using the SQL lite database that comes integrated with WebMatrix)
Do i have to change something of the web.config file to make it work? This are the actual file contents:
<?xml version="1.0" encoding="UTF-8"?>
<configuration >
</configuration>
My web.config file looks like this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>

How do you set a relative path for a file-based package source with NuGet?

I'm using NuGet 2.1 with multiple solutions, each in a child directory under a single parent directory and a single packages directory which is shared by all the solutions (this became possible with NuGet 2.1).
I'd like to add a file-based package source which points to the packages directory in my working copy (I'm using Subversion).
This works:
<configuration>
<repositoryPath>_Packages</repositoryPath>
<activePackageSource>
<add key="Working copy package source" value="C:\AllMySolutions\_Packages" />
</activePackageSource>
</configuration>
This doesn't:
<configuration>
<repositoryPath>_Packages</repositoryPath>
<activePackageSource>
<add key="Working copy package source" value="_Packages" />
</activePackageSource>
</configuration>
I don't want to hard-code the absolute path but can't find a way of using a relative path in the value attribute inside activePackageSource.
It looks like this is now supported.
http://nuget.codeplex.com/workitem/2810
Put a file called nuget.config in the root of your solution (next to packages folder and solution file) containing:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="Local" value="packages-local" />
</packageSources>
</configuration>
Build you packages to the packages-local folder.
The packages in this folder will be available to add to other projects in the solution. (You may need to restart VS or at least close-reopen your solution for the config to be picked up).
I had same problem with <repositoryPath>. I needed to force all SOLUTIONs in my git repository to use same folder to store their NuGet packages. So I created a nuget.config file on the root of the repository with the following contents, as it was described in documentations:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="NugetPackages\" />
</config>
</configuration>
After manually creating NugetPackages beside nuget.config and installing packages, there was nothing in that directory, but everything was working fine. Then I found that VS has created C:\NugetPackages and moved packages there. After trial and error for a while, at last the relative path using .\ worked for me:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value=".\NugetPackages\" />
</config>
</configuration>
I hope it behaves the same for other configurations as well.
P.S.: As Matthew already said, I had to restart VS to make my edits in nuget.config take effect.
For .NETCore/.NET5 and newer, the config key repositoryPath doesn't work anymore, and it is now named globalPackagesFolder, so:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages\" />
</config>
</configuration>