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

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

问题描述

我想将现有的 ASP.NET 应用程序迁移到 ASP.NET MVC 模式格式.我应该遵循什么程序?任何分步说明都会非常有帮助.

I want to migrate an existing ASP.NET application to an ASP.NET MVC pattern format. What procedure should I follow? Any step-by-step instructions would be very helpful.

推荐答案

这些是我的分步指南,基于我们在公司从经典 ASP.Net Webforms 迁移到 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://sitename/{controller}/{action}/{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://sitename/X 会路由到 XController(默认是它的 Index 方法),http://sitename/Y/Get 将路由到 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.

在我们的例子中,我们镜像了当前站点的真实子目录布局,其中每个子目录成为一个控制器,例如/Account 将有一个 AccountController,/X 将有 XController.里面的所有页面都被每个控制器中的动作替换.例如http://sitename/profile/about.aspx 现在变成了 http://sitename/profile/about 并映射到 profileController 中的about"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.使用动态数据的占位符,使用简单的数据类型创建模型的初稿.这个模型一开始很简单,但随着您从原始站点迁移更多页面,它会不断重构,所以如果它看起来有点沉重,请不要担心.如果您发现自己在一个模型中有太多的属性,或者看到超出某些项目子集的模型的逻辑分组,这可能表明模型需要重构以拥有对象而不是这些简单的数据类型作为属性,但在业务逻辑层中组合.

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.

c.如果尚未创建控制器,则创建控制器,并为您的计划确定应该路由到此视图的操作放置适当的 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.

d.如果您的 global.asax.cs 文件中没有 {*catchall} 路由规则,也可以考虑为未知操作添加一些处理代码.

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.

e.为模型创建构造函数类,以便给定控制器将具有的某些参数(作为您的 {id} 或可能来自 URL 的 Request.QueryString 参数,或 HTTP 标头或 cookie 传入),模型将知道如何访问您现有的业务逻辑类并构建自身以供视图呈现.

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.转到列表中的下一页,然后从步骤 a 重新开始.

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.

如果您希望保留旧目录和文件名以保持连续性(例如,用户可能已经为旧站点中的某些页面添加了书签),请以您喜欢的任何方式创建重定向.

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 站点中旧子目录的确切名称,最好在我意识到的时候迁移整个子目录,因为只需执行几个文件,路由规则您需要编写变得更加复杂,因为如果存在与路由规则路径同名的现有文件夹,并且该文件夹具有 Default.aspx 文件,那么 (/foldername/) 将默认为 Default.aspx 页面,因为它优先超过路由规则.

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天全站免登陆