c# - Asp.NET MVC 5 中 Controller 的继承

标签 c# asp.net-mvc inheritance asp.net-mvc-5 asp.net-mvc-controller

我创建了一个通用 Controller 类

namespace OxygenFramework.MvcController
{
  public class MvcController<TEntity> : Controller
    where TEntity : class
  {
    public void UpdateModelState(TEntity t)
    {
        ...
    }
  }
}

然后我按如下方式使用它

namespace LeitnerMVC.Controllers
  {
    public class HomeController : MvcController<Account>
     {
    //
    // GET: /Home/
    public ActionResult Index()
    {
        UpdateModelState(t);
        return View();
    }
  }
}

但是当运行 mvc 应用程序页面时显示此错误

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies)     could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

在网上搜索后找到了解决的方法

  void Application_Start(object sender, EventArgs e)
    {                 
    ControllerBuilder.Current.DefaultNamespaces.Add("OxygenFramework.MvcController");
    }

但是上述解决方案对我不起作用!并再次显示 Http 404 错误

当使用Controller而不是MvcController时页面显示没有问题!!!

谁能帮我吗?

更新:

经过多次调查,我明白为什么会出现这个问题,但我仍然不知道如何解决。 当我将 MvcController 的源代码移出框架程序集 (OxygenFramework.MvcController) 并将其移至 MVC 项目时,MvcController 可以工作,但是当我从 OxygenFramework 程序集 MVC 引用 MvcController 时,显示 404 错误! 现在我知道发生这个问题是因为 MvcController 进入另一个程序集 但我不知道如何解决这个问题

注意:只有 MvcController 的通用实现位于 OxygenFramework 程序集中 所有的 Controller 都放在默认的 Controller 文件夹中

最佳答案

经过多次调查我发现了问题 我想说@Agat谢谢你:)

但是解决方案: 我在我的框架中使用了 System.Web.Mvc.dll 版本 4.0,但在我的 MvcApplication 中使用了 System.Web.Mvc.dll 5.0! 这种干扰会导致继承的 404 错误:D

关于c# - Asp.NET MVC 5 中 Controller 的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19879895/

相关文章:

C# List<baseClass> - 有什么方法可以在其中存储继承的类吗?

c# - 我们如何递归地组装准备好的语句

c# - 如何通过 C#/Xamarin 中 webview 的链接调用 whatsapp?

c# - WPF 切换 UIElements 在 DataTemplate 中的可见性

asp.net-mvc - Azure FileShare 有什么好处?将文件存储在 Azure 中与将文件存储在 Blob 存储中是否是一个好的解决方案

javascript - 如何在 Google Closure 中使用 var_args 调用父类的构造函数?

c# - 为什么没有 string.Split(string) 重载?

jquery - 在下拉框中显示 "Loading..."

asp.net-mvc - 构建中等大小的 asp mvc - 使用 ninject 和创建对象

c++ - C++ 核心指南 C35 "A base class destructor should be either public and virtual, or protected and nonvirtual"对于接口(interface)类是否错误?