2011-07-15 113 views
3

我有一個網絡應用程序使用Asp.net MVC2。我將其升級到MVC 3,現在我發現OutputCache功能無法正常工作。我創建了一個簡單的測試操作,如下所示。從MVC 2升級後,Asp.net MVC 3不能與Ninject一起工作?

[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(); 
     } 
    } 
+0

奇怪,我無法重現該問題。緩存適用於我。 –

+0

如果我創建一個新的Mvc3應用程序,它工作正常。它只在升級的應用程序,我有這個問題。 – Amitabh

+0

這一切都沒有意義。對我來說,這個a)不能被重新編譯b)僅僅是沒有意義 - Ninject只是和'OutputCacheAttribute'沒有關係,所以必須有一些東西搞砸了,比如有多個Ninject或MVC版本在玩。另請參閱https://groups.google.com/forum/?fromgroups=#!topic/ninject/NtsBWNS4MJs –

回答

6

在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

請嘗試以下步驟,也許您的問題將得到解決。

+0

非常感謝。它的工作現在很棒。 – Amitabh

相關問題