First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
Do you Tweet? Follow me on Twitter @shanselman or learn how to use Twitter!
« Hanselminutes on 9 - The .NET Micro Fram... | Main | Hanselminutes on 9 - OmniTechNews - I wa... »

WARNING: This is obscure and you likely don't care.

As Phil mentioned a little while back, ASP.NET MVC didn't ship with Visual Studio 2010 Beta 1, but will ship with Visual Studio 2010 Beta 2 (and beyond.)

When VS10 Beta 2 comes out, you won’t need to download a separate standalone installer to get ASP.NET MVC (though a standalone installer will be made available for VS2008 users that will run on ASP.NET 3.5 SP1).

If you're using Visual Studio 2010 Beta 1 and want to use ASP.NET MVC, you can go get a drop of MVC for just this purpose, up on CodePlex.

This particular version of MVC is ONLY for use on Visual Studio 2010 Beta and is temporary. As such, it's got a placeholder version of 1.1, rather than 1.0.

If you have existing ASP.NET MVC 1.0 applications under .NET 3.5 lying around that you want to run them on VS 2010 Beta 1, you'll have to make some version number changes in your web.config.

I had a bunch of existing MVC apps lying around so I did this little upgrader application. It's poop, but it Works On My Machine©. Enjoy.

The code is here and is likely a blot on my soul. It will update the version numbers in the main web.config as well as the one in ~\views. It'll also make up to two backups, just in case.

The app is a .NET 4.0 Beta 1 Application and is zipped up here. You just run ASPNETMVCUpgrader "c:\path to your app"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ASPNETMVCUpgrader
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: ASPNETMVCUpgrader c:\\pathtomyprojectdir");
System.Environment.Exit(1);
}

//Remember there's TWO, count 'em, TWO web.configs. Madness!
TidyUpFile(Path.Combine(args[0], "web.config"));
TidyUpFile(Path.Combine(args[0], "Views\\web.config"));
}

private static void TidyUpFile(string filename)
{
string oldMvcType = "System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35";
string newMvcType = "System.Web.Mvc, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35";

string oldRoutingLine = "<add name=\"UrlRoutingModule\" type=\"System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35\"/>";
string newRoutingLine = "<add name=\"UrlRoutingModule\" type=\"System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35\"/>";

if (!File.Exists(filename))
{
Console.WriteLine(String.Format("Can't find {0}. You sure it's there?", filename));
System.Environment.Exit(1);
}

//Fix up the references!
string webconfigtext = File.ReadAllText(filename);
string newwebconfigtext = webconfigtext.Replace(oldMvcType, newMvcType);
newwebconfigtext = newwebconfigtext.Replace(oldRoutingLine, newRoutingLine);

//Keep up to two backups
string backupWebConfig = Path.ChangeExtension(filename, ".mvc10backup");
if (File.Exists(backupWebConfig))
{
var twoIsEnough = Path.ChangeExtension(filename, ".mvc10backup2");
File.Delete(twoIsEnough);
File.Copy(backupWebConfig,twoIsEnough);
File.Delete(backupWebConfig);
}
File.Copy(filename, backupWebConfig);

File.WriteAllText(filename, newwebconfigtext);

//Toot!
Console.WriteLine(String.Format("Updated MVC 1.0 references to MVC 1.1 for Dev10 Betas. \n Created a {0} backup. Toot.", filename));
}
}
}

Enjoy.

Extra Credit: If you'd like to show off for no reason, feel free to rewrite this application in as few lines as possible (but still in C#, Ruby people) and post in the comments.



