Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:45:07+00:00 2026-06-15T23:45:07+00:00

I want to store items in the application cache so it’s lazy loaded from

  • 0

I want to store items in the application cache so it’s lazy loaded from the main layout view.

I also want to be able to invalidate the cache, so if it’s invalid, next time when the items-collection is requested, it’s reloaded to that cache location.

Here’s what I’ve implemented:

In the controller:

protected IEnumerable<Slide> CachedSlides
{
  get { return HttpContext.Application[SlidesCacheKey] as IEnumerable<Slide>; }
  set { HttpContext.Application[SlidesCacheKey] = value; }
}

private void ClearSlides()
{
  CachedSlides = null;
}

[AllowAnonymous]
public IEnumerable<Slider> GetSlides()
{
  if (CachedSlides == null)
    CachedSlides = Context.Slides.OrderBy(p => p.SortOrder).ToArray();
  return CachedSlides;
}

I the view (better said in ‘a’ view, I want to be able to load it from every view):

@{
  var sliderController = new LevEl.Controllers.Admin.SliderController().  
  var sliderModel = sliderController.GetSlides();
}

It throws an exception because when I initialize the controller in the view, the HttpContext property returns null (which leads to a NullReferenceException).

Any other ways to implement this will also be welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:45:07+00:00Added an answer on June 15, 2026 at 11:45 pm

    First of all, you may want to consider using the Cache instead of the Application dictionary, specially since your cached data will expire at some point. Take a look at this question

    Also, consider if the controller method will just be used by the views because MVC will expose all public methods in the controller. If you don´t want this method to be freely accessed using an URL then set the [NonAction] attribute.

    Regarding your error, a quick way to fix it will be accessing the Application object through System.Web.HttpContext.Current in the implementation of the CachedSlides property of the controller.

    You could also set the ControllerContext when creating a new instance of the SliderController in the view. That way, the HttpContext in the controller will not return null when accessing the CachedSlides property:

    @{
        var sliderController = new LevEl.Controllers.Admin.SliderController();
        sliderController.ControllerContext = new ControllerContext(ViewContext.RequestContext, sliderController);
        var sliderModel = sliderController.GetSlides();
    }
    

    If it makes sense for you to have a base controller class that will handle all of these views, then getting the controller will be cleaner. You would just need to cast the ViewContext.Controller instance as that base controller class:

    var sliderController = ViewContext.Controller as BaseSlideController;
    var sliderModel = sliderController.GetSlides();
    

    However all of those approaches will require you to add that piece of code to every view. You may consider having a base class for all of your views that will require accessing the Slide collection:

    public abstract class SlidesEnabledView<T> : WebViewPage<T>
    {
        private IEnumerable<Slide> _slides;
        protected IEnumerable<Slide> Slides
        {
            get
            {
                if(_slides == null)
                {
                    var sliderController = ViewContext.Controller as BaseSlideController;
                    _slides = sliderController.GetSlides();
                }
                return _slides;
            }
        }
    }
    

    You will then add the @inherits tag to your views, so they inherit from the base class we have just created (When the @inherits is used, you cannot also use the @model, so in the @inherits you will bound the generic base view type ti a concrete model type). This will allow you to use the property defined in the base SlidesEnabledView view class. Assuming the namespace for SlidesEnabledView is Level.ViewClasses.Admin, this would look like:

    @inherits Level.ViewClasses.Admin.SlidesEnabledView<SomeViewModelClass> 
    
    Number of Slides: @Slides.Count() 
    

    Finally, if you are using DI through your site and you have configured a DependencyResolver, then you could consider moving the logic for getting the slides into its own class and interface like ISlidesProvider and CachedSlideProvider. You could then use property injection in the abstract view class for getting an instance of ISlidesProvider that will be used in the Slides property:

    public interface ISlidesProvider
    { 
        IEnumerable<Slide> GetSlides();
    }
    
    public class CachedSlideProvider : ISlidesProvider
    {
        //you will need a constructor that takes the "Context" object, which will be injected into this class
    
        public IEnumerable<Slide> GetSlides()
        {
            if (CachedSlides == null)
                CachedSlides = Context.Slides.OrderBy(p => p.SortOrder).ToArray();
            return CachedSlides;
        }
    
        private IEnumerable<Slide> CachedSlides
        {
            get { return System.Web.HttpRuntime.Cache[SlidesCacheKey] as IEnumerable<Slide>; }
            set { System.Web.HttpRuntime.Cache[SlidesCacheKey] = value; }
        }
    }
    
    public abstract class SlidesEnabledView<T> : WebViewPage<T>
    {
        private IEnumerable<Slide> _slides;
        protected IEnumerable<Slide> Slides
        {
            get
            {
                if(_slides == null)
                {
                    _slides = this.SlidesProvider.GetSlides();
                }
                return _slides;
            }
        }
    
        //This property will be set by your DI container
        //You have configured ISlidesProvider to be resolved as CachedSlidesProvider in the DI container
        //You have also implemented and registered an MVC DependencyResolver that uses your DI container
        //For example, using Microsoft Unity you could set this attribute
        [Dependency]
        public ISlidesProvider SlidesProvider { get; set; }        
    }
    

    As you were creating an instance of the controller using a parameterless constructor, probably you are not using DI on your site. So this DI option may be a bit of overkill just for resolving this particular problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to store various Canvas items in seperate XAML files
I want to use Context.Items to store some info of a User Control like:
I want to store the password used for signing in a financial application that
My application is a store selling fishes, aquariums etc. I want to get a
I'm working with a rather large .net web application. Users want to be able
I am creating a JSF application. I have some items (e.g. products) from database
I want to develop a java application where a user should be able to
I want to write a calendar application. It is really recurring items that throw
I'm using mongodb to store application error logs as json documents. I want to
I want to store a price for an item in decimal in MySQL. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.