如何将现有的asp.net应用程序到ASP.NET MVC模式格式 [英] How to migrate existing asp.net application to asp.net MVC pattern format

查看:132
本文介绍了如何将现有的asp.net应用程序到ASP.NET MVC模式格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想迁移现有的asp.net应用程序现在ASP.NET MVC模式的格式。我应该会遵循什么程序?任何一步一步的操作将是非常有益的。

I want to migrate existing asp.net application to asp.net MVC pattern format now. What procedure should I will follow? Any step-by-step instructions would be very helpful.

推荐答案

这是我一步一步的指导,根据步骤中,我们在我的公司我们从经典的ASP.Net Web表单搬到ASP.Net中采取MVC。因为我们必须这样做,因为该网站的规模阶段,但也许别人会根据我们的研究结果发现并提交改进答案它并不是完美的,并且仍在继续。

These is my step-by-step guide, based on steps we have taken at my company during our move from a classic ASP.Net Webforms to ASP.Net MVC. It's not perfect, and still ongoing since we have to do this in stages because of the size of the site, but perhaps someone else will find and file an improved answer based on our results.

阶段:
1.规划 - 从ASP.Net Web窗体移动到MVC需要一些仔细的规划。我们在移动所犯的错误是没有意识到有实际上有两个方面,到这个阶段的规划,路线规划和模型/控制器/行动规划。
如果不这样做,这将导致稍后当你试图延长你的网站的功能或打更复杂的迁移严肃的问题。

Stages: 1. Planning - moving to MVC from Web Forms in ASP.Net requires some careful planning. The mistake we made in our move is not realising that there are are really two aspects to this stage of planning, route planning and model/controller/action planning. Not doing this will cause serious issues later on as you try to extend functionality of your site or hit more complex migrations.

温馨提示:
- 看当前的站点地图,并设计在ASP.Net MVC应用程序中使用改进的网站地图/目录结构。找出一种语言为您的网站,例如ASP.Net MVC的默认行为是有一个的http://站点名称/ {控制器} / {行动} / {ID}行为,但你当你获得更多的经验黑客的路由规则可以覆盖这一点。

Tips: - Look at your current sitemap, and design the improved sitemap/directory structure to be used in the ASP.Net MVC application. Figure out a 'language' for your website, e.g. the default behaviour of ASP.Net MVC is to have a http://sitename/{controller}/{action}/{id} behavior, but you can override this as you gain more experience hacking routing rules.


  • 默认情况下,每个控制器将通过您的应用程序,例如一个虚拟的子目录排到记住 HTTP://站点名称/ X 将路线XController(并且默认它的索引方式), HTTP://站点名称/ Y /获取将路线YController的get()方法。请你(路由真的很强大)你可以改变这一点,但这超出了这个答案的范围。

  • Remember by default each Controller will be routed to via a virtual subdirectory of your application, e.g. http://sitename/X would route to XController (and by default its Index method), http://sitename/Y/Get would route to YController's Get() method. You can change this as you please (routing's really powerful), but that's beyond the scope of this answer.

利用现有的地图,指定每个当前.aspx页面中应属于哪个文件夹中的MVC结构(当然,首先询问是否应该存在的话)。

Using the existing sitemap, specify what folder in the MVC structure each current .aspx page should fall (of course, first ask if it should exist at all).

如果脚本,图片等不存储在一起,或者在每个子目录内的一些保留名称'的文件夹,可以考虑所以现在为你重新设计做的事情。
这是因为这将极大地允许您在Global.aspx.cs使用Map.IgnoreRoute()的路由规则命令简化设计文件绕过处理这些文件夹的路径。

If Scripts, Images etc are not stored together, or in some 'reserved name' folders within each subdirectory, consider doing so now as you're redesigning. This is as it would greatly simplify your design by allowing you to use a Map.IgnoreRoute() routing rule command in the Global.aspx.cs file to bypass processing these folders as routes.

在我们的例子中,我们反映了当前站点,其中每个子目录成为一个控制器,例如真正的子目录布局/账户将有一个的AccountController,/ X将有XController。落在里面有所有的网页是由每个控制器内的操作代替。例如 HTTP://sitename/profile/about.aspx 成了现在的 HTTP://站点名称/资料/关于并映射到ProfileController可里面的关于的ActionResult方法。
这是使我们通过在一系列的冲刺做一个或两个目录(或一个目录中的几个文件)的部分迁移,而不是对整个网站的迁移一气呵成了更长的时间,以保持敏捷。

In our case we mirrored the real subdirectory layout of the current site, where each subdirectory became a controller, e.g. /Account would have an AccountController, /X would have XController. All pages that fell inside there were replaced by actions within each Controller. e.g. http://sitename/profile/about.aspx now became http://sitename/profile/about and mapped to the "about" ActionResult method inside the profileController. This is allowing us to stay agile by doing a partial migration of one or two directories (or several files inside one directory) over a series of sprints, rather than having to migrate the entire site in one go over a much longer duration.


  1. 在Visual Studio中创建一个新的ASP.Net MVC应用程序,并立即建立在Global.asax文件中忽略了存在于当前网站中的文件夹路由规则的规则。

  1. Create a new ASP.Net MVC application in Visual Studio and immediately create the rules in the Global.asax file that ignore routing rules for the folders that exist in the current site.

从ASP.Net Web应用程序的ASP.Net MVC应用程序文件夹复制的文件夹。运行该网站,并确保其工作正常(它应该不存在路由规则正在尚未使用)。

