QueryString in Routing and Model Binding in ASP.NET MVC

Filed Under (Development) by Emad Alashi on 13-01-2012

Tagged Under : , , , , ,

In a nutshell, a query string will not be part of the RouteData dictionary in the routing process, yet it will affect the route matching. On the other hand when Model Binding takes place, query string will be used to bind to the parameters of the action method, unless there is an item in route data with the same key.
Now the details, let’s consider this ASP.NET MVC application, we have an action method:
public ViewResult Archive(DateTime? dateFrom, int? page){...
And we have defined the following routes:
routes.MapRoute(

          "Archive",

          "Archive/{dateFrom}",

          new { controller = "Episode", action = "Archive", dateFrom = UrlParameter.Optional}

          );

Let’s check the URL “~/Archive/1-2-2012?page=2”:
The RouteData dictionary will be as the following:
Key Value
controller Episode
action Archive
dateFrom 1-2-2012?page=2

Yet the action method above will work, and be invoked with the parameters:

Parameter Value
dateFrom 1-2-2012
page 2

So let’s notice the following:

  1. The query string “page” key is NOT part of the RouteData dictionary
  2. In the routing process “?page=2” is considered to be part of the value of the “dateFrom” item in the RouteData dictionary (watch out your route unit tests!)
  3. Even though “dateFrom” route item has the value “1-2-2012?page=2”, Model Binding figures that it can ignore the “?page=2” part because it’s a query string, and the dateFrom parameter is bound correctly to the “1-2-2012”
  4. Even though there is NO “page” item in the RouteData dictionary, the query string it is still used in the Model Binding to bind to the “page” parameter

Having this explained and the above notes taken in mind, what if we have defined an item “page” in the route itself at the end like the following:

routes.MapRoute(

    "Archive",

    "Archive/{dateFrom}",

     new { controller = "Episode", action = "Archive", dateFrom = UrlParameter.Optional, page = 1}

     );

Then this item “page” will show in the RouteData but it should NOT be mistaken by the query string “page”, the Model Binding will always ignore the query string and use the route data item to bind to the action parameter “page”.

And this is why I am writing this post:

  1. I have mistaken the route item “page” with the query string “page” and this confused me a lot
  2. My routing unit tests failed because the query string was still concatenated and present in the value of the route data item, I should have ignored them

I hope this helps you better understand how query strings affects Routing and Model Binding, and helps you avoid what I have went through. Watch out for routing unit tests, and good luck.

Reflections on Jordev Web Camp

Filed Under (Development, Personal) by Emad Alashi on 17-05-2011

Tagged Under : , , , , , , ,

webcampsLast Saturday 14th of May 2011 we had the first web camp in Jordan among Jordev’s activities, and it was great!
check my presentation slides at the end of this post.

The event was like the following:

  1. 8:30 AM – 9:00 AM: Registration
  2. 9:00 AM – 1:00 PM: Four 50 minutes sessions with 10 minutes between each for breaks, there was a few attendees at the beginning so we delayed the first session for couple of minutes (yes we have a morning problem here). Sessions were “Entity Framework 4.1”, “ASP.NET MVC One Step Deeper”, “Dynamic Data”, and “jQuery
  3. 1:00 PM – 2:15 PM: lunch break, where people went to the near market and had their lunch there.
  4. 2:15 PM – 4:30 PM: free coding session.

Things went great:

  1. Enough people attended. The attendees were about 25 people spread all over a hall that takes at least 100, this gave us a great freedom in moving around and hocking cables freely on available slots.
  2. The attendees were great. It’s awesome that the attendees were really serious about the event; everyone brought his/her laptop charged and ready, every one was kind enough to pay the right attention, and everyone stayed to the last minute; it’s this passion and dedication that makes a successful event a successful event.
  3. Very good speakers. We were lucky enough to host one of the smartest and most active community members in Jordan: Omar Qadan, Mahmoud Manasrah, and Omar Muwahed did a great job and delivered such a rich value, I was humbled to be among such intelligent speakers and share the stage with them.
  4. Topics were diverse. It’s true all web, but we covered four important parts that summed the basics of a web app: Entity Framework, ASP.NET MVC, Dynamic Data, and jQuery.
  5. There was no lunch arrangement hassle. Interestingly enough, we decided to skip the arrangement for lunch; we still had a lunch break and we provided fast coffee, but we revolted on the pattern of supplying sponsored food and snacks on the lunch break, this gave us the opportunity to concentrate more on delivering technical value, and less managerial things. Of course the near market made our decision a lot easier, in addition to our good luck of having such sufficient number of attendees.
  6. Two and half hours of Free coding. Actually this was pretty good; the free nature of the session allowed the attendees to contribute, and to ask their questions freely.
    We first gave the attendees the opportunity to try things on their own, then we suggested to have walkthroughs; started playing with some of the latest technologies NuGet and Glimpse, then a walkthrough on ASP.NET MVC, then finally a brief general talk about OData.

    Though I see a big space for improvement here; the down side is that there was a dominant stream because the presenter used the main desk and the presentation screen to talk to the majority in the walkthroughs, which was a distraction to the individuals who wanted to try things on their own, anyway I didn’t hear any complaints.
    We had an option to distribute people among groups depending on the technology they want to learn, but it appeared that it was little bit hard to organize, and the attendees in majority agreed to the way we concluded.

  7. The DVD accumulated for the event. We accumulated a DVD that contains Visual Studio 2010 Express, SQL 2008 Express, VS2010 SP1, NerdDinner sample, and MVCMusicStore sample. This helped others to boot up fast with the event, and a nice thing for the attendees to go home with.

Things went wrong:

  1. Marketing the event. We thought that we should limit the number of the attendees to 80 so we don’t end up in crowded auditorium, so we did, and 80 people registered on EventBrite in less than 48 hours of declaring the event on Facebook and Twitter. To our sad happy surprise only 25 people showed up! I know that not all event registrars attend the events they register for online, but the percent is strikingly high! 75% not attending?! what was wrong?
    I think we didn’t do enough reminders, apparently people are lazy about keeping their calendars
  2. SQL Express installation file was 64 bit. 32 bit OS is still the most common OS here, so we missed that up.

Things we did for preparations:

  1. Distributed tasks among us (four people) so everyone had a clear task, this way we made sure we don’t miss anything due to ambiguity in responsibilities
  2. One of us made sure the hall was booked (more tedious than you think!)
  3. Created an event on EventBrite and shared the link over a mailing list, Facebook, and Twitter
  4. Brought enough 3-in-1 packs of Nescafe, one electronic kettle for hot water, and many small bottles of water
  5. Burned out DVD’s with free content (check above)
  6. Brought 3 multi-slot plugs to support the many laptops with electricity
  7. Rehearsed enough for the presentations Smile

That was about it, I hope this reading benefits you and good luck with YOUR web camps.

Jordev-Webcamp-Speakers

My presentation slides embedded:

First Jordev Web Camp

Filed Under (Development) by Emad Alashi on 07-05-2011

Tagged Under : , , , , , , ,

For too long and all Jordev’s activities have been in the form of sessions with fast code demo’s, and for 1637932099-3the first time we are going to break this rule and do our first web camp in which attendees will have the chance to have hands-on experience with some of Jordan’s active experts, it will be on the 15th of May 2011.

There are two things in web camps that make them more interesting: REAL CODING and COLLABORATION; coding is a practice science, best way to learn it is by practice, and this practice will be much more fruitful and enjoyable if done with bunch of enthusiasts who share same passion with you. Speakers will have their share, but the biggest share will go for the hidden experts who have been hiding under excessive working hours, and who avoid boring talkative sessions, this gathering will be a chance for us to dig these gems out and have the most geeky fun and benefit we seek in such communities.

The schedule will be as the following:

Session

Time

Duration

Speaker

Registration

08:30 – 09:00

00:30

-

Entity Framework

09:00 – 9:50

00:50

Omar Qadan

MVC one step deeper

10:00 – 10:50

00:50

Emad Alashi

Data Dynamics

11:00 – 11:50

00:50

Mahmoud Manasrah

jQuery

12:00 – 12:50

00:50

Omar Muwahed

Break

1:00 – 2:00

1:00

-

Hands on Labs

2:00 – 4:30

2:30

Speakers and attendees

For more details you can follow it on EventBrite here, see you there.

Code Sample Should Be Clean

Filed Under (Development, software management) by Emad Alashi on 02-04-2011

Tagged Under : , ,

big_Telerik01The other day I needed an ASP.NET MVC grid control, and I have always heard about Telerik’s ASP.NET MVC Extensions and the great tools they provide, so I decided to give them a try.

So I followed the installation guide step by step, and prepared my code to use the extensions; I added a reference to the DLL, added the scripts, etc… and I thought I was ready to test-drive it.
I opened Telerik’s sample website which showed the control and below it the code sample that made it work, innocently enough I did what any other developer would do to try out the sample code: copy and paste it in your page and then build. To my surprise there was an error in the View; it didn’t recognize an Extension method called “Configurator”!

It was so strange, why wouldn’t it work? I made sure that I added the correct reference dll, that I added the namespaces in the web.config as advised by the guide, and that I used the “using” at the top of my page, but yet it didn’t work.
Ok now that is really strange, I downloaded Reflector ILSpy and disassembled Teleriks dll to make sure that this method exists, and it wasn’t there! where does this Configurator method come from?!

I opened the sample project to make sure of their code, and guess what…the Configurator method was a method that only exists in their sample project; it wasn’t part of the DLL! I wasted considerable time trying to figure this out, and my conclusion was: never use auxiliary code when you are presenting a sample for another code unless you make it clear.

Introduction to ASP.NET MVC session at Jordev

Filed Under (Development) by Emad Alashi on 27-11-2010

Tagged Under : , , ,

It was great to get back to presentations after this while; ASP.NET MVC was the topic for the latest session I delivered through Jordev. The audience was great, I hope they enjoyed it as much as I did preparing for it.

you can find the slides on Slideshare, embedded here as well:

I will upload the photos of the session and possibly a recorded video to youtube in the nearest chance enshallah.