我正在为我公司的 MVC4 模板构建一个 nuget 包。我遇到了需要 Global.asax.cs
的问题修改为添加这两行:
using System.Web.Optimization;
在顶部,在命名空间之前和
BundleConfig.RegisterBundles(BundleTable.Bundles);
末
Application_Start()
方法我尝试创建一个
Global.asax.cs.pp
与 namespace $rootnamespace$
在里面,但这似乎不起作用。似乎 Nuget 不会覆盖现有文件?在我看来,我的最后一个选择是编写一个 powershell 脚本(
Install.ps1
?)来执行此操作。这是我的 template.nuspec
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>Template.MVC4</id>
<version>1.5</version>
<title>MVC4 Template</title>
<description>Installs and configures files for MVC 4 application template.</description>
<authors>Me</authors>
<language>en-US</language>
<dependencies>
<dependency id="Microsoft.AspNet.Web.Optimization" />
</dependencies>
<iconUrl>http://www.someurl.com/Logo.jpg</iconUrl>
</metadata>
<files>
<file src="Template\Helpers\*" target="content\Helpers\" />
<file src="Template\Content\*.css" target="content\Content\" />
<file src="Template\Content\animGif\*" target="content\Content\animGif\" />
<file src="Template\Content\custom-theme\*" target="content\Content\custom-theme\" />
<file src="Template\Content\templateImages\*" target="content\Content\templateImages\" />
<file src="Template\Scripts\*" target="content\Scripts\" />
<file src="Template\Views\Shared\*" target="content\Views\Shared\" />
<file src="Template\Views\Home\*" target="content\Views\Home\" />
<file src="Template\Views\_ViewStart.cshtml" target="content\Views\" />
<file src="NuGetPackage\App_Start\*" target="content\App_Start\" />
<file src="NuGetPackage\Controllers\*" target="content\Controllers\" />
<file src="NuGetPackage\Helpers\*" target="content\Helpers\" />
<file src="NuGetPackage\Models\*" target="content\Models\" />
<file src="NuGetPackage\Views\*" target="content\Views\" />
<file src="NuGetPackage\*" target="content\" />
</files>
</package>
我的问题是 2 倍,1) 我在 .nuspec 中做错了什么吗?和 2) 如果修改
Global.asax
使用 .pp 不是一个选项,那么当这个 nuget 添加到项目时,我需要编写什么 powershell 脚本来自动运行它,我是否需要做任何特殊的事情才能运行(阅读文档似乎只是将 Install.ps1
放在 tools\
中)?
最佳答案
这是万一有人需要的答案,这在您的 Install.ps1
中内Tools
文件夹:
param($installPath, $toolsPath, $package, $project)
# Read the transformed text from the custom template included in the package
$customGlobalAsax = $project.ProjectItems | where { $_.Name -eq "Global.asax.cs.custom" }
$customGlobalAsax.Open()
$customGlobalAsax.Document.Activate()
$customGlobalAsax.Document.Selection.SelectAll();
$replacementGlobalAsax = $customGlobalAsax.Document.Selection.Text;
$customGlobalAsax.Delete()
# Replace the contents of Global.asax.cs
$globalAsax = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "Global.asax.cs" }
if($globalAsax) {
$globalAsax.Open()
$globalAsax.Document.Activate()
$globalAsax.Document.Selection.SelectAll()
$globalAsax.Document.Selection.Insert($replacementGlobalAsax)
$globalAsax.Document.Selection.StartOfDocument()
$globalAsax.Document.Close(0)
}
关于asp.net-mvc - 用于修改 Global.asax.cs 的 Nuget PowerShell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13242282/