热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通过7个简单的步骤创建NuGet包加上使用NuGet将ASP.NETMVC3集成到现有WebForms应用程序中的步骤...

UPDATE:CheckoutmyfollowuppostwhereIremovetheneedforeditingtheGlobal.asax.csandshowuptoUpda

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.

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




AddMvc3ToWebForms
0.4
Scott Hanselman
Scott Hanselman
http://www.hanselman.com/images/nugeticon.png
false
A totally unsupported way to quickly add ASP.NET MVC 3 support to your WebForms Application. Works on my machine.
MVC MVC3 ASP.NET WebForms


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。 这是我在控制文件夹中添加到程序包中的文件。










































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




推荐阅读
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 本文介绍了在CentOS 6.4系统中更新源地址的方法,包括备份现有源文件、下载163源、修改文件名、更新列表和系统,并提供了相应的命令。 ... [详细]
  • 本文介绍了RxJava在Android开发中的广泛应用以及其在事件总线(Event Bus)实现中的使用方法。RxJava是一种基于观察者模式的异步java库,可以提高开发效率、降低维护成本。通过RxJava,开发者可以实现事件的异步处理和链式操作。对于已经具备RxJava基础的开发者来说,本文将详细介绍如何利用RxJava实现事件总线,并提供了使用建议。 ... [详细]
  • wpf+mvvm代码组织结构及实现方式
    本文介绍了wpf+mvvm代码组织结构的由来和实现方式。作者回顾了自己大学时期接触wpf开发和mvvm模式的经历,认为mvvm模式使得开发更加专注于业务且高效。与此同时,作者指出mvvm模式相较于mvc模式的优势。文章还提到了当没有mvvm时处理数据和UI交互的例子,以及前后端分离和组件化的概念。作者希望能够只关注原始数据结构,将数据交给UI自行改变,从而解放劳动力,避免加班。 ... [详细]
  • node.jsurlsearchparamsAPI哎哎哎 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
author-avatar
攻玉是我_944
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有