MVC 3 项目中 T4MVC 生成的代码出现编译错误

发布于 2024-10-31 07:39:36 字数 1707 浏览 2 评论 0原文

我们正在使用 ASP.Net 4 和 ASP.Net 4 开发 Web 应用程序。 MVC 3 框架。我已经通过 NuGet 安装了 T4MVC,所有视图、控制器和静态内容都成功生成为强类型。

但是,当我尝试编译该项目时,它在生成的文件 T4MVC.cs 中引发一个错误,即:

'T4MVC_ViewResultBase.FindView(System.Web.Mvc.ControllerContext)': 
 return type must be 'System.Web.Mvc.ViewEngineResult' to match overridden member
 'System.Web.Mvc.ViewResultBase.FindView(System.Web.Mvc.ControllerContext)'

这是生成的源代码:

[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
public class T4MVC_ViewResultBase : System.Web.Mvc.ViewResultBase,
                                                            IT4MVCActionResult 
{
  public T4MVC_ViewResultBase(string area, string controller, string action):
      base()  {
       this.InitMVCT4Result(area, controller, action);
    }

    protected override void FindView(System.Web.Mvc.ControllerContext context){}

    public string Controller { get; set; }
    public string Action { get; set; }
    public RouteValueDictionary RouteValueDictionary { get; set; }
}

错误说:

protected override void FindView(System.Web.Mvc.ControllerContext context) { }

应该是:

protected override ViewEngineResult 
               FindView(System.Web.Mvc.ControllerContext context) { }

但随后它引发了另一个编译错误,因为此方法应该返回代码。

如果我们检查它继承的基类,System.Web.Mvc.ViewResultBase,它实际上声明 FindView()ViewEngineResult 返回类型:

public abstract class ViewResultBase : ActionResult
    {
        ...
        protected abstract ViewEngineResult FindView(ControllerContext context);
    }

有人遇到这个错误吗?这与 MVC 版本有关吗?我们使用的是 MVC 3 吗?

多谢! 塞尔吉

We are developing a web application with ASP.Net 4 & MVC 3 Framework. I've installed T4MVC through NuGet and all the Views, Controllers and static content are succesfully generated as strong types.

But, when I try to compile the project, it raises an error at generated file T4MVC.cs, which is:

'T4MVC_ViewResultBase.FindView(System.Web.Mvc.ControllerContext)': 
 return type must be 'System.Web.Mvc.ViewEngineResult' to match overridden member
 'System.Web.Mvc.ViewResultBase.FindView(System.Web.Mvc.ControllerContext)'

This is the source code generated:

[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
public class T4MVC_ViewResultBase : System.Web.Mvc.ViewResultBase,
                                                            IT4MVCActionResult 
{
  public T4MVC_ViewResultBase(string area, string controller, string action):
      base()  {
       this.InitMVCT4Result(area, controller, action);
    }

    protected override void FindView(System.Web.Mvc.ControllerContext context){}

    public string Controller { get; set; }
    public string Action { get; set; }
    public RouteValueDictionary RouteValueDictionary { get; set; }
}

The error says that:

protected override void FindView(System.Web.Mvc.ControllerContext context) { }

should be:

protected override ViewEngineResult 
               FindView(System.Web.Mvc.ControllerContext context) { }

But then it raises another compiling error, as this method should return code.

If we check the base class it inherits from, System.Web.Mvc.ViewResultBase, it actually declares FindView() with ViewEngineResult return type:

public abstract class ViewResultBase : ActionResult
    {
        ...
        protected abstract ViewEngineResult FindView(ControllerContext context);
    }

Has anyone got this error? Has it something to do with MVC version, are we are using MVC 3?

Thanks a lot!
Sergi

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

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

发布评论

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

评论(1

饭团 2024-11-07 07:39:36

我想我看到了这个问题,这是一个 T4MVC 错误。但希望它很容易解决。

您是否有一个声明为返回 ViewResultBase 的控制器操作?如果是的话,能否将返回类型改为ActionResult?或者,您可以将返回类型更改为您要返回的具体类型(例如,它是 ViewResult)吗?

T4MVC 的错误是它没有正确覆盖 ActionResult 类型中的非 void 方法。

I think I see the problem, and it is a T4MVC bug. But hopefully it's easy to work around.

Do you have a controller action that is declared to return a ViewResultBase? If so, can you change the return type to be ActionResult? Or alternatively you can change the return type to be whatever the concrete type is that you're returning (e.g. is it ViewResult)?

The T4MVC bug is that it doesn't correctly override non-void methods in ActionResult types.

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