首页 > 解决方案 > Creating a new package and adding it ot nuget source in Visual Studio

问题描述

I have a Asp.Net MVC solution, that needs to be added as a Nuget package, which can be used as a shared component by internal applications.

I have googled a bit and found that we can create a nuget package only by using a class library. project. In my case how can I create a nuget package using a web application ? Or should I create a class library project referencing the the Asp.Net MVC application dll ?

Previously this whole application as a dll was referenced by other components/ projects. Now I need to move this dll to the nuget source so that it can be still used by other projects by downloading it from nuget manager.

Thanks in advance.

标签: asp.net-mvcnugetnuget-package

解决方案


您可以添加一个 .nuspec 文件来定义您的 dll 及其依赖项,并添加一个构建后调用来创建包

示例 nuspec 文件:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" >
  <metadata>
    <id>myproductid</id>
    <version>$version$</version>
    <title>some title</title>
    <authors>me</authors>
    <owners>myself</owners>
    <projectUrl>http://www.example.com</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>...</description>
    <releaseNotes>First nuget release.</releaseNotes>
    <copyright>Copyright 2020</copyright>
    <dependencies>
      <dependency id="AutoMapper" version="9.0.0" />
      <dependency id="jQuery" version="3.3.1" />
      <dependency id="Microsoft.AspNet.Mvc" version="5.2.7" />
      <dependency id="Microsoft.AspNet.Razor" version="3.2.7" />
      <dependency id="Newtonsoft.Json" version="12.0.2" />
      <dependency id="System.Reflection.TypeExtensions" version="4.6.0" />
    </dependencies>
  </metadata>
  <files>
    <file src="bin\My.WebApplication.dll" target="lib\net461" />
  </files>
</package>

就我而言,构建后事件如下所示:

if ($(Configuration)) == (Release) nuget pack "$(ProjectPath)" -Prop Configuration=Release  -OutputDirectory "K:\nuget"

推荐阅读