MVC2 和 MVC Futures 导致 RedirectToAction 问题

发布于 2024-08-28 08:16:36 字数 3034 浏览 5 评论 0原文

我一直在尝试从 MVC Futures 项目中获取强类型版本的 RedirectToAction 来工作,但一无所获。以下是我遵循的步骤以及遇到的错误。非常感谢任何帮助。

我创建了一个新的 MVC2 应用程序,并将 HomeController 上的“关于”操作更改为重定向到索引页面。

Return RedirectToAction("Index")

但是,我想使用强类型扩展,因此我从 CodePlex 下载了 MVC Futures,并将对 Microsoft.Web.Mvc 的引用添加到我的项目中。

我在 HomeContoller.vb 的顶部添加了以下“导入”语句,

Imports Microsoft.Web.Mvc

我注释掉了上面的 RedirectToAction 并添加了以下行:

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

到目前为止,一切顺利。但是,我注意到如果我取消注释第一个(非通用)RedirectToAction,它现在会导致以下编译错误:

错误 1 ​​重载解析失败,因为无法使用以下参数调用可访问的“RedirectToAction”:
扩展方法“Public Function RedirectToAction(Of TController)(action As System.Linq.Expressions.Expression(Of System.Action(Of TController))) As System.Web.Mvc.RedirectToRouteResult”在“Microsoft.Web.Mvc.ControllerExtensions”中定义':无法从这些参数推断出类型参数的数据类型。显式指定数据类型可能会纠正此错误。
扩展方法“Public Function RedirectToAction(action As System.Linq.Expressions.Expression(Of System.Action(Of HomeController))) As System.Web.Mvc.RedirectToRouteResult”在“Microsoft.Web.Mvc.ControllerExtensions”中定义:值类型“String”无法转换为“System.Linq.Expressions.Expression(Of System.Action(Of mvc2test1.HomeController))”。

尽管智能感知显示了 8 个重载(原来的 6 个非泛型重载,加上来自 Futures 程序集的 2 个新的泛型重载),但似乎在尝试编译代码时,编译器只会“找到”2 个非泛型重载。来自期货评估的通用扩展方法。

我认为这可能是因为我使用了 MVC2 程序集和 futures 程序集的冲突版本,因此我将 Futures 下载中的 MvcDiaganotics.aspx 添加到我的项目中,一切看起来都是正确的:

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
代码库:file:///C:/WINDOWS/ assembly/GAC_MSIL/System.Web.Mvc/2.0.0.0__31bf3856ad364e35/System.Web.Mvc.dll
部署:GAC 部署的

ASP.NET MVC Futures 程序集信息 (Microsoft.Web.Mvc.dll)

程序集版本:ASP.NET MVC 2 RTM Futures (2.0.50217.0)
全名:Microsoft.Web.Mvc,Version=2.0.0.0,Culture=neutral,PublicKeyToken=null
代码库:file:///xxxx/bin/Microsoft.Web.Mvc.DLL
部署:bin-deployed

这让我发疯!因为我认为这可能是一些 VB 问题,所以我使用 C# 创建了一个新的 MVC2 项目,并尝试了与上面相同的操作。

我在 HomeController.cs 的顶部添加了以下“using”语句

using Microsoft.Web.Mvc;

这次,在 About 操作方法中,我只能通过键入完整命令来调用非通用 RedirectToAction,如下所示:

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

即使我有一个“using " 类顶部的声明,如果我尝试按如下方式调用非泛型 RedirectToAction:

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

我会收到以下编译错误:

错误 1 ​​非泛型方法“System.Web.Mvc.Controller.RedirectToAction(string)”不能与类型参数一起使用

给出了什么?我并不是想做任何不寻常的事情。 这是一个简单的普通 MVC2 项目,仅引用了 Futures 程序集。

我希望我错过了一些明显的事情,但我已经挠头太久了,所以我想我应该寻求一些帮助。

如果有人设法让这个简单的场景工作(在 VB 和/或 C# 中),他们可以让我知道他们做了什么(如果有的话)不同吗?

谢谢!

I've been trying to get the strongly typed version of RedirectToAction from the MVC Futures project to work, but I've been getting no where. Below are the steps I've followed, and the errors I've encountered. Any help is much appreciated.

I created a new MVC2 app and changed the About action on the HomeController to redirect to the Index page.

Return RedirectToAction("Index")

However, I wanted to use the strongly typed extensions, so I downloaded the MVC Futures from CodePlex and added a reference to Microsoft.Web.Mvc to my project.

I addded the following "import" statement to the top of HomeContoller.vb

Imports Microsoft.Web.Mvc

I commented out the above RedirectToAction and added the following line:

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

So far, so good. However, I noticed if I uncomment out the first (non Generic) RedirectToAction, it was now causing the following compile error:

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))'.

Even though intelli-sense was showing 8 overloads (the original 6 non-generic overloads, plus the 2 new generic overloads from the Futures assembly), it seems when trying to complie the code, the compiler would only 'find' the 2 non-gneneric extension methods from the Futures assessmbly.

I thought this might be an issue that I was using conflicting versions of the MVC2 assembly, and the futures assembly, so I added MvcDiaganotics.aspx from the Futures download to my project and everytyhing looked correct:

ASP.NET MVC Assembly Information (System.Web.Mvc.dll)

Assembly version: ASP.NET MVC 2 RTM (2.0.50217.0)
Full name: System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
Code base: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Web.Mvc/2.0.0.0__31bf3856ad364e35/System.Web.Mvc.dll
Deployment: GAC-deployed

ASP.NET MVC Futures Assembly Information (Microsoft.Web.Mvc.dll)

Assembly version: ASP.NET MVC 2 RTM Futures (2.0.50217.0)
Full name: Microsoft.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
Code base: file:///xxxx/bin/Microsoft.Web.Mvc.DLL
Deployment: bin-deployed

This is driving me crazy! Becuase I thought this might be some VB issue, I created a new MVC2 project using C# and tried the same as above.

I added the following "using" statement to the top of HomeController.cs

using Microsoft.Web.Mvc;

This time, in the About action method, I could only manage to call the non-generic RedirectToAction by typing the full commmand as follows:

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

Even though I had a "using" statement at the top of the class, if I tried to call the non-generic RedirectToAction as follows:

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

I would get the following compile error:

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

What gives? It's not like I'm trying to do anything out of the ordinary.
It's a simple vanilla MVC2 project with only a reference to the Futures assembly.

I'm hoping that I've missed out something obvious, but I've been scratching my head for too long, so I figured I'd seek some assisstance.

If anyone's managed to get this simple scenario working (in VB and/or C#) could they please let me know what, if anything, they did differently?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-09-04 08:16:36

尝试使用:

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

使用 this 限定符允许使用扩展方法。

Try using:

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

Using the this qualifier allows the extension methods to be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文