ASP.NET MVC in SitefinityIvan Osmak, Sitefinity Product ManagerWhat is ASP.NET MVC?The ASP.NET MVC web application framework was introduced by Microsoft in March 2009. As of thiswriting, Microsoft is preparing to release the fourth iteration of this framework. ASP.NET MVC wascreated in response to growing criticism of ASP.NET WebForms, as well as evolving trends in webdevelopment. The web development community criticized ASP.NET WebForms on three major points:1. Problems with testability2. Lack of control over the markup3. Modifying the stateless nature of the web (HTTP) through ViewStateCriticisms aside, ASP.NET WebForms has been and is STILL a tremendously successful web frameworkthat will be heavily used in the years to come. In fact, one could argue that Microsoft was using ASP.NETMVC to address a growing demographic, rather than fundamentally shifting how .NET web applicationsare created.
The MVC pattern and its advantagesThe ASP.NET MVC pattern is Microsoft's take on a long-standing and well-known MVC design pattern.MVC stands for Model-View-Controller; in this design pattern there are three building blocks of anapplication and they are used to create clean separation between an application's layers.The MVC pattern's goal is to separate presentation (View), logic (Controller) and data (Model). Seasoneddevelopers will recognize the value of this proposal, but it's important to acknowledge that this is entirelypossible with ASP.NET WebForms. An ASP.NET WebForms application can enjoy similar benefits if sounddecisions are made when planning the application's architecture and implementation. However, becausethese layers are mandated by the MVC pattern it's harder to make a mistake.TestabilityIn ASP.NET WebForms the building block is a class (even class inherits Control class). Testingof types that derive from Control class is essentially impossible; this is due to the fact that the instance ofControl class requires an instance of class, which Microsoft has made sealed and almostimpossible to .Control Page HttpContext mock With advent of ASP.NET MVC, Microsoft has remedied this problem by introducing a new class calledwhich is abstract and therefore trivial to mock. This fact, together with the built-inseparation of concerns makes ASP.NET MVC building blocks much easier to test.HttpContextBaseControl over the markup outputOver the past ten years, we have witnessed the growing importance of web standards, proper markup andstyling. With the explosion in popularity of JavaScript,client side frameworks and AJAX, the ability to precisely
control the markup of web pages has becomeextraordinarily important. Total control over the markupin ASP.NET WebForms is impossible – and with a goodreason.Comparing ASP.NET MVC and ASP.NET WebFormsWhile it is quite simple to technically juxtapose the two frameworks, it's important to examine thepracticalities of everyday life. Namely, the great advantage of ASP.NET WebForms lays in the enormousinfrastructure and ecosystem built around it. In fact, ASP.NET WebForms could be compared toUnderstanding why ASP.NET WebForms does not provide greater levels of control over the markuprequires us to go back, more than ten years ago, to the release of ASP.NET 1.0. At that time, the mainproblems of the industry were quite different: poor support of web standards, almost every browserinterpreted markup, CSS and JavaScript differently and an army of developers trained to develop instateful environment (desktop). Microsoft offered a solution to these critical problems through itsASP.NET WebForms framework. The framework took care of the browser incompatibilities and mimickedstate (on a purely stateless HTTP protocol) through the ViewState feature – thus making it simpler forthousands of developers to embrace the ever growing need for web development. The abstraction –which we have all appreciated ten years ago and which made ASP.NET WebForms phenomenally popular– is what many criticize today. With browsers becoming highly standardized, developers having ten or soyears' experience on the web – we need the control back.ASP.NET MVC framework, together with its brilliant Razor rendering engine was the answer to thisgrowing criticism. With ASP.NET MVC, it is the developer that completely controls the markup as theabstractions provided by the Control class are gone. There is no more “runat=server” attribute, as theserver is not involved anymore. With developers starting to appreciate the stateless ways of HTTPprotocol, the ViewState is gone as well.petroleum powered cars, while ASP.NET MVC to electric cars. With all the advantages of electric cars, it'spractical to consider the numerous gas stations or mechanics that do not support this technology.Furthermore, in software there is no silver bullet. While, ASP.NET MVC makes a lot of sense for webpages, it's possible that web applications – especially ones not relying on JavaScript and AJAX – could stillbenefit greatly from WebForms.In the end, design patterns have been around for quite a while in software development. These patternstranscend environments and programming languages and an experienced developer will decide what touse based on what needs to be built – not on what's “cooler”.How can one use ASP.NET MVC within Sitefinity?Within Sitefinity, there are three ways in which ASP.NET MVC can be used. We call these three modes:hybrid, pure and classic.The first two modes are Sitefinity specific and bring ASP.NET MVC with a Sitefinity twist. Namely,Controllers are interpreted as widgets, meaning that you can build Sitefinity widgets from Controllers andactually have more than one Controller within a page. The classic mode, as the name suggests, is themode in which a page is represented by one controller (in essence, bypassing Sitefinity page routes) – asit is outside of the context of Sitefinity.You might be tempted to think that the first two modes are “hacks”, or at best anti-patterns, however thatis not the case. In the following paragraphs implementations of all modes will be explained in detail,Since v3.0, all the way to v5.0 Sitefinity has been exclusively ASP.NET WebForms application. With v 5.1,Sitefinity begins to natively support ASP.NET MVC. This news is bound to produce mixed and strongreactions, so it's perhaps best to offer the following statement first:Sitefinity 5.1 was not rewritten to ASP.NET MVC. Sitefinity will not ditch support for ASP.NETWebForms or decrease investment in it. The implementation of ASP.NET MVC in Sitefinity is not a“look alike”, but rather the actual ASP.NET MVC where appropriate extension points of theframework were taken advantage of.The three modes of ASP.NET MVCbut before we move on – let's quickly explore how and why this kind of revolutionary support is possiblewithin Sitefinity.The hybrid mode allows widgets based on Controls (ASP.NET WebForms)and Controllers (ASP.NET MVC) coexist on the same page.When using hybrid mode, a controller, model and view are created justas they would be created using classic ASP.NET MVC. The only differenceis that the Controller class is decorated with the ControllerToolboxItemattribute. When started, Sitefinity will automatically recognize thecontroller as a toolbox item and it will add it to the toolbox, as a widget.Sitefinity will represent this widget in the Page Editor through a Controlcalled MvcControllerProxy and it will display the content of the controllerduring Render method. However, this is valid only for the editA bit more than a year ago, when Sitefinity 4.0 was released –completely rewritten from the previous 3.Xversions – many architectural decisions were taken that made our support for ASP.NET MVC todaypossible.First and foremost, Sitefinity is using the . Microsoft initially developed this engine forASP.NET MVC and later extracted it so ASP.NET WebForms could also take advantage of it. However, thisRouting Engine has remained paramount with ASP.NET MVC and is an absolute requirement for any typeof real (not hacked or faked) support of ASP.NET MVC. Because Sitefinity already used this Routing Enginenatively, no changes were needed to meet this requirement.Secondly, in Sitefinity 4.1, Sitefinity natively implemented Virtual Path Provider approach for servingpages and content. Coincidently, this is the very same way in which ASP.NET MVC works, with its nativeimplementation – class.Experienced developers will see how Sitefinity provides the two key pieces for ASP.NET MVC support,even before it was officially announced. Namely, Sitefinity is able to work with routes and has extensiveextension points around handling HTTP requests through its native implementation of Routing Engine.On the other hand, through native implementation of Virtual Path Provider, Sitefinity is able to controlhow, what and when is being served to the client – in the very same way ASP.NET does.Routing EngineVirtualPathProviderViewEngineFlexible architecture of SitefinityHybrid – mixing ASP.NET WebForms and ASP.NET MVC within
mode. On the actual page, due to our usageof Virtual Path Provider, the contents of theView will be rendered directly into the page,without the proxy control.In the pure mode, Sitefinity will not allow adding Widgets based on ASP.NET WebForms to the page, butit will, however, allow composing the page through multiple Controllers – each representing one Widget.As a result, the rendered page will not have any ASP.NET WebForms artifacts (such as <formrunat=”server” or ViewState).It is important to note here, that this is not achieved through Partial Views, but rather full blownController-View-Model building blocks. Developers with experience in ASP.NET MVC may find this odd,as in ASP.NET MVC a page is represented by a Controller. In Sitefinity, we have moved this abstraction abit lower.Namely, Sitefinity's native route controls the pages and is responsible for instantiating the Widgets on it.Having this lower level abstraction, we are able to instantiate multiple controllers, execute them and havetheir output “baked” into the page by the time it reaches the Virtual Path Provider. If you reflectSystem.Web.Mvc assembly, you will quickly find out that this is exactly what ASP.NET MVC does – exceptit does it only for one controller. In the end, the only real difference is that Sitefinity provides a bit moreadvanced route registration system (and does that also through UI – page management) and a bit moreadvanced page composition mechanism through our implementation of Virtual Path Provider.Note: HTML specification does not allownested FORM tags. As in ASP.NET WebForms, allpage content must be enclosed inside of aFORM tag, thus a FORM tag cannot be usedinside your MVC based widgets when workingin hybrid mode. There is a simple solution tothis, instead of using the Html.BeginFormextension method, use theHtml.BeginFormSitefinity extension method.This method will recognize if the page is in thehybrid mode and will provide a workingsolution through JavaScript solution, while if thepage is in pure mode will output standardFORM tag. If you're building a page that heavilyuses forms, we suggest – for the sake of clarity -creating that particular page in “pure” mode.Pure – using multipleControllers within a pageClassic – using routes and controllers onlyClassic mode of using ASP.NET MVC with Sitefinity is simply an opened up possibility to use RouteTableto register routes and implement Controllers, just as one would do in the stand alone ASP.NET MVCapplication. More information on this mode can be taken directly from the ASP.NET MVC website.
There is only one thing to keep in mind here. The route that serves Sitefinity pages is registered beforethe ASP.NET MVC routes and will therefore have the priority should there be a collision between the tworoutes. This decision has been made consciously – with the thinking that developers will understand thecollision problem much more easily than would end users.Which mode should I use?There is no “best” mode that we can recommend. We have provided three modes as each of themprovides certain advantages and depending on the situation each of the modes may prove to be thebest.Hybrid mode provides the most flexibility. At the moment, Sitefinity does not provide pure MVC versionsof all the widgets; therefore should one decide to use MVC and yet have all the benefits of built-inSitefinity widgets one should go with the hybrid mode. Over the time, as Sitefinity provides more andmore pure MVC widgets, pages can be simply converted to the pure mode.If one is building a simple website or a very specific one, which is not covered by built in functionality –and at the same time fancies MVC – a pure mode should be chosen.The classic mode should generally be used only for existing or new applications that need to beintegrated with Sitefinity. One has to keep in mind that by using the classic mode, page management isdelegated to ASP.NET MVC and therefore, features such as pages, page editor, and layout editor will becompletely circumvented.Finally, it is very important to note that modes can be mixed. Some pages can work in the pure ASP.NETMVC mode, some in hybrid mode, some using only ASP.NET WebForms and still some routes can point tothe classic implementation of the ASP.NET MVC. The final decision is really left up to the developer – withno “best practice” being pushed by Sitefinity.Is Sitefinity abandoning ASP.NET WebForms?Short and long answer is: no.We do see benefits in using ASP.NET MVC in certain parts of application, just as we see the value inASP.NET WebForms.
In addition to this, Sitefinity will continue to deliver widgets based on ASP.NET WebForms, just as theirASP.NET MVC counterparts will be developed.We are fully aware of the enormous amount of code and functionality being implemented on theASP.NET WebForms framework for Sitefinity and we deem it impossible to simply declare this code as notsupported or in maintenance state.All this being said, there are no short, mid or long term plans to prefer either of the frameworks over theother one.How do I choose between ASP.NET MVC andASP.NET WebForms?Very often, when we provide alternate ways of achieving something we are asked what the “best practice”is. We cannot answer this question. It is like to ask us do we recommend using a car or an airplane: topick up a bottle of milk, we'd suggest a car; to visit a different continent, we'd recommend an airplane.The decision will and should depend on many different factors. How much does one value benefits ofASP.NET MVC as opposed to benefits of ASP.NET WebForms. If majority of your team is experienced inASP.NET WebForms, the proposition to move towards ASP.NET MVC may not be as convincing. Howmuch of the client side development you expect to do; perhaps the learning curve of ASP.NET MVC maypay off.Once again, we do not have a strong and definitive answer. Our mission is to empower people to buildweb sites and web applications productively. The more of choice we offer, the better we fulfill ourmission.Hybrid? De?velopment ?About the AuthorIvan Osmak is the Product Manager of Sitefinity and is responsible for aligning the nine teams ofSitefinity to push in same direction, ensure conceptual integrity of the product and finally make all thathappen on time. Being a developer with twelve years of experience, he also implements an occasionalfeature on the side, such as Module Builder or Mobile & Responsive Design.Ivan comes originally from Croatia, however for the last 15 or so years has lived on various places inUnited States, Austria and finally settled down in Sofia, Bulgaria, where he has been the member ofSitefinity team, in various roles, for last six years.Sitefinity Around the WorldNORTH AMERICABULGARIAUNITED KINGDOMGERMANYAUSTRALIA
About SitefinitySitefinity is a modern online business platform which adapts to any business requirement and worksequally well for online marketers, developers and IT managers. Sitefinity is used for all kinds of onlineapplications from commercial websites to community portals and intranets, and scales effortlessly nomatter the size of your project. Best of all, Sitefinity offers a simple, easy-to-use interface with a virtuallyflat learning curve that makes it a delight to use.Sitefinity powers more than 10,000 websites worldwide with a particularly strong presence in financialservices, government and education. Global brands such as Kraft Foods, Dannon, Magna, BASF, Toyota,Chevron and Yale School of Management rely on Sitefinity every day
转载于:https://www.cnblogs.com/0660jd/archive/2012/08/24/2654818.html
相关资源:数据结构—成绩单生成器