Adding an alias to an action parameter for model binding in ASP.NET MVC

This is the introduction of a new Nuget package called ActionParameterAlias.

Have you ever had a URL:

http://example.com?productid=123456

where you wanted to bind it with an ASP.NET MVC action like this?

   1:  public ActionResult Search(Product product)
   2:  {
   3:      //...
   4:      return View(product);
   5:  }

 

Chris Missal was discussing this for a project, so we worked together and came up with an action filter that enables the use of aliases.  More specifically, the following allows the same action to accept url with a query string parameter matching it or any of the aliases.

   1:  [ParameterAlias("Product", "pid", Order = 1)]
   2:  [ParameterAlias("Product", "p_id", Order = 2)]
   3:  [ParameterAlias("Product", "productid", Order = 3)]
   4:  public ActionResult Search(Product product)
   5:  {
   6:      //...
   7:      return View(product);
   8:  }

Just head on over to Nuget and go grab it.  This is a feature that I’d like to see make it into the next version of the framework.  It’s so tiny of a feature, but so many folks have parameter names that are just a bit off because there is no way to add aliases currently.

CropperCapture[1]

To check out the code, clone it from BitBucket here: https://bitbucket.org/jeffreypalermo/action-parameter-alias or on the Nuget gallery at http://nuget.org/packages/ActionParameterAlias/4.0.0.

Comments

Betty said on 2.23.2012 at 7:55 PM

Reminds me a bit of Jimmy Bogards intelligent model binder lostechies.com/.../intelligent-mod

Jeffrey Palermo said on 2.23.2012 at 9:46 PM

@Betty,

Yes, that model binder served us well! The difference is that model binding converts from a string to a particular type. The value providers look in multiple locations to get the string based on a key. Here, we are providing more places to look because if we don't find it with the parameter name, then we look based on the alias(es).

Betty said on 2.24.2012 at 12:03 AM

Yeah I got the fact this was adding in aliases, but you're still getting back a full model instead of just an Id. I guess I presumed it also was loading the entity from db.

Ken Egozi said on 2.24.2012 at 9:03 AM

Good stuff.

How about this API variant:

public ActionResult Search([Alias("pid", "p_id", "product")] Product product)

?

James Nail said on 2.24.2012 at 1:14 PM

I really love this idea, Jeffrey. I think some added flexibility was badly needed here, as I've experienced quite a bit of magic string-induced refactoring pain when dealing with changes to parameter naming.

But I'd like to second Ken Egozi's suggestion as well. While parameter attributes might not be the prettiest thing in the world to look at, I think they fit the semantic of what's being done here better than a method attribute.

Jeffrey Palermo said on 2.25.2012 at 9:17 PM

@Ken

That API is good too. I've developed it so that all the capability is in the value provider. The attribute and global.asax can be used to configure it. Wouldn't be too hard to add a parameter attribute as well. I'm happy to have a pull request!

Jeffrey Palermo said on 2.25.2012 at 9:17 PM

@James,

I agree it could be very useful.

StarTrekRedneck said on 3.15.2012 at 12:57 PM

Very nice! I think I agree with Ken and James, but what I especially like is that this is a perfect feature for a web framework, something very usable there, and that it is not a business concept or process fattening the web layer.