我可以使用ASP.NET MVC与常规ASP.NET Web表单 [英] Can I use ASP.NET MVC together with regular ASP.NET Web forms

查看:151
本文介绍了我可以使用ASP.NET MVC与常规ASP.NET Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经要求从客户端建立了一个庞大的网站使用ASP.NET Web表单。问题是,我发现ASP.NET Web表单是有点不直观的(我个人的口味只)。因此,我希望做的是使用MVC,但我真的不能指望我的客户在一个完全重写付出我的时间(也没有,他有足够的时间)。

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.NET MVC和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?

推荐答案

更​​新2014年:

的Visual Studio 2013使我们更接近一个ASP.NET。 有没有MVC   项目类型或Web窗体项目中的任何更长的时间,但只是ASP.NET。如果   你想混合Web窗体和Web API或MVC和SignalR,继续前进!   我们鼓励和支持。新的特性和功能   带来了与的NuGet而不会破坏现有的应用程序。

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.

VS 13

所以Web Forms和MVC混合(这一直是pretty的多少一直致力于无缝)现在鼓励的界限被模糊处理。我想MS认识到有必要使项目慢慢迁移到MVC,或只是部分迁移需要,并获得两全其美。

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.

MVC项目:

有使用MVC与Web表单相当长的一段时间,它是绝对有可能和一个很好的选择。新的功能和页面可以很容易地加入到MVC的网页具有良好的建筑设计,如DDD(服务信息库,依赖注入等)和旧的东西可以留下来,因为它是。结合MVC中的web表单页面内也能正常工作,虽然有可以与JS验证一些小问题。我会极力推荐它。

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应用程序(5大气压),然后后,你的基本模板和运行添加了旧web表单一个文件夹内。这样你正确地得到新的MVC设置,它保留了向后兼容性。

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项目,只是复制所有的Web表单页面转换成如一个文件夹。 (可以只升级一个项目也一样,我想像)
  • 设置路由忽略与在URL中特定文件夹名的请求。这种方式MVC和web表单可以一起使用而不麻烦中的URL。
  • 在Asp.net的WebForms的的aspx 的页面可以驻留在MVC,只要他们不置于/浏览文件夹内。
  • 前往剃刀在旧的语法,你可以轻松地自定义HTML佣工,这是一个很大的清洁剂。
  • 检查到MVC标准和惯例,控制器应该非常轻便。这不是code-后面,因为你需要它可以包含尽可能多的逻辑。
  • 在浏览次数应该不超过presentation逻辑(没有业务逻辑)
  • 含有较多
  • 在一切新的东西=> MVC
  • 在资源,web.config文件等都是由双方可用
  • 在菜单不使用旧的站点地图中的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)
  • 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.
  • Asp.net WebForms aspx pages can reside within MVC as long as they're not placed inside the /Views folder.
  • Go with Razor over the old syntax, you can customize HTML helpers easily and it's a lot cleaner.
  • 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.
  • Views shouldn't contain more than presentation logic (no business logic)
  • Everything new => MVC
  • Resources, web.config etc. are usable by both
  • Menus don't use the old sitemaps in MVC

使用MVC内的web表单.aspx页面中:

下面的文本是为了演示如何从一个web表单页面内使用MVC。 (例如内部MyPage.aspx)可以使用MVC的动作/视图里面的WebForms与如AJAX填写页面或某些DIV的一个部分。

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.

含有MVC添加HTML里面调用Ajax填补MVC一个div至少在正常工作,Web表单

Webforms containing MVC works fine, at least when adding an ajax call inside the HTML to fill a div from MVC.

在一个.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>

这将填补通过MVC的页面的一部分,你可以清晰地做你的MVC,而不必担心什么在web表单完成。

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.

的WebForms与MVC:

此时,您很可能pretty的意识到了这两种技术之间的差异,但是这里是他们之间的一个小比较。他们都有自己的目的和用途。我个人$ PFER MVC p $因为我需要做的事情,但它可能取决于你想要达到的目的。

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.

如果您使用如他们既WebForms和MVC页后面的SOA可以利用相同的业务逻辑。 Web表单可能是有害的时候,code是所有在后面的页面,并绑定到用户界面(关注没有分离)。凭借雄厚的架构和一点努力,虽然可以降低。

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.

延伸阅读:

Web表单与MVC(code项目)

<一个href="http://www.webdevelopmenthelp.net/2013/10/Difference-between-ASP.NET-WebForm-And-ASP.NET-MVC.html">Difference betweeen ASP.NET WebForms和ASP.NET MVC(一博客)

这篇关于我可以使用ASP.NET MVC与常规ASP.NET Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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