|
|
Want to know more about Packt's Article Network? Interested in contributing your article ideas? Please visit our FAQ for more information. See More BROWSE
All Titles WordPress Web Services SOA BPEL Web Graphics & Video Web Development RAW Portugues, Espanol, Italiano, French PHP/MySQL Oracle Open Source Networking & Telephony Moodle Microsoft & .NET Linux Servers jQuery Joomla! JBoss Java e-Learning e-Commerce Dynamics Drupal CRM Cookbook Content Management Beginner Guides Architecture and Analysis AJAX Future Titles Recently Published Titles When downloading and installing the ASP.NET MVC framework SDK, a new project template is installed in Visual Studio—the ASP.NET MVC project template. This article by Maarten Balliauw describes how to use this template. We will briefly touch all aspects of ASP.NET MVC by creating a new ASP.NET MVC web application based on this Visual Studio template. Besides view, controller, and model, new concepts including ViewData—a means of transferring data between controller and view, routing—the link between a web browser URL and a specific action method inside a controller, and unit testing of a controller are also illustrated in this article. See More |
Mixing ASP.NET Webforms and ASP.NET MVC
Not every ASP.NET MVC web application will be built from scratch. Several projects will probably end up migrating from classic ASP.NET to ASP.NET MVC. The question of how to combine both technologies in one application arises—is it possible to combine both ASP.NET Webforms and ASP.NET MVC in one web application? Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET. There's actually only one crucial difference: ASP.NET lives in System.Web, whereas ASP.NET MVC lives in System.Web, System.Web.Routing, System.Web.Abstractions, and System.Web.Mvc. This means that adding these assemblies as a reference in an existing ASP.NET application should give you a good start on combining the two technologies. Another advantage of the fact that ASP.NET MVC is built on top of ASP.NET is that data can be easily shared between both of these technologies. For example, the Session state object is available in both the technologies, effectively enabling data to be shared via the Session state. Plugging ASP.NET MVC into an existing ASP.NET applicationAn ASP.NET Webforms application can become ASP.NET MVC enabled by following some simple steps. First of all, add a reference to the following three assemblies to your existing ASP.NET application:
After adding these assembly references, the ASP.NET MVC folder structure should be created. Because the ASP.NET MVC framework is based on some conventions (for example, controllers are located in Controllers), these conventions should be respected. Add the folder Controllers, Views, and Views | Shared to your existing ASP.NET application. The next step in enabling ASP.NET MVC in an ASP.NET Webforms application is to update the web.config file, with the following code: < ?xml version="1.0"?> Note that your existing ASP.NET Webforms web.config should not be replaced by the above web.config! The configured sections should be inserted into an existing web.config file in order to enable ASP.NET MVC. There's one thing left to do: configure routing. This can easily be done by adding the default ASP.NET MVC's global application class contents into an existing (or new) global application class, Global.asax. using System; This code registers a default ASP.NET MVC route, which will map any URL of the form /Controller/Action/Idinto a controller instance and action method. There's one difference with an ASP.NET MVC application that needs to be noted—a catch-all route is defined in order to prevent a request for ASP.NET Webforms to be routed into ASP.NET MVC. This catch-all route looks like this: routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");This is basically triggered on every request ending in .aspx. It tells the routing engine to ignore this request and leave it to ASP.NET Webforms to handle things. With the ASP.NET MVC assemblies referenced, the folder structure created, and the necessary configurations in place, we can now start adding controllers and views. Add a new controller in the Controllers folder, for example, the following simpleHomeController: using System.Web.Mvc; The above controller will simply render a view, and pass it a message through the ViewData dictionary. This view, located in Views | Home | Index.aspx, would look like this: <%@ Page Language="C#" The above view renders a simple HTML page and renders the ViewData dictionary's message as the page title.
ASP.NET MVC 1.0 Quickly
Plugging ASP.NET into an existing ASP.NET MVC applicationThe road to enabling an existing ASP.NET MVC web application to serve ASP.NET Webforms contents is actually quite easy. Because the ASP.NET MVC framework is built on top of ASP.NET Webforms, any classic web form will automatically be available from an ASP.NET MVC web application. This means that any ASPX file will be rendered using ASP.NET Webforms, unless the route table contains a matching route for handling an ASP.NET MVC request. To avoid strange results in a mixed application, consider adding a catch-all route to the route table for ASPX pages, which will ignore any requests to ASP.NET Webforms, and will route only ASP.NET MVC requests. This can be done in the global application class, Global.asax. using System; The above code registers a default ASP.NET MVC route, which will map any URL in the form /Controller/Action/Id into a controller instance and action method. The catch-all route for ASP.NET Webforms looks like this: routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");Sharing data between ASP.NET and ASP.NET MVCWhether you are creating a new mixed ASP.NET Webforms-ASP.NET MVC application—or doing a migration, the chances are that you will need to share data between the two technologies. For example, a form can be posted by an ASP.NET Webforms page to an ASP.NET MVC action method. Because the ASP.NET MVC framework is built on top of ASP.NET Webforms, the following objects are always available in both technologies:
This way, it is easy to set a Session state item in classic ASP.NET Webforms and read it in ASP.NET MVC. using System; The above code is a "codebehind" for a classic ASP.NET page. As you can see, it sets the SharedData dictionary item of Session to a string value. This data can easily be read easily in an ASP.NET MVC controller, for example: using System.Web.Mvc; The Index action method tries to read data from the SharedData dictionary item of Session. If it has been set by ASP.NET Webforms or ASP.NET MVC, it is assigned to the ViewData dictionary. In other cases, an empty string is passed in. Building views at compile timeBy default, the views in ASP.NET MVC applications are built the first time that a request comes in. This means that if there is a compilation error in a view, it will only be visible the first time that a user requests for that view to be rendered. To overcome this issue, views can be compiled whenever the ASP.NET MVC application is compiled. To build views at compile time, the following steps should be executed:
Enabling view compilation may add some extra time to the build process. It is recommended that this is not enabled during development as a lot of compilation is typically involved during the development process. SummaryIn this article, we mixed ASP.NET Webforms and ASP.NET MVC in one application and shared data between both these technologies. We also added a post-build action to a project file to make sure that all views can be compiled. If you have read this article you may be interested to view : ASP.NET MVC 1.0 Quickly
About the AuthorMaarten Balliauw has a Bachelor's Degree in Software Engineering and has about eight years of experience in software development. He started his career during his studies where he founded a company that did web development in PHP and ASP.NET. After graduation, he sold his shares and joined one of the largest ICT companies in Belgium, RealDolmen, where he continued web application development in ASP.NET and application life cycle management in Visual Studio Team System. He is a Microsoft Certifi ed Technology Specialist in ASP.NET and works with the latest Microsoft technologies such as LINQ and ASP.NET 3.5. He has published many articles in both .NET and PHP literature, such as MSDN magazine Belgium, and PHP Architect. Ever since the fi rst announcement of the ASP.NET MVC framework, he has been following all its developments and features. He can be contacted on his blog-http://blog.maartenballiauw.be or by email at maarten@maartenballiauw.be. Books from Packt |
TOP TITLES ![]()
One of the driving goals for the ASP.NET MVC framework has been to create a flexible framework in which every component can be extended or replaced by a custom solution, whether developed by you or obtained from a third-party vendor. In this article by Maarten Balliauw, you will learn how you can customize and extend the ASP.NET MVC framework. See More |
| ||||||||