Did something change with how HttpContext.Items and MVC cont

Posted by Community Admin on 04-Aug-2018 15:32

Did something change with how HttpContext.Items and MVC controls works?

All Replies

Posted by Community Admin on 02-May-2014 00:00

I upgraded a 6.3 site that has a few MVC widgets that use HttpContext to communicate with each other and then display data.  This works fine in my 6.3 site, but when I upgraded it to 7.0 these widgets no longer render any content.

 Through debugging I found out that at some point HttpContext.Items is empty.

Here is how I have it setup:

I have a manager widget where I assign what category I want to pull images from.  This manager should then create the collection and store it in HttpContext so that another widget can pull from it.

Here are the actions:

public ActionResult Index()
        
            var model = InitializeModel();
            return View("Default");
        
 
        protected override void HandleUnknownAction(string actionName)
        
            var model = InitializeModel();
            View("Default").ExecuteResult(this.ControllerContext);
        

I am not going to post all the model code, but at the end of that InitializeModel() it checks this:

if (model.Tombstones != null && model.Tombstones.Count > 0)
            
                if (HttpContext.Items["Tombstones"] == null)
                    HttpContext.Items.Add("Tombstones", model.Tombstones);
            
 
            return model;

Now I used the VS debugger and confirmed at this point that there is indeed a collection being found and it does create the HttpContext.Items["Tombstones"] object

 Now on the same page I have a second widget that will check for that object and then call out to its view to render the collection of images.

public ActionResult Index()
        
            InitializeControls();
            return View("Default");
        
 
        protected override void HandleUnknownAction(string actionName)
        
            InitializeControls();
            View("Default").ExecuteResult(this.ControllerContext);
        
 
        #region Methods
 
        private void InitializeControls()
        
            if (HttpContext.Items["Tombstones"] != null)
                ViewBag.Tombstones = HttpContext.Items["Tombstones"];
        
 
        #endregion

In my 6.3 site I can see in VS that HttpContext.Items has roughly 83 items and HttpContext.Items["Tombstones"] has the collection of images I want.  The 7.0 site shows that HttpContext.Items has a count of "0" so nothing ever renders.

 My manager view does not render anything and is only to setup the HttpContext.  My images view looks something like this.

 

@using SitefinityWebApp.Mvc.Models.AdsManager
 
@if (ViewBag.Tombstones != null)
    foreach (TombstoneAd item in ViewBag.Tombstones.Values)
    
        <a href="@item.NavigateUrl" target="_blank">
            <img class="border" src="@item.ImageUrl" alt="@item.Caption" width="@item.Width" height="@item.Height" />
        </a>
 
    

Posted by Community Admin on 02-May-2014 00:00

I think I might have a solution that works.  If I use SystemManager.CurrentHttpContext which is Sitefinity's wrapper to store the information it seems to be a happy camper.

This thread is closed