gpt4 book ai didi

从 MVC 2 升级后,Asp.net MVC 3 无法与 Ninject 一起使用?

转载 作者:行者123 更新时间:2023-12-02 18:53:55 26 4
gpt4 key购买 nike

我有一个使用 Asp.net MVC2 的 Web 应用程序。我将其升级到MVC 3,现在我发现OutputCache功能不再起作用。我创建了一个简单的测试操作,如下所示。

 [OutputCache(Duration = 1000000, VaryByParam = "none")]
public virtual ActionResult CacheTest(string name)
{
string message = string.Format("{0}: Time is {1}", name, DateTime.Now.ToLongTimeString());
ViewData.Add("Message", message);
return View();
}

这始终给出当前时间,表明它未缓存。我在这里遗漏了什么吗?

更多信息:如果我创建一个新的 Mvc3 应用程序,它可以正常工作。只有在升级后的应用程序中我才遇到此问题。

更新:我也在使用 Ninject。如果我停止使用 Ninject OutputCache 就会开始工作。

public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // Route name
"{controller}/{action}.aspx/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

}

protected void Application_Start()
{
RegisterDependencyResolver();

AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);
}

protected void RegisterDependencyResolver()
{
var kernel = CreateKernel();
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}

protected IKernel CreateKernel()
{
return new StandardKernel();
}
}

最佳答案

在 ASP.NET MVC 3 中使用 Ninject 的正确且推荐的方法如下:

  1. 安装ninject.mvc3 NuGet 包。这将确保您获得与 ASP.NET MVC 3 兼容的最新版本。

  2. 安装后,它将向您的项目添加一个 App_Start/NinjectMVC3.cs 文件,并且您将在 RegisterServices 方法中注册您的 Ninject 模块:

    private static void RegisterServices(IKernel kernel)
    {
    var modules = new INinjectModule[]
    {
    // your modules here
    };
    kernel.Load(modules);
    }
  3. 从 Global.asax 中删除所有 Ninject 特定代码,包括任何 NinjectDependencyResolver

尝试按照这些步骤操作,也许您的问题就会得到解决。

关于从 MVC 2 升级后,Asp.net MVC 3 无法与 Ninject 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6705354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com