【问题标题】:Trying to create reusable ASP.NET MVC application. Templates/NuGet/Both?试图创建可重用的 ASP.NET MVC 应用程序。模板/NuGet/两者?
【发布时间】:2018-06-18 20:22:18
【问题描述】:
我有一个带有自定义控制器和一些默认视图的“核心”应用程序,我想用它们来创建一些扩展核心功能的自定义应用程序。我了解从该项目创建模板的过程。但是,如果我添加一些新的核心功能,我希望能够从该模板中刷新并维护我的所有扩展。模板似乎不支持这一点。我已经对 NuGet 进行了一些试验,因为这似乎是我想要的更多,但我不确定将整个核心应用程序放入 NuGet 包是否是正确的方法。有人做过这样的事吗?
【问题讨论】:
标签:
asp.net-mvc
visual-studio
nuget
【解决方案1】:
但我不确定将整个核心应用程序放入 NuGet 包是否正确
我们知道,NuGet 本身是一个需要清单或项目文件才能工作的最小工具,而 dotnet pack 只会打包源文件,而不是所有资产。因此,如果要将整个核心应用程序放入 NuGet 包中,则应手动将清单添加到 .nuspec 文件中。
此外,由于您只有一些自定义控制器和一些用于创建自定义应用程序的默认视图,因此您不需要将整个核心应用程序包含在 NuGet 包中,只需将这些控制器和视图添加到 nuget 包中。
由于您使用的是核心应用程序,因此您应该使用ContentFiles 来包含您的控制器和视图文件。以下是我的测试样本:
我的.nuspec 文件:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>MyTestCore</id>
<version>4.0.0</version>
<authors>TestContentFile</authors>
<owners>TestContentFile</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<contentFiles>
<files include="any/any/Views/Test.cshtml" buildAction="content" flatten="true" copyToOutput="false"/>
<files include="any/any/Controllers/Test.cs" buildAction="content" flatten="true" copyToOutput="false"/>
</contentFiles>
</metadata>
<files>
<file src="contentFiles/any/any/Views/Test.cshtml" target="contentFiles/any/any/Views/Test.cshtml" />
<file src="contentFiles/any/any/Controllers/Test.cs" target="contentFiles/any/any/Controllers/Test.cs" />
</files>
</package>
文件资源管理器中的控制器和视图文件的结构:
打包此包并安装到其他项目后,控制器和视图文件将添加到项目中。
请参阅NuGet ContentFiles Demystified 了解更多详细信息。