2

I've run into a bit of an odd problem, and I'm not 100% sure what's the way to resolve this.

Given an MVC controller, BaseController (Minimal example below)

BaseController : Controller 
{
    public ActionResult LotsOfActions() 
    {
        //this calls a view that renderActions AnAction(SomeModel) a bunch of times.
    }

    public ActionResult AnAction(Object SomeModel) 
    { 
        //do stuff
        return View("AnAction",TemplateFromCode,ViewModel); //Depending on something in SomeModel, we want a different template for this.
    }
}

DerivedController : BaseController 
{
    public override ActionResult LotsOfActions() 
    {
        //this calls a view that renderActions BaseController.AnAction(SomeModel) a bunch of times but with different logic.
    }
}

When AnAction is called on DerivedController, it's attempting to use DerivedController/AnAction.cshtml (or whatever name), which I don't need to exist because the subview should be the same, and so I get a view not found error. How do I get this to use BaseController/AnAction.cshtml as I intend it to? I don't want to use the shared view folder for this, because that's scanned before DerivedController in case I do want to override this view for something else that is a subclass of BaseController.

2
  • 1
    You can just be explicit in your base controller: return View("~/Views/Base/AnAction.cshtml"... Commented Jun 1, 2016 at 17:08
  • That was... surprisingly obvious, actually. Thanks. If you drop this as an answer I'll quickly mark it as the answer. Thought it'd be easier programmatically, but hey. Commented Jun 1, 2016 at 17:30

2 Answers 2

2

The easiest way is just to be explicit in your base class by specifying the full path to the view, like this:

public ActionResult AnAction(Object SomeModel) 
{ 
    //do stuff
    return View("~/Views/Base/AnAction.cshtml",TemplateFromCode,ViewModel);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of inheriting the base controller why not have anything calling AnAction just call that controller? Usually controller inheritance is used for internal details and not public action method overrides.

Otherwise you are stuck with what DavidG suggested since the view path is auto-constructed from the realized controllers location, not the base controllers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.