I’m baffled why this works on my development server (using IIS Express) and not on the production server (Win2K8 R2 + IIS 7.5).
I have a very simple WebAPI controller method:
public HttpResponseMessage Get(string notificationType = "all", int skip=0, int take=25) {
Trace.WriteLine(String.Format("Hello, world! From NotifcationsController#Get notificationType={0} skip={1} take={2}", notificationType, skip, take));
Trace.WriteLine(String.Format("And furthermore, HttpContext.Current.Request[notificationType]={0}", HttpContext.Current.Request["notificationType"]));
Trace.WriteLine(String.Format("And finally, HttpContext.Current.Request.QueryString[notificationType]={0}", HttpContext.Current.Request.QueryString["notificationType"]));
...
}
On the development server at http://localhost:1398/api/notifications?notificationType=comments, things work. I see this in trace.axd:
Trace Information
Category Message From First(s) From Last(s)
Hello, world! From NotifcationsController#Get notificationType=comments skip=0 take=25
And furthermore, HttpContext.Current.Request[notificationType]=comments
And finally, HttpContext.Current.Request.QueryString[notificationType]=comments
But when hitting this controller at http://api.nameofmywebsite.com/api/notifications?type=comments I see this in trace.axd:
Trace Information
Category Message From First(s) From Last(s)
Hello, world! From NotifcationsController#Get notificationType=all skip=0 take=25
And furthermore, HttpContext.Current.Request[notificationType]=
And finally, HttpContext.Current.Request.QueryString[notificationType]=
...
Querystring Collection
Name Value
type comments
This seems very, very bizarre to me. Based on what’s in trace.axd, it sure doesn’t seem to be a routing issue… it’s hitting the correct controller and action.
Why isn’t the querystring parameter getting mapped to the parameter of the controller action in the production environment!?
For whatever it’s worth, I’m using the same web.config on both development and server. I can’t think what configuration differences would cause this difference…
I’m a moron.
This wasn’t working because I was hitting “notifications?type=comments” on the production server and not “notifications?notificationType=comments”
The method parameter name has to match the querystring parameter name for binding to happen. I knew that, but didn’t realize I was passing in the wrong querystring parameter name! What happened is that I changed the method parameter name a few weeks ago, forgot it in the meantime, and didn’t realize the browser’s autocomplete function supplied me with a history match that contained the old parameter name.