Part one was an in-depth look at upgrading ASP.NET MVC Preview 5 to the new Beta which you can view here.
In the above post I talked about the Microsoft Futures assembly and the fact that there was not one yet released for the Beta... Well, in true "Gu" style, Scott Guthrie has written an (another) awesome post on all the new changes and fixes to the ASP.NET MVC Beta, which includes a link to the new Microsoft Futures assembly on CodePlex.
Thanks Scott....it was the last thing I was needing in my MVC Preview 5 to Beta update.
By the way, I'm loving the new strongly typed "Add View..." from the controller. (if you don't know what this is check out Scott's post on the new release.
Well, I'm biting the bullet and I'm going to update my ASP.NET MVC Preview 5 project to the beta!
This Beta is an official release; as the previous "official" release was Preview 3. Lets see how it goes:
Step 1 - Install the Beta
This kind of goes without saying, but I this is a step by step guide.
Make sure all Instances of Visual Studio has been closed, Uninstall ASP.NET MVC Preview 5, then run the Beta Installer:
Start up Visual Studio and open your MVC project.
Step 2 - Update your assembly references
Update the following references in your web project to the new Beta dll's:
- System.Web.Abstractions.dll
- System.Web.Routing.dll
- System.Web.Mvc.dll
The default location for these are in: %ProgramFiles%\Microsoft ASP.NET\ASP.NET MVC Beta
Step 3 - Add web.config namespace and assembly
All the Html Helper extension methods have been moved to there own namespace: System.Web.Mvc.Html. You need to add this namespace to your web.config in order for the Html Helpers to show up in your views etc.
<namespaces>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="Microsoft.Web.Mvc"/>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Linq"/>
<add namespace="System.Collections.Generic"/>
</namespaces>
You'll also have to add this namespace if you use any of the input extensions in your own HtmlHelper extensions.
Because Mvc is now installed in the GAC I had to add the assembly to the compilation section:
<compilation debug="true">
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
</compilation>
Step 4 - Change Html.Form...maybe!
In this release the HtmlHelper Form has been changed to BeginForm. This returns a MvcForm which is IDisposable so you can still wrap it up in a using statement or use the Html.EndForm().
The reason that I've said "maybe" is that most of my forms use the HtmlHelper Form from the Microsoft Mvc Futures assembly. This allows me to use the nice Generic form with Linq:
<% using (Html.Form<HomeController>( x => x.Index() )) { %>
I prefer to use this way as it does not involve entering the controller and action as strings.
If you want to use this way just reference the Microsoft.Web.Mvc.dll which was made available in Preview 5. They say (Microsoft aka Phil Haack) that the MvcFutures will be available on CodePlex, but I have not found the new version since the Beta. Something to remember to update...
UPDATE (17-10-2008): See Part 2 for info on the Microsoft Futures for Beta.
Don't forget to add this to your namespace section in your web.config as above.
If you use FormMethod anywhere in your code you may get an ambiguous reference now....so just reference the correct one (in my case the one in the Microsoft Futures). Same goes for the FormCollection.
Step 5 - Update any Custom View Engines
The IViewEngine now was a new method that you need to implement:
public void ReleaseView(ControllerContext controllerContext, IView view)
{
throw new NotImplementedException();
}
Also the ViewEngineResult constructor now need an IViewEngine as well as the IView:
return new ViewEngineResult(new WebFormView(viewPath), new WebFormViewEngine());
Step 5 - Build your project
Check to see if you get an other compilation errors.
I got one with an ambiguous reference to the FormMethod Enum in one of my partial views.
Step 6 - Test your project.
My project seems to be working, but make sure you check all area's. Time to run your unit tests, intergration tests and acceptance test!
That's it for me...let me know if you come across any other changes that you have had to make.
Has anyone else noticed that Microsoft have released the beta version of ASP.NET MVC?
You can get it from here.
Happy MVC-ing....
Technorati Tags:
ASP.NET MVC,
Beta
Steve asked me in a previous post for a sample application for detecting a mobile device and rendering a mobile view in ASP.NET MVC.
You can download a sample MVC app here.
The app is the same as the ASP.NET MVC template that the "GU" put together, with the added mobile action filter and views.
I've also updated my mobile browser detection. Turns out not to be so straight forward as new phones are coming out all the time with different User Agents etc. This is the best "generic" way I could find:
if (filterContext.HttpContext.Request.Browser.IsMobileDevice ||
filterContext.HttpContext.Request.ServerVariables["HTTP_ACCEPT"].Contains("text/vnd.wap.wml") ||
filterContext.HttpContext.Request.ServerVariables["HTTP_ACCEPT"].Contains("application/vnd.wap.xhtml+xml")
)
Hope this help Steve, let me know how you get on.
Technorati Tags:
Mobile,
ASP.NET MVC
One of the great things about MVC is the ability to direct any Controller Action to any named View that you want.
This means that I'm able to detect wether the browser is a mobile device and direct them to the Mobile View:
public ActionResult Index()
{
if (Convert.ToBoolean(filterContext.HttpContext.Request.Browser["IsMobileDevice"]))
{
return View("Index_mobile")
}
return View();
}
This works great. My Index_mobile.aspx view has a Master Page (just like any other view) that sets up the doc type and styles, and I can render the view as normal. The doc type I used is:
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
Another way is to use an ActionFilterAttribute to detect if the browser is a mobile device and then append "_mobile" to the current view.
The benefit of using an action filter is that I can just mark up my Action Method or Controller with this attribute and it will automatically render a mobile view.
[MobileUserFilter]
public ActionResult Index()
{
return View();
}
public class MobileUserFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (Convert.ToBoolean(filterContext.HttpContext.Request.Browser["IsMobileDevice"]))
{
var action = (string)filterContext.RouteData.Values["action"];
var viewName = ((ViewResult) filterContext.Result).ViewName;
if (string.IsNullOrEmpty(viewName)) viewName = action;
var result = new ViewResult
{
ViewName = (viewName + "_mobile"),
MasterName = ((ViewResult) filterContext.Result).MasterName,
ViewData = ((ViewResult) filterContext.Result).ViewData,
TempData = ((ViewResult) filterContext.Result).TempData
};
filterContext.Result = result;
filterContext.HttpContext.Response.Clear();
}
base.OnActionExecuted(filterContext);
}
}
There we have it now we have a mobile site:
By the way, I'm testing my mobile site on Openwave Phone Simulator which you can download here. Does anybody use a better Phone Emulator? Let me know if you do, I'd love to try it out.
I few posts ago I mentioned a colleague Bob Thomson, and how he needed to get a blog started.
Well... he's actually listened to me! He's created a blog under Storm ID's (the company we both work for.) little sister.
Introducing:
Bob's blog is located at http://blog.stormideas.com/
Check it out; it will have loads of cool ideas for applications as well as cutting edge information on Silverlight.
Nice one Bob...look forward to reading your blog! :)
In the "olden days" of Web Forms, to render an image from the database you would have to create a HttpHandler.
<httpHandlers>
<add verb="GET" path="image.ashx" type="MyApp.Web.ImageHandler, MyApp.Web" validate="false"/>
</httpHandlers>
In ASP.NET MVC you can now use a Controller Action and View to handle an image. One of the benefits is that it can harness the power of System.Web.Routing and there are no web.config sections to maintain.
Step 1 - add a route
routes.MapRoute(
"MyImageHandler",
"image/{id}/{type}.png",
new {controller = "ImageHandler", action = "Image"}
);
Step 2 - create image handling controller
public class FeaturedBusinessesController : Controller
{
public ActionResult Image(string type, Guid id)
{
var myObject = CatalogService.GetMyObject(id);
byte[] imageData = null;
if(type == "large")
{
if(myObject.LargeImage != null)
imageData = myObject.LargeImage.Data;
}
else if(type == "thumb")
{
if(myObject.ThumbImage != null)
imageData = myObject.ThumbImage.Data;
}
if (imageData != null) Response.BinaryWrite(imageData);
return View();
}
}
Step 3 - create the view
<%@ Page Language="C#" ContentType="image/png" AutoEventWireup="true" CodeBehind="Image.aspx.cs" Inherits="MyApp.Web.Views.ImageHandler.Image" %>
NOTE: Notice that the ContentType of the page has been set to "image/png"
Step 4 - render image on your page
The image will now be viewable by going to: /image/8234633b-79b9-4500-a9c8-9b2100c80030/thumb.png
You can change the route however you want and pass in as many parameters to the Image method in order to get the image data back from the database (or any other storage).
Say goodbye to HttpHandlers !???
Technorati Tags:
ASP.NET,
MVC
My current development machine is Windows Server 2003. Because I'm doing a lot of ASP.NET MVC work, I want to be able to test my applications on IIS7.
I've been testing on Virtual Machines and production servers that are running Windows Server 2008, however this is just a bit of a pain while in development. The only way is to upgrade my OS.
The easiest way to upgrade would be to Server 2008, however there is licensing issues with using Server 2008 as a development machine. So my only option is Windows Vista.
One slight problem is that there is no upgrade path from Server 2003 to Vista. Looks like I'm going to have to wipe my machine and start from scratch....re-installing every single application that I use!
That is even more of a pain!
Does anyone have any good ideas that would make this transition a little less painful?
Technorati Tags:
IIS7,
Vista
I work for a web development company in Edinburgh called Storm ID (http://www.stormid.com). We love staying at the forefront of web technology; ASP.NET MVC, WCF, Silverlight....
Our company's Silverlight guru is Bob Thomson. "The Drum" (http://www.thedrum.co.uk) has published an article on Bob's thought's on Silverlight 2.0:
"Hi Ho Silver! Has Flash Had Its Day?"
http://www.thedrum.co.uk/indepth/1650-hi-ho-silver-has-flash-had-its-day-
Love the title Bob!
Bob discuses all the current Web technologies from Flex/Flash, HTML/JavaScript/AJAX, to Silverlight and Microsoft Mesh. Really interesting article.
All we need now is a Bob to set up his own blog so the rest of us can keep up with his "cutting edge" Silverlight development. "Come on Bob....how about it!"
The likelihood of you not knowing what Google Chrome is very very slim.... but just in case here is the link: http://www.google.com/chrome/
Must Have's
As a web developer my default browser must be able to handle certain developer needs (other than rendering the website/application correctly).
The most useful web developer tool is a very well known guru of an add-on for Firefox:
You guessed it Firebug!
So my first question is "What does Chrome offer?"
Goggle Chrome has it's very own "Inspector":
This is nice Google! Clearly based of Firebug, Chromes version has all the stuff (sorry for being so technical!) you would expect.
The UI is very much like the rest of the browser: clean, simple and nice to use. Far better that IE8's version of the Developer Toolbar.
To bring up the inspector you can either right click on any element on your web page and select "Inspect element" or select "Developer" -> "JavaScript console " from the Page menu
.
What else?
There are loads of feature in Google Chrome...and lots of features to come, but as a web developer it renders fast, looks great, works well...and therefore I setting Google Chrome as my default browser...for now.
What do you think?