1

Recently I inherited an ASP.NET MVC 4 application which is using global.asax to share data across Views.

public class MvcApplication : System.Web.HttpApplication
{
    public static List<Models.Feed> feedItems;

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        GetDataFromWebService(); //expensive call
    }       
}

There are many views that use the feedItems like:

@foreach (var item in MvcApplication.feedItems)
{
    <li>@item.Title</li>                  
}

I believe the above implementation is incorrect (since the feedItems data doesn't change unless the app pool is recycled) and there should be a better way of doing this. My requirements are:

  1. Call the GetDataFromWebService method once.
  2. Share the feedItems across multiple views.

I have looked at this SO post but I'm not sure if this is suitable in my scenario, since I want the web service to be hit only one time.

What would be the best way to do this?

Thanks!

3
  • I don't understand why this is incorrect. You want to call the method once and the way you did it, it is called once in application lifetime. Commented Mar 31, 2015 at 15:30
  • Need the method to be called once per session and not once per application lifetime. Commented Mar 31, 2015 at 15:33
  • So your variable should be stored in session, not with a static scope. Commented Mar 31, 2015 at 15:35

1 Answer 1

2

Store the variable in the session scope if you want to get it once per session.

Global.asax

   protected void Session_Start(Object sender, EventArgs e)
   {
      HttpContext.Current.Session["WebServiceData"] = GetDataFromWebService();
   }

You can use the session from a razor view.

View

@foreach (var item in (IEnumerable<WebServiceItem>)Session["WebServiceData"])
{
    <li>@item.Title</li>
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for the stupid question. My bad. Didn't go through the global.asax methods.
@WhoAmI your question isn't stupid. Maybe it could be rewriten to be more explicit about how you expected it to work. You have to learn about different scopes availables in asp.net (Session, Cache, QueryString, Form inputs, Request...) and why you should avoid static. Session should be avoided if you want a better scalability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.