Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Link building 101 with ASP.Net MVC

Because I was stumped on the various ways to build links (hyperlinks)with the new MVC framework, I thought I would save others the pain that I went through and show the various ways to build links.  You can also get this information by watching Scott Hanselman's screencasts on the frameworks as well.

There are 3 different ways to build links in the new framework.  Each one is effective, but each has its own pros/cons.

  • The quick and dirty way
    The MVC framework is based on HTML, so you could simply do the following:
    <a href="/SomeController/SomeAction/<%= o.Value %>"">Link</a>

    This method has one huge, glaring issue.  It is not refactor friendly.  If you ever wanted to change the name of the controller being called, or the action method being called you would have to do a find and replace. 

    This method is pure evil as it is littered with 'magic' strings.

  • The better way
    Another way to create link is to use the Html helper classes that are provided as part of the MVCTool kit (now just part of the MVC framework).  The helper method we want to use is the Html.ActionLink method.  The following is an example:
    <%=Html.ActionLink("Edit", "EditAction", new { id = o.Value } ) %>

    This method is better then the one above as you do not have to worry about the controller as it is implied (assuming you don't want to use a different controller then the one in the current path).  However, this still has a refactor issue.  The 2nd param is the name of the action (method) being called on the controller.  If you need to change this for any reason you again would have to do a find and replace.

  • The best way (In my opinion)
    The last option also uses the Html helper classes, but this time we are going to use a Lambda expression.  The following is an example:
    <%=Html.ActionLink<MyController>(c => c.Action( o.Value ), "Edit" ) %>

    This method is the best in my opinion because it is the only one that is strongly typed.  This option may cause some people some pain because they will have to learn/understand lambda expressions, but that is something well worth learning.

    With this method, since the controller being used is strongly typed, the method name and parameter values are all typed strongly typed, refactoring should be a breeze.

    Remember it is always better to have compile time errors then runtime errors due to your refactoring.

I hope this helps someone.  I know I was a little stuck till I watched Scott's video.

BTW, this post could also apply to most of the other HTML helper methods.  Many of them are able to be typed because they use Lamba expressions.

Till next time,


Posted 03-06-2008 7:34 PM by Derik Whittaker

[Advertisement]

Comments

Peter Parker wrote re: Link building 101 with ASP.Net MVC
on 03-06-2008 8:54 PM

Unfortunately, the best method isn't available to us lowly VB.NET programmers

Derik Whittaker wrote re: Link building 101 with ASP.Net MVC
on 03-06-2008 9:39 PM

@Peter,

I did not know that Lamba's were not part of the VB syntax.  That sucks.

Reflective Perspective - Chris Alcock » The Morning Brew #48 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #48
on 03-07-2008 2:49 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #48

Christopher Steen wrote Link Listing - March 6, 2008
on 03-07-2008 7:57 AM

ASP.NET ASP.NET Team Releases for Mix 2008 [Via: podwysocki ] Link Collections Interesting Finds: March...

Daily Bits - March 7, 2008 | Alvin Ashcraft's Daily Geek Bits wrote Daily Bits - March 7, 2008 | Alvin Ashcraft's Daily Geek Bits
on 03-07-2008 9:25 AM

Pingback from  Daily Bits - March 7, 2008 | Alvin Ashcraft's Daily Geek Bits

Ben Scheirman wrote re: Link building 101 with ASP.Net MVC
on 03-07-2008 9:57 AM

I'm concerned about the attributes of the links.  You could either pass in a dictionary (or anonymous type) that contains the name=value pairs, but that is cumbersome to write and to read.

The other option (I think mvccontrib is looking at this) is to have a lambda for building attributes for HTML elements.

So you might have:

<%= Html.ActionLink<ProductsController>("Edit", c => c.Edit(ViewData.Product.Id, l => l.CssClass = "foo", l.title="the_link" ) %>

I'm concerned about how verbose this is, compared to the plain html version:

<a href="/products/edit/6" class="foo" title="the_link">Edit</a>

The benefit is better type safety and refactoring support, however the readability definitely suffers.

Derik Whittaker wrote re: Link building 101 with ASP.Net MVC
on 03-07-2008 10:26 AM

@Ben,

I have not thought about the link attributes, but yes you are correct.  More magic strings.  But attributes may be less of an issue than controller names/method names.

Improved Search Engine Placement wrote Improved Search Engine Placement
on 05-14-2008 10:16 AM

a shopping cart of ads

ASP.NET MVC Archived Buzz, Page 1 wrote ASP.NET MVC Archived Buzz, Page 1
on 07-15-2008 12:54 PM

Pingback from  ASP.NET MVC Archived Buzz, Page 1

umbyersw wrote re: Link building 101 with ASP.Net MVC
on 11-21-2008 12:48 AM

You actually can do simple anonymous functions in VB, it would look like this:

<%=Html.ActionLink(Of MyController)(Function(c) c.Action( o.Value ), "Edit")%>

Link Building wrote re: Link building 101 with ASP.Net MVC
on 05-30-2009 4:33 AM

The above thought is smart and doesn’t require any further addition. It’s  perfect thought from my side.

Suzzane Waltz

Not Going Dark wrote Converting a classic ASP site to ASP.NET MVC Part III - Links and avoiding violating the DRY principle
on 03-02-2010 5:03 PM

Converting a classic ASP site to ASP.NET MVC Part III - Links and avoiding violating the DRY principle

Not Going Dark wrote Converting a classic ASP site to ASP.NET MVC Part III - Links and avoiding violating the DRY principle
on 09-29-2010 5:05 PM

Converting a classic ASP site to ASP.NET MVC Part III - Links and avoiding violating the DRY principle

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)