C# 在MVC5中从基本控制器调用方法时查看错误

C# 在MVC5中从基本控制器调用方法时查看错误,c#,asp.net-mvc,inheritance,C#,Asp.net Mvc,Inheritance,我有以下控制器 public class StoreController : Controller { public ActionResult Index() { var model = new SomeViewModel(); return View(model); } } 及 从派生类调用基索引方法时,我遇到以下错误: 找不到视图“getindex”或其主视图,或者没有视图引擎 支持搜索的位置。下列地点为 搜索:

我有以下控制器

public class StoreController : Controller
{
     public ActionResult Index()
     {
            var model = new SomeViewModel();
            return View(model);
     }
}

从派生类调用基索引方法时,我遇到以下错误:

找不到视图“getindex”或其主视图,或者没有视图引擎 支持搜索的位置。下列地点为 搜索:

默认情况下,GetIndex()方法在派生控制器的视图文件夹中查找视图,即使没有调用view()方法,但由于没有调用view()方法,因此会发生错误

你知道为什么这个方法隐式地寻找一个视图,以及如何克服这个错误吗

编辑:在花了一些时间研究这个问题之后,我遇到了这两篇文章:控制器继承似乎不是那么流行或直接的决定。我的问题的解决方案可以是: 1.不使用控制器继承 2.创建自定义视图引擎,如第二个链接所示(高级)
3.正如其他人在下面提到的,使用视图的完整路径或重定向操作也可以工作

它确实会根据您最初调用的操作方法名称查找视图。如果使用接受视图名称/路径的重载View()方法,则始终可以重写此行为:

public class StoreController : Controller
{
    public ActionResult Index(string viewName = "Index")
    {
        var model = new SomeViewModel();
        return View(viewName, model);
    }
}

public class SofiaStoreController : StoreController
{
    public ActionResult GetIndex(string city)
    {
        return base.Index();
    }
}

SofiaStoreController从StoreController继承的原因是什么?我有一个带有抽象工厂方法的抽象Store类。然后SofiaStore和其他类继承它并重写工厂方法。我认为使用一个基本的StoreController并从中继承是一个更好的主意,而不是依赖字符串来初始化store类。每个派生的存储控制器初始化不同的存储类。一个选项是尝试重定向。现在我有一个错误:未找到视图“索引”或其主节点,或者没有视图引擎支持搜索的位置。搜索了以下位置:~/Views/sofiastore/Index.cshtml~/Views/sofiastore/Index.vbhtml~/Views/Shared/Index.cshtml~/Views/Shared/Index.vbhtml。GetIndex方法继续搜索views/SofiaStore文件夹中的视图,在那里我什么都没有。我的视图位于views/Store文件夹中,在那里我已经有Index.cshtmlth,这只是一个如何覆盖应该使用哪个视图的示例。我不知道你的视图文件。您还可以传递路径而不仅仅是名称,例如“~/Views/existing folder/exisiting file.cshtml”
public class StoreController : Controller
{
    public ActionResult Index(string viewName = "Index")
    {
        var model = new SomeViewModel();
        return View(viewName, model);
    }
}

public class SofiaStoreController : StoreController
{
    public ActionResult GetIndex(string city)
    {
        return base.Index();
    }
}