none
Why Application variable is not available in Application_Start in Global.asax.cs in ASP.NET MVC 4 though in ASP.NET we can? RRS feed

  • Question

  • In the Application_Start event I want to store some value in Application variable so that any user can use the value in the whole application. eg. Application["xyz"]="Hello World";

    Though it was possible in ASP.NET but in ASP.NET MVC 4 ,why I am unable to write Application variable in Application_Start.It is giving me compilation error.

    Though the way round is to store the value in HttpContext.Current.Application, my second question is HttpContext.Current.Application is per user context Application object ,how it is storing the global value for all users?

    Wednesday, August 9, 2017 10:54 AM

Answers

  • I tell you to use static variables instead is just because that's the way Microsoft recommends you to use.

    Honestly speaking, unless you're directly using old ASP code, there is little reason to use HttpAppication.Application to store anything.


    Since it does not work for you now, IMO it's a good time to "correct the inefficient leftover code".
    • Edited by cheong00 Friday, August 11, 2017 1:23 AM
    • Marked as answer by nirmallyakoley Wednesday, August 30, 2017 2:48 AM
    Friday, August 11, 2017 1:16 AM

All replies

  • If you only need to share the value within worker process instance, you can just create a static variable and host it in any class within your web application.

    To share the value across servers (say, web servers on load balancer), you'll need to save the value on common medium such as SQL server tables.

    Thursday, August 10, 2017 6:03 AM
  • In fact you should use "HttpContext.Application" in the Controller, while the Application property defined in the HttpApplication will be initialized automatically and Application_OnStart will be called.

    Here're the full codes:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace WebApplication1
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                RouteConfig.RegisterRoutes(RouteTable.Routes);
            }
    
            protected void Session_Start(object sender, EventArgs e)
            {
                Application.Lock();
                object count = Application["count"];
                if (count == null)
                {
                    count = 0;
                }
                else
                {
                    count = (int)count + 1;
                }
                Application["count"] = count;
                Application.UnLock();
            }
        }
    }

    Controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace WebApplication1.Controllers
    {
        public class DefaultController : Controller
        {
            // GET: Default
            public ActionResult Index()
            {
                ViewBag.UserCount = HttpContext.Application["count"].ToString();
                return View();
            }
        }
    }

    View:

    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <h1>The total number on line is: @ViewBag.UserCount.ToString()</h1>
    </head>
    <body>
        <div> 
        </div>
    </body>
    </html>

    Actual running result:


    Reproduce your quesions with ScreenToGif is your choice.
    For IIS: IIS Forum,
    For WebSite of .NET, ASP.NET Forum,
    For others, StackExchange.
    For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com

    Thursday, August 10, 2017 8:59 AM
  • My intention is not to share value across webfarm or across worker process. My simple question is, in ASP.NET application ,in Global.asax there is a  Application_Start event. If we want to store application specific data we can store it in Application variable.

    ie. Application["abc"]="something  simple";

    But in ASP.NET MVC we can not write Application["abc"]="...." in Application_Start in Global.asax. Why? 

    Thursday, August 10, 2017 11:25 AM
  • My intention is not to share value across webfarm or across worker process. My simple question is, in ASP.NET application ,in Global.asax there is a  Application_Start event. If we want to store application specific data we can store it in Application variable.

    ie. Application["abc"]="something  simple";

    But in ASP.NET MVC we can not write Application["abc"]="...." in Application_Start in Global.asax. Why? 

    I can write Application[……]. Application is the property from HttpApplication.

    Reproduce your quesions with   ScreenToGif is your choice.
    For IIS: IIS Forum,
    For WebSite of .NET, ASP.NET Forum,
    For others, StackExchange
    For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com

    Friday, August 11, 2017 1:10 AM
  • I tell you to use static variables instead is just because that's the way Microsoft recommends you to use.

    Honestly speaking, unless you're directly using old ASP code, there is little reason to use HttpAppication.Application to store anything.


    Since it does not work for you now, IMO it's a good time to "correct the inefficient leftover code".
    • Edited by cheong00 Friday, August 11, 2017 1:23 AM
    • Marked as answer by nirmallyakoley Wednesday, August 30, 2017 2:48 AM
    Friday, August 11, 2017 1:16 AM
  • Hi nirmallyakoley,

    According to your question is more related to MVC, You could post a new thread in ASP.NET forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 30, 2017 1:58 AM