1

我很困惑为什么这适用于我的开发服务器(使用 IIS Express)而不是生产服务器(Win2K8 R2 + IIS 7.5)。

我有一个非常简单的 WebAPI 控制器方法:

 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"]));
      ...
 }

在 的开发服务器上http://localhost:1398/api/notifications?notificationType=comments,一切正常。我在 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

但是当http://api.nameofmywebsite.com/api/notifications?type=comments我在 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

这对我来说似乎非常非常奇怪。根据 trace.axd 中的内容,它似乎肯定不是路由问题……它击中了正确的控制器和操作。

为什么查询字符串参数没有映射到生产环境中控制器操作的参数!?

不管它值多少钱,我在开发和服务器上都使用相同的 web.config。我想不出什么配置差异会导致这种差异......

4

1 回答 1

0

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.

于 2012-12-09T00:22:11.263 回答