开发者

T4MVC not generating some Actions for one Controller

开发者 https://www.devze.com 2023-03-12 21:44 出处:网络
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 i

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
    }
}

/Contro开发者_开发问答llers/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;            
        }
    }
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消