使用T4MVC生成的代码在MVC 3项目中编译错误

时间:2011-04-06 11:58:02

标签: asp.net-mvc asp.net-mvc-3 t4mvc

我们正在开发一个使用ASP.Net 4& amp;的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 ,它实际上用 ViewEngineResult 声明 FindView()返回类型:

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

有没有人收到此错误?它与MVC版本有关,我们是否正在使用MVC 3?

非常感谢! 塞吉

1 个答案:

答案 0 :(得分:5)

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

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

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

相关问题