0

I'm working on a ASP.NET Core MVC web app. I have a Model that includes a Dictionary. In one Action I'm adding a new element to it. Then I have other actions supposed to use the object from the Dictionary that was just added. But as it turns out - the dictionary is empty after the first action finished executing. Is there a way to fix it, so that the object is added permanently to the dictionary?

Update: Well, the object I need to store is basically a virtual medical slide with a Deep Zoom tile generator. The flow is as follows: user click on the link to open the slide -> the ViewSlide Action creates the slide object -> then the OpenSeadragon viewer on the corresponding view sends requests to get XML metadata and JPEG tiles (256x256) on various Deep Zoom levels (based on mouse cursor position). So there's going to be a lot of requests for the tiles and I'm looking for a way to optimize the time needed to create them.

Here's a code snippet of the said actions:

[Route("[controller]/{slug}")]
public IActionResult ViewSlide(string slug)
{
    try
    {
        var currentSlide = slideSet.Get(slug);
        return View(currentSlide);
    }
    catch (Exception)
    {
        return RedirectToAction("Index");
    }

}

public Slide Get(string slideUrl)
{
    if (Slides.ContainsKey(slideUrl))
        return Slides[slideUrl];

    var pathToSlide = FilePaths[slideUrl];

    Slides[slideUrl] = new Slide(pathToSlide);

    return Slides[slideUrl];
}

[Produces("application/xml")]
[Route("[controller]/{slug}.dzi")]
public string Dzi(string slug)
{
    try
    {
        return slideSet.Get(slug).DeepZoomGenerator.GetDziMetadataString(DEEPZOOM_FORMAT);
    }
    catch (Exception)
    {
        RedirectToAction("Index");
        return "";
    }
}
Arquest
  • 1
  • 4

2 Answers2

0

If you want to add the item permanently you can store it in:

  1. Session (will not work in a web farm)
  2. Cookie
  3. Database
  4. File

Here is how to store it in session:

// Place something in session
System.Web.HttpContext.Current.Session["whatever"] = value;

// Read from session
var whatever = System.Web.HttpContext.Current.Session["whatever"];

MVC also provides TempData which is basically a session which lives during the lifecycle of the trip on the server.

Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
0

Depending on how you want to use this data, you have different options:

  • You can store it in the Session, Cookie, or TempData, if it's tied to the client, and no one else will need it. How long do you want to store the data? Cookies can be cleared, and you don't want to hold too much data in the Session either for a long time.
  • If the data does not belong to specific users, you can use a repository (e.g. singleton dictionary / database / HttpCache), but the first two needs to be cleaned regularly, while the HttpCache is not guaranteed to hold the data until it's requested.
  • And you could also rethink this concept, and stay stateless. This also makes it easier to scale your application horizontally, as well as adding HTTP cache, or even reverse proxy.

So basically it depends on what kind of data would you like to persist between action calls.

Zenima
  • 380
  • 2
  • 14
  • Well, the object I need to store is basically a virtual medical slide with a Deep Zoom tile generator. The flow is as follows: user click on the link to open the slide -> the ViewSlide Action creates the slide object -> then the OpenSeadragon viewer on the corresponding view sends requests to get XML metadata and JPEG tiles (256x256) on various Deep Zoom levels (based on mouse cursor position). So there's going to be a lot of requests for the tiles and I'm looking for a way to optimize the time needed to create them. – Arquest May 14 '17 at 08:03
  • @Arquest In this case I'd use a file-level cache for the XML and JPEG files, and implement a cleanup job to clear out old files. If you'll have a multi-tenant system, this can be placed on a file share, too. – Zenima May 14 '17 at 08:16