Copy the folders from the ASP.Net Web Application to the ASP.Net MVC Application folders. Run the website and ensure it works properly (it should as no routing rules are being used yet).

子目录迁移中选择文件的子目录或子集。

Pick a subdirectory or subset of files within a subdirectory to migrate.

有关这个子目录中的每个.aspx页面中:

For each .aspx page inside this subdirectory:

一个。首先创建视图。我倾向于使用呈现的网页版本作为我的基本HTML网络浏览器,然后把占位符,我知道都充满了动态数据的位置。

a. Create its View first. I tend to use the web-browser rendered version of the page as my base HTML, and then put placeholders in the locations that I know are filled with dynamic data.

乙。使用动态数据的占位符,创建一个使用简单数据类型模型的第一稿。这种模式将开始简单的,但你迁移从原来的网站更多的网页不断重构,因此如果它开始看起来有点沉重不用担心。如果你发现自己有太多的属性在一个模型你的味道,或者看到一个逻辑分组超出项目的特定子集的只是模型,也许这是一个标志模型需要进行重构有一个对象,而不是用这些简单的数据类型,性质,但由业务逻辑层。

b. Using the placeholders for the dynamic data, create a first-draft of the Model using simple data types. This model will start off simple, but be constantly refactored as you migrate more pages from the original site, so don't worry if it starts looking a little heavy. If you find yourself with too many Properties in one Model for your taste, or see a logical grouping beyond just the Model of certain subset of items, perhaps this is a sign the Model needs to be refactored to have an object instead with these simple data types as properties but is composed in the business logic layer.

℃。创建控制器如果它未尚未创建,并出台相应的ActionResult方法,你的计划已确定应该以这种观点航线的行动。
如果你能意识到的东西,有没有映射到旧网站的一个页面一个新的动作,然后创建控制器的视图,包括适当// TODO:标签,这样可以保持的这条赛道后你实现在迁移现有的页面。

c. Create the controller if it hasnt been created yet, and put the appropriate ActionResult method for the Action your planning has determined should route to this view. If you realise something that there is a new action that doesn't map to a page from the old site, then create the View for the controller, and include appropriate //TODO: tags so you can keep track of this to implement after you have migrated the existing pages.

Ð。考虑把一些处理code为未知的动作为好,如果你没有在你的Global.asax.cs文件中有一个{*}包罗万象的路由规则此了。

d. Consider putting in some handling code for unknown actions as well, if you don't have a {*catchall} Routing rule for this already in your global.asax.cs file.

即为模型创建构造类,以便该控制器将(通过作为您的{ID}或可能从URL或HTTP报头或cookie中的Request.QueryString参数)给予一定的参数,模型会知道如何伸出您现有的业务逻辑类,并建立本身的渲染视图

e. Create the constructor classes for the Model so that given certain parameters that the Controller will have (passed in as your {id} or possibly a Request.QueryString parameter from the URL, or an HTTP header or cookie), the Model will know how to reach out to your existing business logic classes and build up itself for rendering by the View.

F。转到下一页在列表中,并从步骤重新开始。

f. Go to next page in the list and start again from step a.

最后,创建路由规则,将打电话给你新的控制器,让你写的操作实施。调试,调试,调试......一旦你很高兴一切都很好,删除现有的文件夹和文件,你从主站点迁移,以及在Global.asax.cs中的IgnoreRoute规则。

Finally create the routing rule that will call your new Controller and allow the Actions you have written to be implemented. Debug, debug, debug...Once you're happy all is well, remove the existing folder and files you've migrated from your main site, as well as the IgnoreRoute rule in the global.asax.cs.

创建你,如果你想保持连续性preserve旧目录和文件名的任何方式preFER重定向(例如,用户可能已经收藏在旧的网站已经确定页)。

Create a redirect in whatever manner you prefer if you wish to preserve old directory and file names for continuity (e.g. users may have bookmarked certain pages in the old site already).

请注意:如果您在移植阶段保留旧子目录在你的MVC网站的确切名称,它是preferable迁移时我已经意识到时间整个子目录,因为只有做了几个文件你需要写变得更加复杂,因为如果使用相同的名称作为一个路由规则的路径中存在一个现有文件夹,该文件夹中有一个Default.aspx文件,然后(/ FOLDERNAME /)将默认为Default.aspx页面的路由规则,它接管路由规则precidence。

Note: If you are keeping the exact names of the old subdirectories in your MVC site during the porting phase, it's preferable to migrate a whole subdirectory at a time I've realised, because by only doing a few files the routing rules you need to write become more complex since if an existing folder exists with the same name as a routing rule's path and that folder has a Default.aspx file then (/foldername/) will default to the Default.aspx page, as it takes precidence over the routing rules.

提示:认真考虑使用像 RouteDebug 一个工具,路线调试等等你可以计算出奇怪的事情像上面,或者当你有多个路由规则开火,造成意外行为。

Tip: Seriously consider using a tool like RouteDebug for route debugging so you can figure out strange things like above, or when you have multiple routing rules firing and causing unexpected behaviors.

这是我的第一稿,请给我的反馈,如果我错过了任何步骤,或者如果你看到指南中的任何漏洞,我会适当地修改答案。

This is my first draft, please give me feedback if I've missed any steps or if you see any holes in the guide, and I'll modify the answer appropriately.

这篇关于如何将现有的asp.net应用程序到ASP.NET MVC模式格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