T4MVC 未为一个控制器生成某些操作

发布于 2024-11-15 09:07:41 字数 2113 浏览 1 评论 0原文

我遇到的情况是,T4MVC 正确生成所有内容(意味着智能感知显示所有区域/控制器/操作并且所有内容都编译),但是当我运行代码时,我收到 T4MVC was returnedly 运行时错误。

我调查了生成的文件,发现对于我的项目中的一个控制器,只有基类中的操作才会生成覆盖的存根操作。对于其他控制器,正在生成所有操作。它们都具有相同的设置,如下所述。

我有一个 BaseController 类,它有一些共享代码(并且继承自 Controller)。在 Controllers 目录(项目的根目录)中,我有许多控制器,它们都继承自 BaseController

然后我有几个区域。在每个区域中,我都有相同的控制器,每个控制器都继承自根 Controllers 目录中的同名控制器。

运行 T4MVC(版本 2.6.54),除了一个控制器之外,一切正常。奇怪的是,智能感知适用于控制器,但在引用实际操作时(在 ActionLink() 调用中)会出现阻塞。

我手动将一项操作特别添加到生成的代码中,并且没有错误。

所以我的问题是,什么会导致 T4MVC 生成控制器的所有代码?缺少的操作都是公共虚拟ActionResult,并且操作本身工作正常。问题控制器在所有区域都存在相同的问题。

一些缩写代码。

/Controllers/BaseController.cs

namespace MyProject.Controllers
{
    public abstract partial class BaseController : Controller
    {
        protected ISession session;

        public BaseController()
        {
        }

        // other shared methods/actions
    }
}

/Controllers/ActivitiesController.cs(这是问题控制器)

namespace MyProject.Controllers
{
    public partial class ActivitiesController : BaseController
    {
        // for resolving concurrency exceptions
        private Activity userValues;
        private Activity databaseValues;

        public ActivitiesController() : base()
        {
            ViewBag.ControllerName = "Activities";
        }

        // this action is causing the problem used like
        <li>@Html.ActionLink("Activities", MVC.Areas.Module1.Activities.Index())</li> in a view
        public virtual ActionResult Index()
        {
            return View();
        }
    }
}

/Areas/Module1/Controllers/ActivitiesController.cs。这是全班同学

namespace MyProject.Areas.Module1.Controllers
{
    public partial class ActivitiesController : MyProject.Controllers.ActivitiesController
    {
        public ActivitiesController() : base()
        {
            base.currentModule = Modules.Module1;            
        }
    }
}

I have a situation where T4MVC is generating everything properly (meaning intellisense shows all areas/controllers/actions and everything compiles), but when I run the code, I get a T4MVC was called incorrectly runtime error.

I've investigated the generated files and discovered that for one controller in my project, only actions in the base class are getting the overridden stub actions generated. For other controllers, all actions are being generated. They all have the same set up, described below.

I have a BaseController class that has some shared code (and inherits from Controller). In the Controllers directory (root of project) I have a number of controllers, all which inherit from BaseController.

I then have several Areas. In each Area, I have the same controllers, each inheriting from the controller of the same name in the root Controllers directory.

Running T4MVC (version 2.6.54), everything works fine except for one controller. The odd thing is that intellisense works for the controller, but chokes when the actual action is referenced (in an ActionLink() call).

I manually added one action in particular into the generated code and there was no error.

So my question is, what would cause T4MVC to not generate all code for a controller? The missing actions are all public virtual ActionResult and the actions themselves work fine. The problem controller has the same issue in all Areas.

Some abbreviated code.

/Controllers/BaseController.cs

namespace MyProject.Controllers
{
    public abstract partial class BaseController : Controller
    {
        protected ISession session;

        public BaseController()
        {
        }

        // other shared methods/actions
    }
}

/Controllers/ActivitiesController.cs (this is the problem controller)

namespace MyProject.Controllers
{
    public partial class ActivitiesController : BaseController
    {
        // for resolving concurrency exceptions
        private Activity userValues;
        private Activity databaseValues;

        public ActivitiesController() : base()
        {
            ViewBag.ControllerName = "Activities";
        }

        // this action is causing the problem used like
        <li>@Html.ActionLink("Activities", MVC.Areas.Module1.Activities.Index())</li> in a view
        public virtual ActionResult Index()
        {
            return View();
        }
    }
}

/Areas/Module1/Controllers/ActivitiesController.cs. This is the whole class

namespace MyProject.Areas.Module1.Controllers
{
    public partial class ActivitiesController : MyProject.Controllers.ActivitiesController
    {
        public ActivitiesController() : base()
        {
            base.currentModule = Modules.Module1;            
        }
    }
}

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

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

发布评论

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

评论(2

旧伤慢歌 2024-11-22 09:07:41

如果其他人遇到这个问题,我也会遇到类似的问题并产生运行时错误消息,但情况略有不同。它位于 ActionResult 方法末尾的 RedirectToAction 语句中:

RedirectToAction(Edit(id));

错误更正后消失了:

RedirectToAction(MVC.[action name].Edit(id));

错误消息不是很直观,重新运行自定义工具的建议并没有真正帮助。

In case anyone else comes across this I had a similar issue and resulting run-time error message but in a bit different scenario. It was in the RedirectToAction statement at end of a ActionResult method:

RedirectToAction(Edit(id));

The error went away after correcting it to:

RedirectToAction(MVC.[action name].Edit(id));

The error message isn't very intuitive and the suggestion to re-run the custom tool doesn't really help.

述情 2024-11-22 09:07:41

您是否确保重新运行 T4MVC 以基于最新版本生成(右键单击 .tt 文件/运行自定义工具)?

如果这不是问题,我可能需要查看有问题的示例应用程序以了解发生了什么情况。

Did you make sure to re-run T4MVC to generate based on the latest (Right click .tt file / run custom tool)?

If that's not the problem, I may need to look at a sample app that has the problem to see what's going on.

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