Noah Blumenthal's Blog

March 23, 2009

UriModelBinder for ASP.NET MVC RTM

Filed under: ASP.NET MVC — noahblu @ 4:54 pm
Tags: ,

I blogged a while ago about creating a UriModelBinder to bind a url string to a Uri object (see https://noahblu.wordpress.com/2008/11/07/uri-modelbinders-and-updatemodel/) but things have changed since then (I think that was RC1, but I don’t recall).

Anyway, here’s the same gist updated for the MVC RTM release:

public class UriModelBinder : IModelBinder
{
#region IModelBinder Members

public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult result;
Uri url = null;
bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out result);
if (result != null && string.IsNullOrEmpty(result.AttemptedValue))
{
if (Uri.TryCreate(result.AttemptedValue, UriKind.RelativeOrAbsolute, out url))
return url;
}
return null;
}

#endregion
}

As you can see, the code is pretty simple.  I’m returning a NULL Uri if the Url is not valid.  I’m also accepting Relative OR Absolute Uris.  This might not be what you want to do.  But as you can see, custom model binders are pretty easy.

———

A friend asked me how this works, so I’m gonna post it here:

Take a look at this line:

bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out result);

What we’re doing here is attempting to get  a ValueProviderResult based on the the bindingContext.ModelName. Keep in mind that the ValueProvider is a way of getting a value and in our website is most likely in the form of a FormCollection.

Now I’m using the bindingContext.ModelName to pull the value because a Uri is made up of only a single field — i.e. I have a website with textbox called website url that takes an entire string like “http://www.google.com” .  So if we’re traversing an object tree and one of the properties that we’re binding is called “WebsiteUrl”, we’ll expect that a field called “WebsiteUrl” is posted with the rest of the data and that’s the bindingContext.ModelName.  Now this might change if we were building a more complex object or an object that’s based on multiple fields.

Advertisements

1 Comment »

  1. Very helpful. I made a few changes for the latest mvc, but used this on a custom ‘Money’ object. Worked great.

    Thanks for the post, it helped me out

    Comment by Steve — November 23, 2009 @ 5:07 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: