I have on request from a client built a huge site with ASP.NET Web forms. The problem is that I'm finding ASP.NET Web forms to be somewhat unintuitive (my personal taste only). So what I would like to do is use MVC but I can't really expect my client to pay for my time on a complete rewrite (nor do he have the time).
我有一个客户要求我用ASP建一个大的网站。净Web表单。问题是我正在寻找ASP。NET Web表单有点不直观(我个人的喜好)。所以我想做的是使用MVC,但是我不能期望我的客户为我的完整重写支付时间(他也没有时间)。
So what I'm asking is, can I use ASP.NET MVC and Web forms at the same time and gradually use MVC more and more? Any tips?
我想问的是,我可以用ASP吗?NET MVC与Web表单同时使用,并逐渐越来越多的使用MVC ?任何建议吗?
1 个解决方案
#1
16
Update 2014:
2014年更新:
Visual Studio 2013 brings us closer to One ASP.NET. There’s no MVC project type or Web Forms project any longer, there’s just ASP.NET. If you want to mix Web Forms and Web API, or MVC and SignalR, go ahead! You are encouraged and supported. New features and functionality are brought in with NuGet without breaking existing apps.
Visual Studio 2013让我们更接近一个ASP.NET。不再有MVC项目类型或Web表单项目,只有ASP.NET。如果您想混合Web表单和Web API,或者MVC和SignalR,请继续!你受到鼓励和支持。在不破坏现有应用的情况下,NuGet引入了新的功能和功能。
和13
So the mixing of Web Forms and MVC (which has been pretty much been working seamlessly) is now encouraged and the boundaries are obfuscated. I guess MS recognized the need to allow projects to slowly migrate to MVC, or just migrate portions as needed, and gain the best of both worlds.
因此,Web表单和MVC(几乎一直无缝地工作)的混合现在得到了鼓励,边界也变得模糊。我猜MS认识到需要允许项目缓慢地迁移到MVC,或者只是根据需要迁移部分,并在这两个方面都取得最好的效果。
MVC Project:
MVC项目:
Having used MVC with Web forms for quite some time it is certainly possible and an excellent choice. New functionality and pages can be easily added into MVC pages with good architectural design such as DDD (Service Repository, Dependency Injection etc.) and the old stuff can stay as it is. Combining MVC inside a webforms page also works fine, although there can be some minor issues with JS validation. I would highly recommend it.
使用MVC与Web表单已经有一段时间了,这当然是可能的,也是一个很好的选择。新的功能和页面可以很容易地添加到具有良好架构设计的MVC页面中,比如DDD(服务存储库、依赖注入等等),而旧的东西可以保持原样。在webforms页面中合并MVC也很有效,尽管JS验证可能存在一些小问题。我强烈推荐它。
Its fairly easy to start with creating an MVC application (5 atm) and then after you get the basic template up and running add the old webforms inside a folder. This way you get the new MVC setup correctly and it retains backwards compatibility.
创建一个MVC应用程序(5 atm)非常容易,然后在创建基本模板并在文件夹中运行后添加旧的webforms。通过这种方式,您可以正确地获得新的MVC设置,并保持向后兼容性。
- You can create a MVC project and just copy all the Webforms pages into e.g. a folder. (possible to just upgrade a project too, I'd imagine)
- 你可以创建一个MVC项目,将所有的Webforms页面复制到一个文件夹中。(我想也可以升级一个项目)
- Setup routing to ignore requests with that particular foldername in the URL. This way MVC and webforms can be used together without hassle in the URLs.
- 设置路由以忽略URL中具有特定foldername的请求。这样,MVC和webforms就可以一起使用,而不会在url中出现麻烦。
- Asp.net WebForms aspx pages can reside within MVC as long as they're not placed inside the /Views folder.
- Asp.net WebForms aspx页面可以驻留在MVC中,只要它们不在/Views文件夹中。
- Go with Razor over the old syntax, you can customize HTML helpers easily and it's a lot cleaner.
- 使用Razor对旧语法进行改进,您可以轻松地定制HTML helper,而且它更简洁。
- Check into MVC standards and conventions, the controller should be very lightweight. It's not the code-behind which can contain as much logic as you need.
- 检查MVC标准和惯例,控制器应该是非常轻量级的。它不是代码背后可以包含你需要的足够多的逻辑。
- Views shouldn't contain more than presentation logic (no business logic)
- 视图不应该包含多于表示逻辑(没有业务逻辑)
- Everything new => MVC
- 一切新的= > MVC
- Resources, web.config etc. are usable by both
- 网络资源。配置等都可以使用
- Menus don't use the old sitemaps in MVC
- 菜单不使用MVC中的旧站点地图
Using MVC inside a webforms .aspx page:
使用MVC内部的webforms。aspx页:
The text below is meant to demonstrate how you can utilize MVC from inside a webforms page. (For example inside MyPage.aspx) You can use MVC actions/views inside webforms with e.g. ajax to fill a portion of the page or certain div's.
下面的文本旨在演示如何在webforms页面中使用MVC。(例如在MyPage.aspx中)您可以在webforms中使用MVC操作/视图,例如使用ajax填充页面的一部分或某些div。
Webforms containing MVC works fine, at least when adding an ajax call inside the HTML to fill a div from MVC.
包含MVC的web表单工作得很好,至少在HTML中添加ajax调用来填充MVC中的div时是如此。
Inside an .aspx
在一个是
..html & webforms code
<div id="fillMeFromMVC">
<script type="text/javascript">
$.ajax(... call an MVC action to fill
the "fillMeFromMVC" div that this script sits inside of);
</script>
</div>
This will fill a portion of the page via MVC and you can cleanly do your MVC without worrying about what's done in webforms.
这将通过MVC填充页面的一部分,您可以干净地使用MVC,而不必担心在webforms中做什么。
WebForms vs. MVC:
WebForms与MVC:
By this point you are likely pretty aware of the differences between the two technologies, however here is a little comparison between them. They both have their purposes and uses. I personally prefer MVC for the things I have needed to do, however it likely depends on what you are trying to achieve.
至此,您可能已经非常清楚这两种技术之间的差异,但是这里有一些它们之间的比较。他们都有自己的目的和用途。我个人更喜欢用MVC来做我需要做的事情,但是这可能取决于您想要实现什么。
If you use e.g. a SOA behind them both the webforms and MVC pages can utilize the same business logic. Webforms can be detrimental when the code is all in the page behind and is tied to the UI (no separation of concerns). With a solid architecture and a bit of effort that can be reduced though.
如果你在它们后面使用SOA,那么webforms和MVC页面都可以使用相同的业务逻辑。当代码都在页面后面并且与UI绑定时,Webforms就会变得有害(没有关注点分离)。使用一个坚实的体系结构和一点可以减少的工作量。
Further reading:
进一步阅读:
Webforms vs. MVC (Code project)
Webforms vs. MVC(代码项目)
Difference betweeen ASP.NET WebForms and ASP.NET MVC (a blog)
ASP共用的区别。净WebForms和ASP。净MVC(博客)
#1
16
Update 2014:
2014年更新:
Visual Studio 2013 brings us closer to One ASP.NET. There’s no MVC project type or Web Forms project any longer, there’s just ASP.NET. If you want to mix Web Forms and Web API, or MVC and SignalR, go ahead! You are encouraged and supported. New features and functionality are brought in with NuGet without breaking existing apps.
Visual Studio 2013让我们更接近一个ASP.NET。不再有MVC项目类型或Web表单项目,只有ASP.NET。如果您想混合Web表单和Web API,或者MVC和SignalR,请继续!你受到鼓励和支持。在不破坏现有应用的情况下,NuGet引入了新的功能和功能。
和13
So the mixing of Web Forms and MVC (which has been pretty much been working seamlessly) is now encouraged and the boundaries are obfuscated. I guess MS recognized the need to allow projects to slowly migrate to MVC, or just migrate portions as needed, and gain the best of both worlds.
因此,Web表单和MVC(几乎一直无缝地工作)的混合现在得到了鼓励,边界也变得模糊。我猜MS认识到需要允许项目缓慢地迁移到MVC,或者只是根据需要迁移部分,并在这两个方面都取得最好的效果。
MVC Project:
MVC项目:
Having used MVC with Web forms for quite some time it is certainly possible and an excellent choice. New functionality and pages can be easily added into MVC pages with good architectural design such as DDD (Service Repository, Dependency Injection etc.) and the old stuff can stay as it is. Combining MVC inside a webforms page also works fine, although there can be some minor issues with JS validation. I would highly recommend it.
使用MVC与Web表单已经有一段时间了,这当然是可能的,也是一个很好的选择。新的功能和页面可以很容易地添加到具有良好架构设计的MVC页面中,比如DDD(服务存储库、依赖注入等等),而旧的东西可以保持原样。在webforms页面中合并MVC也很有效,尽管JS验证可能存在一些小问题。我强烈推荐它。
Its fairly easy to start with creating an MVC application (5 atm) and then after you get the basic template up and running add the old webforms inside a folder. This way you get the new MVC setup correctly and it retains backwards compatibility.
创建一个MVC应用程序(5 atm)非常容易,然后在创建基本模板并在文件夹中运行后添加旧的webforms。通过这种方式,您可以正确地获得新的MVC设置,并保持向后兼容性。
- You can create a MVC project and just copy all the Webforms pages into e.g. a folder. (possible to just upgrade a project too, I'd imagine)
- 你可以创建一个MVC项目,将所有的Webforms页面复制到一个文件夹中。(我想也可以升级一个项目)
- Setup routing to ignore requests with that particular foldername in the URL. This way MVC and webforms can be used together without hassle in the URLs.
- 设置路由以忽略URL中具有特定foldername的请求。这样,MVC和webforms就可以一起使用,而不会在url中出现麻烦。
- Asp.net WebForms aspx pages can reside within MVC as long as they're not placed inside the /Views folder.
- Asp.net WebForms aspx页面可以驻留在MVC中,只要它们不在/Views文件夹中。
- Go with Razor over the old syntax, you can customize HTML helpers easily and it's a lot cleaner.
- 使用Razor对旧语法进行改进,您可以轻松地定制HTML helper,而且它更简洁。
- Check into MVC standards and conventions, the controller should be very lightweight. It's not the code-behind which can contain as much logic as you need.
- 检查MVC标准和惯例,控制器应该是非常轻量级的。它不是代码背后可以包含你需要的足够多的逻辑。
- Views shouldn't contain more than presentation logic (no business logic)
- 视图不应该包含多于表示逻辑(没有业务逻辑)
- Everything new => MVC
- 一切新的= > MVC
- Resources, web.config etc. are usable by both
- 网络资源。配置等都可以使用
- Menus don't use the old sitemaps in MVC
- 菜单不使用MVC中的旧站点地图
Using MVC inside a webforms .aspx page:
使用MVC内部的webforms。aspx页:
The text below is meant to demonstrate how you can utilize MVC from inside a webforms page. (For example inside MyPage.aspx) You can use MVC actions/views inside webforms with e.g. ajax to fill a portion of the page or certain div's.
下面的文本旨在演示如何在webforms页面中使用MVC。(例如在MyPage.aspx中)您可以在webforms中使用MVC操作/视图,例如使用ajax填充页面的一部分或某些div。
Webforms containing MVC works fine, at least when adding an ajax call inside the HTML to fill a div from MVC.
包含MVC的web表单工作得很好,至少在HTML中添加ajax调用来填充MVC中的div时是如此。
Inside an .aspx
在一个是
..html & webforms code
<div id="fillMeFromMVC">
<script type="text/javascript">
$.ajax(... call an MVC action to fill
the "fillMeFromMVC" div that this script sits inside of);
</script>
</div>
This will fill a portion of the page via MVC and you can cleanly do your MVC without worrying about what's done in webforms.
这将通过MVC填充页面的一部分,您可以干净地使用MVC,而不必担心在webforms中做什么。
WebForms vs. MVC:
WebForms与MVC:
By this point you are likely pretty aware of the differences between the two technologies, however here is a little comparison between them. They both have their purposes and uses. I personally prefer MVC for the things I have needed to do, however it likely depends on what you are trying to achieve.
至此,您可能已经非常清楚这两种技术之间的差异,但是这里有一些它们之间的比较。他们都有自己的目的和用途。我个人更喜欢用MVC来做我需要做的事情,但是这可能取决于您想要实现什么。
If you use e.g. a SOA behind them both the webforms and MVC pages can utilize the same business logic. Webforms can be detrimental when the code is all in the page behind and is tied to the UI (no separation of concerns). With a solid architecture and a bit of effort that can be reduced though.
如果你在它们后面使用SOA,那么webforms和MVC页面都可以使用相同的业务逻辑。当代码都在页面后面并且与UI绑定时,Webforms就会变得有害(没有关注点分离)。使用一个坚实的体系结构和一点可以减少的工作量。
Further reading:
进一步阅读:
Webforms vs. MVC (Code project)
Webforms vs. MVC(代码项目)
Difference betweeen ASP.NET WebForms and ASP.NET MVC (a blog)
ASP共用的区别。净WebForms和ASP。净MVC(博客)