2

我一直在嘗試從MVC Futures項目中獲得RedirectToAction的強類型版本,但我一直沒有收到任何地方的信息。以下是我所遵循的步驟以及我遇到的錯誤。任何幫助深表感謝。引起RedirectToAction問題的MVC2和MVC期貨

我創建了一個新的MVC2應用程序,並將HomeController上的About操作改爲重定向到Index頁面。

Return RedirectToAction("Index") 

不過,我想使用強類型的擴展名,所以我從CodePlex上下載的MVC期貨,並添加到Microsoft.Web.Mvc引用到我的項目。

我addded下面的「導入」語句來HomeContoller.vb

Imports Microsoft.Web.Mvc 

頂部我註釋掉上述RedirectToAction並添加以下行:

Return RedirectToAction(Of HomeController)(Function(c) c.Index()) 

到目前爲止,一切都很好。然而,我發現,如果我取消了第一個(非通用)RedirectToAction,這是現在導致以下編譯錯誤:

Error 1 Overload resolution failed because no accessible 'RedirectToAction' can be called with these arguments:
Extension method 'Public Function RedirectToAction(Of TController)(action As System.Linq.Expressions.Expression(Of System.Action(Of TController))) As System.Web.Mvc.RedirectToRouteResult' defined in 'Microsoft.Web.Mvc.ControllerExtensions': Data type(s) of the type parameter(s) cannot be >inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Extension method 'Public Function RedirectToAction(action As System.Linq.Expressions.Expression(Of System.Action(Of HomeController))) As System.Web.Mvc.RedirectToRouteResult' defined in 'Microsoft.Web.Mvc.ControllerExtensions': Value of type 'String' cannot be converted to 'System.Linq.Expressions.Expression(Of System.Action(Of mvc2test1.HomeController))'.

即使智能影音意義是顯示8個重載(原6個非通用的過載,加上來自期貨組合的兩個新的泛型重載),似乎在試圖編譯代碼時,編譯器只會從期貨評估中'找到'2種非gneneric擴展方法。

我想這可能是我用的版本衝突的MVC2組件的問題,期貨組裝,所以我從期貨加入MvcDiaganotics.aspx下載到我的項目,everytyhing看着正確的:

ASP .NET MVC大會信息(System.Web.Mvc.dll程序)

大會版本:ASP.NET MVC 2 RTM(2.0.50217.0)
全名:System.Web.Mvc,版本= 2.0 .0.0,文化=中立,PublicKeyToken = 31bf3856ad364e35
代碼基地:文件:/// C:/WINDOWS/assembly/GAC_MSIL/System.Web.Mvc/2.0.0.0__31bf3856ad364e35/System.Web.Mvc.dll
部署:GAC部署

ASP.NET MVC期貨大會信息(Microsoft.Web.Mvc.dll)

大會版本:ASP.NET MVC 2個RTM期貨(2.0.50217.0)
全名:Microsoft.Web.Mvc ,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = null
代碼庫:文件:///xxxx/bin/Microsoft.Web.Mvc.DLL
部署:倉部署

這是推動我瘋了!因爲我認爲這可能是一些VB問題,我使用C#創建了一個新的MVC2項目,並嘗試與上面相同。

我添加了下面的「使用」語句來HomeController.cs

using Microsoft.Web.Mvc; 

這一次的頂部,在關於操作方法,我只能管理由輸入完整調用非通用RedirectToAction條命令如下:

return Microsoft.Web.Mvc.ControllerExtensions.RedirectToAction<HomeController>(this, c => c.Index()); 

即使我有「使用」的聲明在類的頂部,如果我試着撥打非通用RedirectToAction如下:

return RedirectToAction<HomeController>(c => c.Index()); 

我會得到以下編譯錯誤:

Error 1 The non-generic method 'System.Web.Mvc.Controller.RedirectToAction(string)' cannot be used with type arguments

是怎麼回事?這不是我想做的事情不尋常。 這是一個簡單的香草MVC2項目,僅提及期貨組合。

我希望我已經錯過了一些明顯的東西,但我一直在撓我的頭太久,所以我想我會尋求一些幫助。

如果有人設法讓這個簡單的場景工作(在VB和/或C#中),請讓我知道什麼,如果有的話,他們做了什麼不同?

謝謝!

回答

3

嘗試使用:

return this.RedirectToAction<HomeController>(c => c.Index()); 

使用this預選賽中允許使用的擴展方法。

+1

這對我的C#項目沒有什麼竅門。是否有理由需要使用'this'關鍵字?我認爲這是隱含的。 – Darragh 2010-04-13 08:12:33