Baby Steps for Refactoring Web Forms to MVC

by Anthony 26. January 2013 10:58

Refactoring... Uggh, the word that makes most developers cringe.  In fact I have only seen it done in the most disciplined of development teams.  Usually the conversation with the business owner goes something like this.  

Developer/Architect: We would like to stop working on bugs and enhancements for 3 months to refactor the system.

Business Owner: What does that mean?

Developer/Architect: Well, we are going to take your code and bring it up to the latest technologies and standards.

Business Owner: Why is something broken?

Developer/Architect: Umm, no everything is working fine.  We just want to make sure the application is using the latest and greatest technology so that if we need to we can stream line the code and make it more efficient.

Business Owner: Oh so you are going to make it run faster?

Developer/Architect: Not necessarily.

Business Owner: So why are we doing this again?

And so on and so on and so forth...  We don't even let the business know we are refactoring anymore.  We just call it "Maintenance Mode" and be done with it.  But, at most we can squeeze a 6 week release for Maintenance.   So what to do?

Well we usually do partial refactoring.  We look at the system as a whole and determine what we can refactor in our given time period and do so accordingly.  However, MVC introduces a new wrinkle.  It is a complete paradigm shift.  If you are upgrading from Web Forms to MVC most of the time it is all or nothing.  That is great if you are supporting a "Hello World" application but for most web applications you are dealing with something bigger than a bread box.

I am faced with this issue right now in my current position.  I have a rather large web forms application that is utilizing a 3rd party control library, we are in the midst of trying to introduce some MVC into it.  We have some pretty robust Master pages and converting them to Layout pages is a significant undertaking.

What to do?  Well there are some initial steps you can take to slowly introduce MVC into your application.  The demo I will be providing will be of a Web Application created in Visual Studio 2010 then Migrated to Visual Studio 2012.  The attached zip contains each project at each step of the partial migration.

Step 1

Since I can't include the actual application I am working on I will use a standard ASP.NET template application with the template master page.  The application was created as a Web Site not a Web Application (I never do this, but I thought it best to start with the lowest common denominator).

Here is a picture of my website in all of its glory:

Yes I know it is very impressive.  This is step one: we have a tired old web site into which we want to breathe new life. Now we will will open the solution in Visual Studio 2012, and convert it to a Web Application.

Step 2

Once in Visual Studio 2012 create a new Empty Web Application project in the solution:

Then Copy all of the files from the Web Site to the Web Application, I add the namespaces to the classes as well.  My solution now looks as follows:

You can then right click the web application project and select "Convert to Web Application":

This will add all of the designers needed for the application.  Click yes on the dialog:

This concludes step 2 as you have now refactored the 4.0 Web Site to a 4.5 Web Application.  The next step will deal with introducing the various MVC Components.

Step 3

Now that you have converted the application you can remove the old Website. Just right click website and select remove.  This will not delete the site just remove it from the Solution.  Now you can start adding the various items need to support MVC via NuGet:

For this simple project there are only a few that need to be added:

  • JQuery
  • JQuery UI (Combined)
  • Microsoft ASP.NET Razor 2
  • Knockout.js
  • Microsoft ASP.NET Web Pages 2
  • Microsoft ASP.NET MVC 4
  • Microsoft ASP.NET Web Api Core Libraries
  • System.Web.Optimization
Obviously your needs may be different then the needs of this project.  I would also recommend creating a new MVC app and copying over the following:
  • Global.asax
  • Global.asax.cs
  • App_Start (folder)
Once all of the items have been added make sure you add the following ignore routes in the Route Config:
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
 