Saturday, July 11, 2009 7:01:51 AM (Pacific Standard Time, UTC-08:00)
When you say "rewrite this application in as few lines as possible" I hope you didn't mean Perl style :-) keep it readable!
Sunday, July 12, 2009 11:54:48 AM (Pacific Standard Time, UTC-08:00)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ASPNETMVCUpgrader
{
class Program
{
static void Main(string[] args)
{
if (!(args.Length == 1 )) {Console.WriteLine("Usage: ASPNETMVCUpgrader c:\\pathtomyprojectdir"); System.Environment.Exit(1); }
//Remember there's TWO, count 'em, TWO web.configs. Madness!
TidyUpFile(Path.Combine(args[0], "web.config"),Path.Combine(args[0], "Views\\web.config"));
}
private static void TidyUpFile(params string[] filename)
{
if (!filename.All(fn => File.Exists(fn)) )
{
// If it don't exist, don't bother creating strings.
Console.WriteLine(String.Format("Can't find {0} or {1}. You sure it's there?", filename[0],filename[1]));
System.Environment.Exit(1);
}
for(int i = 0; i < 2; i++) {
//Fix up the references!
String MvcType = "System.Web.Mvc, Version=1.{0}.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35";
String Routing = "<add name=\"UrlRoutingModule\" type=\"System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version={0}.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35\"/>";
string webconfigtext = File.ReadAllText(filename[i]);
string newwebconfigtext = webconfigtext.Replace(string.Format(MvcType,"0"),String.Format(MvcType,"1")); //(Old, New)
newwebconfigtext = newwebconfigtext.Replace(string.Format(Routing, "3.5"), String.Format(Routing, "4.0")); //(Old, New)
//Keep up to two backups
string backupWebConfig = Path.ChangeExtension(filename[i], ".mvc10backup");
if (File.Exists(backupWebConfig))
{
var twoIsEnough = Path.ChangeExtension(filename[i], ".mvc10backup2");
File.Delete(twoIsEnough);
File.Copy(backupWebConfig, twoIsEnough);
File.Delete(backupWebConfig);
}
File.Copy(filename[i], backupWebConfig);
File.WriteAllText(filename[i], newwebconfigtext);
//Toot!
Console.WriteLine(String.Format("Updated MVC 1.0 references to MVC 1.1 for Dev10 Betas. \n Created a {0} backup. Toot.", filename));
}
}
}
}
Adam Speight
Sunday, July 12, 2009 11:17:55 PM (Pacific Standard Time, UTC-08:00)
I like how Adam did the strings. Here's a more functional approach:
        static void Main(string[] args)
{
if (args.Length != 1) { Console.WriteLine("Usage: ASPNETMVCUpgrader c:\\pathtomyprojectdir"); System.Environment.Exit(1); }
//Remember there's TWO, count 'em, TWO web.configs. Madness!
var messages = new[] { "web.config", @"Views\web.config" }
.Select(p => Path.Combine(args[0], p))
.Select(f => File.Exists(f) ? TidyUpFile(f) : String.Format("Can't find {0}. You sure it's there?", f));
foreach (var m in messages) Console.WriteLine(m);
}
private static String MvcType = "System.Web.Mvc, Version={0}.0.0,";
private static String Routing = "System.Web.Routing, Version={0}.0.0,";
private static string TidyUpFile(string filename)
{
//Fix up the references!
string newWebConfigText = File.ReadAllText(filename)
.Replace(string.Format(MvcType, "1.0"), String.Format(MvcType, "1.1")) //(Old, New)
.Replace(string.Format(Routing, "3.5"), String.Format(Routing, "4.0")); //(Old, New)
//Keep up to two backups
string backupWebConfig = Path.ChangeExtension(filename, ".mvc10backup");
if (File.Exists(backupWebConfig))
{
var twoIsEnough = Path.ChangeExtension(filename, ".mvc10backup2");
File.Delete(twoIsEnough);
File.Move(backupWebConfig, twoIsEnough);
}
File.Move(filename, backupWebConfig);
File.WriteAllText(filename, newWebConfigText);
//Toot!
return String.Format("Updated MVC 1.0 references to MVC 1.1 for Dev10 Betas. \n Created a {0} backup. Toot.", filename);
}

And a bonus: Unlimited* backups! (* within reason)
            var backupPaths = Enumerable.Repeat<int?>(null, 1)
.Concat(Enumerable.Range(2, int.MaxValue - 2).Select(i => (int?)i))
.Select(i => Path.ChangeExtension(filename, ".mvc10backup" + i));
string toCopy = filename;
foreach (var f in backupPaths.TakeWhile(_ => toCopy != null))
{
string temp = null;
if (File.Exists(f))
File.Move(f, temp = f + "TEMP");
File.Move(toCopy, f);
toCopy = temp;
}
File.WriteAllText(filename, newWebConfigText);

I don't like the TEMP hack, but it will do for now.
Sunday, July 12, 2009 11:18:32 PM (Pacific Standard Time, UTC-08:00)
Beta 1, Beta 2...
RC1, RC2.. ...
I like the RTM :)
Monday, July 13, 2009 5:00:10 PM (Pacific Standard Time, UTC-08:00)
Great rewrites! I love the use of LINQ. I'll need to do a post on your versions!
Tuesday, July 14, 2009 2:59:56 AM (Pacific Standard Time, UTC-08:00)
private static void TidyUpFileShort(string filename)
{
if (!File.Exists(filename)){
Console.WriteLine(String.Format("Can't find {0}. You sure it's there?", filename));
System.Environment.Exit(1);
}

string webconfigtext = File.ReadAllText(filename).Replace("System.Web.Mvc, Version=1.0.0.0,", "System.Web.Mvc, Version=1.1.0.0,").Replace("System.Web.Routing, Version=3.5.0.0", "System.Web.Routing, Version=4.0.0.0"); ;

if (File.Exists(Path.ChangeExtension(filename, ".mvc10backup"))){
File.Replace(filename, Path.ChangeExtension(filename, ".mvc10backup"), Path.ChangeExtension(filename, ".mvc10backup2"));
}
else{
File.Move(filename, Path.ChangeExtension(filename, ".mvc10backup"));
}

File.WriteAllText(filename, webconfigtext);

Console.WriteLine(String.Format("Updated MVC 1.0 references to MVC 1.1 for Dev10 Betas. \n Created a {0} backup. Toot.", filename));
}
Tomas
Wednesday, July 15, 2009 1:04:45 PM (Pacific Standard Time, UTC-08:00)
Here's my regex version:


