NuGet.Server
是一个包,可用于使一个ASP.NET应用host一个package feed 。
- 使用VS创建一个新的空WEB应用,添加
Nuget.Server
包。 - 配置应用的Packages文件夹,添加包。
- 部署应用到服务器上。
创建部署Nuget.Server ASP.NET应用
- 在VS中选择File > New > Project, 搜索"ASP.NET",选择ASP.NET Web Application (.NET Framework),设置Framework为".NET Framework 4.6":
- 给应用起一个合适的名字,点击OK,在下面的对话框中选择空白模板,点击OK。
- 右键项目,选择Manage NuGet Packages。
- 在包管理器界面中,选择Browse选项,搜索安装最新版本的Nuget.Server包。 (还可在包控制台使用
Install-Pacakge Nuget.Server
安装Nuget.Server包。) - 安装Nuget.Server会将空白的web应用转换成了一个package source。它安装了一起其他的包,在web应用的根目录创建了
Packages
文件夹,修改了web.config加上了一些配置。
安装完Nuget.Server后,我们要检查下web.config文件。Nuget.Server可能没有覆盖已经存在于web.config中的元素,而是重复的创建了一个。这可能导致服务器内部错误"Internal Server Error" 。例如如果在安装Nuget.Server之前,你的web.config已经包含了
<compilation debug="true" targetFramework="4.5.2" />
,那么安装完Nuget.Server后,会多出一个<compilation debug="true" targetFramework="4.6" />
。这时,你需要删除老版本的。
- 将要加入的包的 xxx.nupkg文件放到Packages文件夹下面,并将这些.nupkg文件的Build Action设为Content,将Copy to Output Directory设为Copy always:
- 按Ctrl+F5启动网站。首页上会提供package feed URL。如果出现错误,检查web.config去除重复元素。
- 点击上图中的here超级链接,可查看已安装的包。
- The first time you run the application, NuGet.Server restructures the Packages folder to contain a folder for each package. This matches the local storage layout introduced with NuGet 3.3 to improve performance. When adding more packages, continue to follow this structure.
- 一旦本地部署成功了,可以将应用发布到内部或者外部的服务器上。
- 一旦部署到了
http://<domain>
,对应的package source的url为http://<domain>/nuget
配置Packages文件夹
在Nuget.Server 1.5 之后,可以在web.config中的 appSetting/packagesPath
来指定package文件夹:
<appSettings>
<!-- Set the value here to specify your custom packages folder. -->
<add key="packagesPath" value="C:MyPackages" />
</appSettings>
packagesPath
可以是绝对路径也可以是相对路径。
当packagesPath
没有设置或者其值为空白,那么默认的包文件夹为~/Packages
。
添加package
一旦Nuget.Server应用运行起来了,可以使用nuget push添加包。
安装完Nuget.Server,web.config会有一个appSetting/apiKey
:
<appSettings>
<add key="apiKey" value="" />
</appSettings>
当没有apiKey
或者其值为空的时候,push package到feed是禁用的。
设置apiKey并添加appSettings/requireApiKey
设置其值为true
:
<appSettings>
<!-- Sets whether an API Key is required to push/delete packages -->
<add key="requireApiKey" value="true" />
<!-- Set a shared password (for all users) to push/delete packages -->
<add key="apiKey" value="" />
</appSettings>
可以设置requireApiKey
为false
。这样所有的人都可以push package了。
删除package
使用nuget delete命令从仓储中删除package。
可以设置web.config中的enableDelisting
为true
启用delist。