c# - Share Data across ASP.NET MVC application -


recently inherited asp.net mvc 4 application using global.asax 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 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 many views use feeditems like:

@foreach (var item in mvcapplication.feeditems) {     <li>@item.title</li>                   } 

i believe above implementation incorrect (since feeditems data doesn't change unless app pool recycled) , there should better way of doing this. requirements are:

  1. call getdatafromwebservice method once.
  2. share feeditems across multiple views.

i have looked @ this post i'm not sure if suitable in scenario, since want web service hit 1 time.

what best way this?

thanks!

store variable in session scope if want once per session.

global.asax

   protected void session_start(object sender, eventargs e)    {       httpcontext.current.session["webservicedata"] = getdatafromwebservice();    } 

you can use session razor view.

view

@foreach (var item in (ienumerable<webserviceitem>)session["webservicedata"]) {     <li>@item.title</li> } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -