通过7个简单的步骤创建NuGet包-加上使用NuGet将ASP.NET MVC 3集成到现有Web Forms应用程序中的步骤...

UPDATE: Check out my follow up post where I remove the need for editing the Global.asax.cs and show up to Update and Publish a NuGet package.

更新:查看我的后续帖子,其中不再需要编辑Global.asax.cs并显示“更新和发布NuGet包”

Last month I wrote a post called Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications where I showed a very manual and very painful way to add ASP.NET MVC support to an existing ASP.NET WebForms application. You'd then have a lovely hybrid that is both MVC and WebForms.

上个月,我写了一篇名为《将ASP.NET MVC 3集成到现有的升级的ASP.NET 4 Web Forms应用程序中》的文章,其中我展示了一种非常手动且非常痛苦的方式来将ASP.NET MVC支持添加到现有的ASP.NET WebForms应用程序中。 然后,您将拥有一个既可爱又混合在一起的MVC和WebForms。

One of my readers, Yannick said:

我的一位读者Yannick说:

This screeaaams NuGet

这是NuGet的内容

Indeed it did, er, does. He's saying that this is just the kind of awful boring work that NuGet should make easier. So I did it. Thanks Yannick for just the sassy comment I needed to jump into action.

确实,确实如此。 他是说,这只是NuGet应该简化的那种无聊的工作。 所以我做到了。 感谢Yannick仅仅提出我需要采取行动的时髦评论。

install-package AddMvc3ToWebForms

First, what I built, then how I built it. I'd like you, Dear Reader, to take a moment and create your own NuGet packages.

首先,是我构建的,然后是我如何构建的。 亲爱的读者,我想花一点时间来创建自己的NuGet软件包。

使用NuGet将ASP.NET MVC添加到ASP.NET WebForms项目 (Adding ASP.NET MVC to an ASP.NET WebForms project with NuGet)

Step 0 - Get NuGet 1.1 by going here. It's like 300k, just take a second.

步骤0-通过此处获取NuGet 1.1 大约30万,只需要一秒钟。

Step 1 - Open Visual Studio 2010 and make a default ASP.NET (WebForms) Application.

步骤1-打开Visual Studio 2010,并创建一个默认的ASP.NET(WebForms)应用程序。

Step 2 - Right click on References and click Add Library Package Reference. Click Online on the left side, and in the Search box at upper right type in "WebForms" and look for my face. Oh yes. My face. Click Install.

步骤2-右键单击“引用”,然后单击“添加库包引用”。 单击左侧的“在线”,然后在右上角的“搜索”框中键入“ WebForms”,然后寻找我的脸。 哦,是的。 我的脸。 单击安装。

(Alternatively, open the Package Manager Console and type "install-package AddMvc3ToWebForms" and watch the magic. The package is hosted on NuGet.org. You can too!)

(或者,打开程序包管理器控制台,然后键入“ install-package AddMvc3ToWebForms”,然后观看魔术。该程序包托管在NuGet.org上。您也可以!)

Step 2a - Check out the stuff that's been added to your project.

第2a步-检查已添加到项目中的内容。

What's that HookMeUpNow.cs? That's all the routing stuff that I would have needed to edit your Global.asax.cs for. You'll need to add one line of code to Global.asax yourself to make this work now.

那是HookMeUpNow.cs? 这就是编辑您的Global.asax.cs所需的所有路由内容。 您需要自己向Global.asax添加一行代码,以立即完成此工作。

Step 3 - Hook up Routes and Everything

第3步-连接路线和所有内容

Add Mvc3Utilities.RegisterEverything() to your Application_Start. Feel free to rename whatever you like.

将Mvc3Utilities.RegisterEverything()添加到您的Application_Start。 随意重命名您喜欢的任何东西。

Now run it. You can hit both Default.aspx and /Home/About.

现在运行它。 您可以同时单击Default.aspx和/ Home / About。

Step 4 - Profit!

步骤4-获利!

Sweet.

甜。

我如何制作自己的NuGet软件包,您也应该 (How I made my own NuGet package and you should too)

Step 0 - Go get the NuGet.exe command line here. Put it in the Path or somewhere.

步骤0-在此处获取NuGet.exe命令行 将其放在路径或某处。

Step 1 - Make a folder for your new package, go there via the commmand line and run "nuget spec"

第1步-为新软件包创建一个文件夹,通过命令行转到该文件夹​​并运行“ nuget spec”

C:\Users\Scott\Desktop\AddMvc3ToWebForms>nuget spec
Created 'Package.nuspec' successfully.

C:\ Users \ Scott \ Desktop \ AddMvc3ToWebForms> nuget规范成功创建“ Package.nuspec”。

C:\Users\Scott\Desktop\AddMvc3ToWebForms>dir Package.nuspec
Directory of C:\Users\Scott\Desktop\AddMvc3ToWebForms

C:\ Users \ Scott \ Desktop \ AddMvc3ToWebForms> dir Package.nuspec C:\ Users \ Scott \ Desktop \ AddMvc3ToWebForms的目录

02/15/2011  02:23 AM               813 Package.nuspec
               1 File(s)            813 bytes

2011年2月15日02:23 813 Package.nuspec 1个文件813个字节

Now, I changed this file's name and edited it thusly.

现在,我更改了该文件的名称并进行了编辑。

<?xml version="1.0"?>
<package xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>AddMvc3ToWebForms</id>
<version>0.4</version>
<authors>Scott Hanselman</authors>
<owners>Scott Hanselman</owners>
<iconUrl>http://www.hanselman.com/images/nugeticon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A totally unsupported way to quickly add ASP.NET MVC 3 support to your WebForms Application. Works on my machine.</description>
<tags>MVC MVC3 ASP.NET WebForms</tags>
</metadata>
</package>

Step 2 - Add stuff to your Content Folder

第2步-将内容添加到您的内容文件夹

Since I want my NuGet package to add stuff to folders in my target Web Application, I put whatever I want in a folder called Content. Anything in that will show up in the root of my target project. This can be CSS, JS, CS or VB files, whatever. These files will all get dropped onto the project your package is applied to.

由于我希望我的NuGet包将东西添加到目标Web应用程序中的文件夹中,因此我将所需的内容放在名为Content的文件夹中。 其中的任何内容都会显示在目标项目的根目录中。 可以是CSS,JS,CS或VB文件。 这些文件将全部放到您的程序包所应用到的项目上。

In my project I took the folders from an MVC application and put them in my NuGet folder structure. So, Content, Controllers, Models, Scripts, Views. Copied them right over from an existing blank ASP.NET MVC project.

在我的项目中,我从MVC应用程序中获取了文件夹,并将其放入我的NuGet文件夹结构中。 因此,内容,控制器,模型,脚本,视图。 直接从现有的空白ASP.NET MVC项目中复制它们。

Step 3 - Decide what needs to be Pre-Processed

第3步-确定需要预处理的内容

However, when my HomeController shows up in your project, Dear Reader, I don't want it to be in the namespace ScottMvcApplication! You want it in MvcApplication54 or whatever your project name is. I need pre-process the source a little to use your project's context, names, namespaces, etc.

但是,当我的HomeController出现在您的项目中,亲爱的读者,我不希望它出现在名称空间ScottMvcApplication中! 您希望在MvcApplication54中使用它,或者在您的项目名称中使用它。 我需要对源进行一些预处理,以使用项目的上下文,名称,名称空间等。

For the files I want pre-processed automatically by NuGet, I add a .pp extension. In my example, HomeController.cs.pp.

对于要由NuGet自动预处理的文件,我添加了.pp扩展名。 在我的示例中,为HomeController.cs.pp。

Then I add a few tokens I want replaced at install-time for that package. For example $rootnamespace$ or $assemblyname$. You can use any Visual Studio Project Property per the NuGet docs.

然后,我在安装时添加了一些我想替换的令牌。 例如$ rootnamespace $或$ assemblyname $。 您可以根据NuGet文档使用任何Visual Studio项目属性

namespace $rootnamespace$.Controllers
{
public class HomeController : Controller
{
//snip
}
}

Step 4 - Decide what XML elements need to be merged (usually into web.config)

第4步-确定需要合并哪些XML元素(通常合并到web.config中)

The next preprocessing that is common is adding elements to web.config. This is a nice little feature of NuGet because you just need to make a web.config.transform with the new elements and it will automatically and non-destructively add (and remove) them as needed. Here's my web.config.transform, for reference. Note this is not a full web.config. This is the one I added to my package in the control folder.

下一个常见的预处理是将元素添加到web.config。 这是NuGet的一个不错的小功能,因为您只需要使用元素制作一个web.config.transform,它将根据需要自动且无损地添加(和删除)它们。 这是我的web.config.transform,以供参考。 请注意,这不是完整的web.config。 这是我在控制文件夹中添加到程序包中的文件。

<configuration>
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Step 5 - Add any PowerShell script you might need, especially for adding references

第5步-添加您可能需要的任何PowerShell脚本,尤其是添加引用时

Almost done. Most package won't need much PowerShell, but some do. You can have an install.ps1 and an uninstall.ps1 and do lots of things. These go in a folder called Tools that's next to Content (not inside.)

快完成了大多数软件包不需要太多PowerShell,但有些则需要。 您可以具有install.ps1和uninstall.ps1并执行许多操作。 这些文件位于“内容”旁边(而不是内部)旁边的名为“工具”的文件夹中。

Here's my install.ps1.

这是我的install.ps1。

NOTE: Currently today there's no way to STOP the installation of a package while it's happening, so if you try to install mine on NuGet 1.0 I'll just warn you and ask you to uninstall. In the future there will likely be a pre-install or a dependency check. Hence the version check there.

注意:目前,今天尚无法停止软件包的安装,因此,如果您尝试在NuGet 1.0上安装我的软件包,我会警告您并要求您卸载。 将来可能会进行预安装或依赖性检查。 因此,在那里检查版本。

param($installPath, $toolsPath, $package, $project)

if ($host.Version.Major -eq 1 -and $host.Version.Minor -lt 1)
{
"NOTICE: This package only works with NuGet 1.1 or above. Please update your NuGet install at http://nuget.codeplex.com. Sorry, but you're now in a weird state. Please 'uninstall-package AddMvc3ToWebForms' now."
}
else
{
$project.Object.References.Add("Microsoft.CSharp");
$project.Object.References.Add("System.Web.Mvc");
$project.Object.References.Add("Microsoft.Web.Infrastructure");
$project.Object.References.Add("System.Web.WebPages");
$project.Object.References.Add("System.Web.Razor");
$project.Object.References.Add("System.ComponentModel.DataAnnotations");
}

Note that in (the future) NuGet 1.2 I won't need this code, I'll just add the references in my NuSpec file directly.

请注意,在(将来的)NuGet 1.2中,我将不需要此代码,只需将引用直接添加到我的NuSpec文件中即可。

Step 6 - Pack it up

第6步-打包

Go back to the command line and run nuget pack

返回命令行并运行nuget pack

C:\Users\Scott\Desktop\AddMvc3ToWebForms>nuget pack
Attempting to build package from 'AddMvc3ToWebForms.nuspec'.
Successfully created package 'C:\Users\Scott\Desktop\AddMvc3ToWebForms\AddMvc3ToWebForms.0.4.nupkg'.

C:\ Users \ Scott \ Desktop \ AddMvc3ToWebForms>裸包尝试从“ AddMvc3ToWebForms.nuspec”构建软件包。 成功创建包“ C:\ Users \ Scott \ Desktop \ AddMvc3ToWebForms \ AddMvc3ToWebForms.0.4.nupkg”。

Step 7 - Submit your package

步骤7-提交您的包裹

Next, login to the NuGet Gallery (beta) and Contribute Your Package. Just walk through the wizard and upload the nupkg. You can also get an API Key and use the command line tool to do this automatically, perhaps as part of a build process.

接下来,登录到NuGet Gallery(测试版)并贡献您的包裹。 只需完成向导并上传nupkg。 您还可以获取API密钥并使用命令行工具自动执行此操作,这可能是构建过程的一部分。

Submitting my app to the NuGet Gallery

That's it. If you've got an open source library or something interesting or useful, get it up on NuGet before your blog commenters shame you into it!

而已。 如果您拥有开放源代码库或有趣或有用的东西,请在博客评论者羞辱您之前在NuGet上进行使用!

P.S. Yes I didn't count Step 0.

PS是的,我没有计算步骤0。

翻译自: https://www.hanselman.com/blog/creating-a-nuget-package-in-7-easy-steps-plus-using-nuget-to-integrate-aspnet-mvc-3-into-existing-web-forms-applications

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值