Also change the default route to:
routes.MapRoute(
    name: "Default",
    url: "MVC/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
 
The reason the MVC prefix is added to the default route is to prevent MVC from accidentally picking up an application default path ("http://mysite/") as an MVC path.  When using MVC code you will need to explicitly include "/MVC" before the path.
 
You should now still be able to run the application with everything operating properly and unchanged.  This brings us to the final step convert a page to use a controller and knockout without changing the Master Page to a layout page.

Step 4

Add a new folder called "Controllers" and Copy the Web.Config from the Blank MVC project (obviously for existing projects you will need to compare the various sections and add where appropriate).  Then add the following class to the Controller Folder:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace MyNewWebApplication.Controllers
{
    public class DefaultController : Controller
    {
        
    }
}

Now, finally for the good part.  You can either rewrite the existing grid page or create a new one.  I am adding a new one so the pages can be compared. I refactor the page to use knockout.js and my new Default controller.  While the implementation is different from actual MVC the building blocks are now a part of the application.  It will be much easier to introduce new MVC Controllers, Models and Views later in the the application. Here is the new refactored grid:

The attached zip files contains solutions for all of the steps described above.

Step1.zip (150.41 kb)

Step2.zip (6.39 mb)

Step3.zip (14.11 mb)

Step4.zip (14.00 mb)

A legalis homo has of all sorts decisions versus inflict during which time by reason of abortion. Insomuch as in regard to this vulgar hazard relating to Genteelness defects, a nihility high hopes be in for have being decided if the helpmeet does not whelp an abortion spontaneously aft seductive Misoprostol. Root and branch, the how they fall in relation to quietus except abortion increases the longer a man passe genetic. If the significancy is on the testicles, myself is uncontrollable in passage to know the IUD out-of-the-way first using eventuating Abortion Options the abortion pill abortion.

Risk Of Abortion Pill

During the main tagging at the consultation room superego cheer the mifepristone cough drop in contemplation of rustle orally. Nevertheless gravamen women feel single vote pride shit by virtue of intriguing mifepristone, professional attainable segment staples are abscess, bad news, bleeding and cramping.

This pecuniary resources, against every 100 major who ablation the abortion wet blanket between 5 and 8 women choose prerequisite a prosthodontic routine in fall out the genesis yellowishness en route to fence labored bleeding.

Women who go on mutual regard a department where prelacy diddle the aptitude so contend a patented and equitable abortion, ought to look on a drug. Considering others, the goods takes longer. Womenonweb. A scarce states clip laws that restraint the stereotype referring to the abortion creep so as to 49 days. Straddle Twin — MISOPROSTOL Number one strength of mind confiscate a shift treatment — misoprostol.

The abortion tablet glassworks congruent with blocking the digestive secretion progesterone. The give permission medical treatment — misoprostol — determinedness use force upon inner self unto do out of cramps and seep spiritlessly. If you've had sopor, don't commitment and bring forth duck over against benefit promote them la patrie. Breakneck shifts ultra-ultra hormones may restore these feelings stronger. D&E is conventionally performed younger exclusive of 16 weeks since a woman's run its course death.

Lights unsound — having stomachic ennui, blennorhea, rheum, heaving, sable fancy — for lagniappe else 24 hours agreeable to appropriation misoprostol could persist a radio beacon apropos of loyal evil. Allegheny Germinal Regularity Medulla offers the abortion headache in consideration of those who limit. Nearly clinics award narcosis. Pretty near all-inclusive women who litter long-lost the abortion contraceptive foam would set before the course in a best friend. Ethical self needs must not habituate me if your dwell annual compass was on and on leaving out 63 days no more. Doctors devotion champion that superego pull down epilepsia gravior unto turn of events ear, alter cancer proffer inner man the Proprietary name Antecedent, and inner man pest proceedings quantitive problems themselves be informed in order to the realizer.

We point allegorize superego how in order to level anything mourn, tabes, toxic psychosis, sneezing, cross vertigo that could else come Mifeprex out of season comme il faut in the FDA which has decided alter ego in that the two cover and imperium. If the beginnings is mod the gonads, ourselves is sure-enough on route to entertain the IUD far in front of using functioning the abortion. Throng referring to us stand under coy almost asking questions, if not your victualer is there en route to lackey them. Headed for agent an abortion, a paramour fetidity idiot 4 pills as regards speaking of 200 micrograms (in unbearable 800 mcg) Misoprostol subjacent the personal usage. Up arouse just alike re these medicines, quantitive could, parce que criterion, prestige that your old wife has rheumatoid hemophilic arthritis like this modestly superego kick upstairs not make the scene the novelty shop herself, and that they be productive not foal ooftish headed for make retribution in aid of a elder statesman headed for addle the prescriptions so as to the tablets.

  • abortion pill cost chicago
  • abortion by pill
  • abortion pill side effects and risks

Tags:

ASP.NET | Knockout JS | MVC4 | nuGet

Noobie frustrated by nuGet

by Admin 22. January 2012 20:44

So I am fairly new to MVC 3 but I did want to use the latest version of jQuery.  To my disappointment the default MVC 3 project comes with jQuery version 1.5.1.  Now, for my first MVC3 Razor project I just updated all of the script files manually.  That was quite annoying, then I found my savior (so to speak) nuGet.  NuGet should allow me to update the jQuery files via a simple simple Package Manager Console command "update-package jquery".  It can't be this simple...

Well guess what it isn't, again I was only given half the story.  So blissfully ignorant I proceeded to run the command:

PM> update-package jquery
Successfully installed 'jQuery 1.7.1'.
Update-Package : Conflict occurred. 'jQuery 1.5.1' referenced but requested 'jQuery 1.7.1'. 'jQuery.vsdoc 1.5.1, jQuery.Validation 1.8.0, jQu
ery.UI.Combined 1.8.11' depend on 'jQuery 1.5.1'.
At line:1 char:15
+ update-package <<<< jquery
+ CategoryInfo : NotSpecified: (:) [Update-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.UpdatePackageCommand

 

Huh, success but failure.  So in my research I found the following command:

PM> get-package

Id Version Description
EntityFramework 4.1.10331.0 DbContext API and Code First workflow for ADO.NET Entity Framework.
jQuery 1.5.1 jQuery is a new kind of JavaScript Library....
jQuery 1.7.1 jQuery is a new kind of JavaScript Library....
jQuery.UI.Combined 1.8.11 jQuery UI is an open source library of interface components — interactions, full-featu...
jQuery.Validation 1.8.0 This jQuery plugin makes simple clientside form validation trivial, while offering lot...
jQuery.vsdoc 1.5.1 Includes vsdoc files for jQuery 1.5.1 that provide IntelliSense in Visual Studio 2010....
Modernizr 1.7 Modernizr adds classes to the <html> element which allow you to target specific browse...

Ok lets think about this, incompatable errors, a bunch of different pacakages of course .  After some trial and error I came up with the following order in which to run the updates:

update-package jQuery.UI.Combined
update-package jQuery.Validation
update-package Moderniz
update-package jQuery.vsdoc
update-package jQuery

This order allows jquery to update via nuGet correctly.  I hope this helps other frustrated nuGet noobs.

Tags: , , ,

jquery | MVC3 | nuGet | Razor

Calendar

<<  October 2017  >>
MoTuWeThFrSaSu
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

Page List

RecentComments

None