开发者

Asp.net MVC 3 not working with Ninject after upgrade from MVC 2?

开发者 https://www.devze.com 2023-03-20 18:02 出处:网络
I have a web application which was using Asp.net MVC2. I Upgraded it to MVC 3 and now I found that OutputCache feature is not working any-more. I created a simple Test action as shown below.

I have a web application which was using Asp.net MVC2. I Upgraded it to MVC 3 and now I found that OutputCache feature is not working any-more. I created a simple Test action as shown below.

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

This always gives the current time which shows that it is not cached. Am I missing something here?

More Info: If i create a new Mvc3 app it works fine. Its only in the upgraded app that I have this issue.

Update: I am also using Ninject. If I stop using Ninject OutputCache starts working.

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


The correct and recommended way to use Ninject in ASP.NET MVC 3 is the following:

  1. Install the ninject.mvc3 NuGet package. This will ensure that you get the latest version compatible with ASP.NET MVC 3.

  2. Once installed this will add a App_Start/NinjectMVC3.cs file to your project and it is inside the RegisterServices method that you will register your Ninject modules:

    private static void RegisterServices(IKernel kernel)
    {
        var modules = new INinjectModule[]
        {
            // your modules here
        };
        kernel.Load(modules);
    }        
    
  3. Remove all Ninject specific code from your Global.asax including any NinjectDependencyResolver.

Try following those steps and maybe your problem will get fixed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号