static void Main(string[] args)
{
if (args.Length == 0) Console.WriteLine("Usage: ASPNETMVCUpgrader pathToProject1 [pathToProject2] [pathToProject3]");

Regex regex1 = new Regex(@"(?<1>System.Web.Mvc, Version=)1.0(?<2>.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35)", RegexOptions.Compiled);
Regex regex2 = new Regex(@"(?<1><add name=""UrlRoutingModule"" type=""System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=)3.5(?<2>.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"" />)", RegexOptions.Compiled);

foreach (var file in args
.Select(p => new { Both = new[] { Path.Combine(p, "web.config"), Path.Combine(p, @"Views\web.config") } })
.SelectMany(p => p.Both)
.Select(p => new { Path = p, Content = File.ReadAllText(p) }))
{
try
{
File.Move(file.Path, Enumerable.Range(0, 100).Select(p => string.Format("{0}.backup_{1:00}", file.Path, p)).First(p => !File.Exists(p)));
File.WriteAllText(file.Path, regex1.Replace(regex2.Replace(file.Content, "${1}4.0${2}"), "${1}1.1${2}"));
Console.WriteLine("Done converting: {0}", file.Path);
}
catch (Exception ex) { Console.WriteLine("Error with: {0}" + Environment.NewLine + "Exception: {1}", file.Path, ex.Message); }
}
}
Dušan Radovanović
Tuesday, August 04, 2009 5:39:30 AM (Pacific Standard Time, UTC-08:00)
yes th
Comments are closed.

Contact

Sponsors

Hosting By

Hot Topics

Tags

Calendar

<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

November, 2009 (3)
October, 2009 (19)
September, 2009 (11)
August, 2009 (12)
July, 2009 (21)
June, 2009 (26)
May, 2009 (16)
April, 2009 (13)
March, 2009 (17)
February, 2009 (17)
January, 2009 (18)
December, 2008 (32)
November, 2008 (17)
October, 2008 (22)
September, 2008 (16)
August, 2008 (14)
July, 2008 (25)
June, 2008 (19)
May, 2008 (17)
April, 2008 (17)
March, 2008 (26)
February, 2008 (21)
January, 2008 (28)
December, 2007 (19)
November, 2007 (17)
October, 2007 (31)
September, 2007 (39)
August, 2007 (37)
July, 2007 (43)
June, 2007 (37)
May, 2007 (32)
April, 2007 (38)
March, 2007 (29)
February, 2007 (46)
January, 2007 (31)
December, 2006 (27)
November, 2006 (31)
October, 2006 (32)
September, 2006 (39)
August, 2006 (34)
July, 2006 (40)
June, 2006 (18)
May, 2006 (31)
April, 2006 (34)
March, 2006 (30)
February, 2006 (38)
January, 2006 (44)
December, 2005 (19)
November, 2005 (34)
October, 2005 (24)
September, 2005 (37)
August, 2005 (20)
July, 2005 (24)
June, 2005 (33)
May, 2005 (16)
April, 2005 (22)
March, 2005 (34)
February, 2005 (15)
January, 2005 (37)
December, 2004 (28)
November, 2004 (30)
October, 2004 (34)
September, 2004 (22)
August, 2004 (34)
July, 2004 (18)
June, 2004 (64)
May, 2004 (49)
April, 2004 (21)
March, 2004 (29)
February, 2004 (29)
January, 2004 (36)
December, 2003 (25)
November, 2003 (24)
October, 2003 (59)
September, 2003 (42)
August, 2003 (24)
July, 2003 (44)
June, 2003 (29)
May, 2003 (21)
April, 2003 (30)
March, 2003 (27)
February, 2003 (47)
January, 2003 (50)
December, 2002 (31)
November, 2002 (38)
October, 2002 (44)
September, 2002 (15)
May, 2002 (2)
April, 2002 (4)

Google Ads