2012-12-08 19 views
1

我很困惑爲什麼在我的開發服務器(使用IIS Express)而不是生產服務器(Win2K8 R2 + IIS 7.5)上工作。簡單的ASP.NET WebApi參數綁定不起作用

我有一個非常簡單的WebAPI控制器的方法:在http://localhost:1398/api/notifications?notificationType=comments

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

在開發服務器,工作的事情。我看到這個的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。我想不出什麼配置差異會導致這種差異...

回答

0

我是個白癡。

這是行不通的,因爲我打「的通知?類型=意見」在生產服務器上,而不是「通知?notificationType =意見」

的方法參數名稱必須與查詢參數名稱相匹配的結合即將發生。我知道,但沒有意識到我傳遞了錯誤的querystring參數名!發生了什麼是我幾個星期前改變了方法參數名稱,同時忘了它,並沒有意識到瀏覽器的自動完成功能爲我提供了一個包含舊參數名稱的歷史匹配。