Accidental Technologist

Musings about Entrepreneurship, Technology and Software Development

  • Home
  • About
  • Still River Software
  • Privacy Policy

Powered by Genesis

You are here: Home / ASP.NET MVC / ASP.NET MVC Recent Code Drop Updates Cause Some Pain

ASP.NET MVC Recent Code Drop Updates Cause Some Pain

May 6, 2008 by Rob Bazinet

Tweet

Microsoft released the ASP.NET MVC Framework updates to CTP 2 in the middle of April and for those using it you may have noticed some changes to the way certain things are done. 

I am working on a project where I am using the CTP to get a web site built and learning ASP.NET MVC along the way.  I started using the CTP 2 for the project and moved over to the latest code drop as soon as it came out.  Sure, I expected some changes so it was not a big deal, but the ones I encountered were interesting and caused a little head scratching about something that worked before but not now.

Project Wizard

The CodePlex April 16 MVC Interim Source Code Release includes two downloads, the MVC code itself and a set of Visual Studio MVC Templates.  When I first saw the templates I figured they were just some additional ones but they are above and beyond those in the original CTP2.  You can see there are two templates now when creating a new project.

VS2008-NewProject

One is shown under Visual Studio installed templates, called ASP.NET MVC Web Application and the other one and newer is under My Templates and is also called ASP.NET MVC Web Application.  Choosing one over the other creates two very different sets of code.

One benefit of having two sets of templates is you can create applications for both sets of code bases, CTP2 and the interim drop.  One other thing that is different when using the new templates is you don’t get a nice test project to go in your solution.  It can be added of course.

Controllers

One of the changes which threw me off right away is the way Controller action methods have changed.  You can see from the screen shot just below that the controller action method Index() returns a void.

MVC-CTP2Controller

But the controller from the interim code drop controller action now returns an ActionResult, not a void.  This obviously breaks existing code.  Changing my existing code to work was not a big deal but was a surprise when trying the first build with the new code drop.

MVC-Controller-PostCTP

After fixing this code and heading over to Scott Guthrie’s blog, he explains the reasoning behind the changes:

The MVC feature team is experimenting with a few ideas in this week’s drop and are trying out some new ideas:

  1. Action methods on Controllers now by default return an "ActionResult" object (instead of void).  This ActionResult object indicates the result from an action (a view to render, a URL to redirect to, another action/route to execute, etc). 

  2. The RenderView(), RedirectToAction(), and Redirect() helper methods on the Controller base class now return typed ActionResult objects (which you can further manipulate or return back from action methods).

  3. The RenderView() helper method can now be called without having to explicitly pass in the name of the view template to render.  When you omit the template name the RenderView() method will by default use the name of the action method as the name of the view template to render.  So calling "RenderView()" with no parameters inside the "About()" action method is now the same as explicitly writing "RenderView(‘About’)".

So why change Controller action methods to return ActionResult objects by default instead of returning void?  A number of other popular Web-MVC frameworks use the return object approach (including Django, Tapestry and others), and we found for ASP.NET MVC that it brought a few nice benefits.

Please take a look at Scott’s post on this to see all of the benefits which I won’t go into here.

Routing and Default.aspx

In the CTP2 release there is a default.aspx page that was really just a placeholder page that had no code-behind, but building with the new bits causes the default route to not behave the same way and produce a page not found error.  In order to fix this some code behind is added to default.aspx which fixes the problem for CTP2 code.  Below is what I added and incidentally is the same code added by the new templates.

MVC-Defaultaspx

The changes are needed because of helper method called routes.MapRoute() which eases the syntax of creating routes.  Anyone who created routes with routes.Add will understand how it can be a bit simpler:

MVC-Routes 

The recent code drop (4/16) templates fix all these issues and new projects just work without having to deal with tweaking code.

Conclusion

I think these changes are keeping us on course in the right direction but are a couple of gotchas if you are not aware of them.

Technorati Tags: Microsoft,ASP.NET MVC

Share this:

  • LinkedIn
  • Twitter
  • Facebook
  • Email
  • More
  • Pinterest
  • Tumblr
  • Pocket
  • Reddit

Related

Filed Under: ASP.NET MVC

Care about your privacy? I do and use Fathom Analytics on this site.

Fathom Analytics

Recent Posts

  • How to Fix Rails Flash Rendering When Using Hotwire
  • Hotwire Fix for CORS Error when using Omniauth
  • Fix Installation of Ruby using rbenv on macOS Big Sur
  • RailsConf 2021 and the Future of Conferences
  • Fixing Out of Diskspace Errors on Amazon EC2

Categories

Services I Love

HatchBox - Easy Rails Deploys Fathom Analytics
Follow @rbazinet

Rob Bazinet (ruby.social/@rbazinet)
@rbazinet

  • Working to wrap up my current consulting gig by mid-April. I will be looking for the next thing soon. If anyone has… https://t.co/sg3cSV9yqM
    about 6 hours ago
  • I’ve been spending some time using Notion AI lately and it surprisingly good. Since all the rage lately is ChatGPT,… https://t.co/cUXFWIRCVV
    about 3 days ago
  • I think these steps are true for any target audience. https://t.co/0ht6rIzOW3
    about 3 days ago
  • I’ve been enjoying a show from the Magnolia network called The Craftsman. It’s mainly about woodworking but his phi… https://t.co/sWTUBbSsl6
    about 3 days ago
  • I found the resource I was looking for last night that is similar to https://t.co/Pyn0IIjRdP, it is… https://t.co/U7TMe77FMF
    about 3 days ago
  • RSS - Posts
  • RSS - Comments
Find me on Mastodon