asp.net-mvc - 用于修改 Global.asax.cs 的 Nuget PowerShell 脚本

标签 asp.net-mvc powershell nuget

我正在为我公司的 MVC4 模板构建一个 nuget 包。我遇到了需要 Global.asax.cs 的问题修改为添加这两行:

using System.Web.Optimization;

在顶部,在命名空间之前和
BundleConfig.RegisterBundles(BundleTable.Bundles);

Application_Start()方法

我尝试创建一个 Global.asax.cs.ppnamespace $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/

相关文章:

asp.net-mvc - MVC - 字典需要类型为 'System.Collections.Generic.IEnumerable` 1 的模型项

html - 如何动态地在网页中的图像上放置不同的图案。

c# - 如何将此 PowerShell 脚本转换为 C#? - 获取项目属性

c# - 如何创建 .NET 4 MVC 3 作业/队列系统?

JavaScript 文件太大? - MVC

NuGet "Failed to generate binding redirects for My Project"错误 "Object reference not set to an instance of an object."

powershell - 在 Visual Studio Nuget 包管理器控制台中中止 "unclosed"powershell 命令

c# - 是否需要 bindingRedirect .config 文件或应用程序中的所有程序集?

powershell - 循环直到用户输入三个必需字母之一

powershell - 是否可以通过 Powershell 远程处理以 "nt authority\system"启动进程?