Does asp.net MVC have Application variables?

后端 未结 6 2177
感动是毒
感动是毒 2020-11-27 13:31

I am busy converting a web application to MVC and have some information saved to Application variables used across multiple tenants/accounts to make things a bit more effici

相关标签:
6条回答
  • 2020-11-27 13:59

    Yes, you can access Application variables from .NET MVC. Here's how:

    System.Web.HttpContext.Current.Application.Lock();
    System.Web.HttpContext.Current.Application["Name"] = "Value";
    System.Web.HttpContext.Current.Application.UnLock();
    
    0 讨论(0)
  • Do they have Application Variables? Yes, MVC is a framework that sits on top of the normal asp.net framework.

    I would however create a static class that uses a cache store as it's backing.

    0 讨论(0)
  • 2020-11-27 14:00

    Make a static class?

    0 讨论(0)
  • 2020-11-27 14:02

    You can declare Application variables in Application_Start like this:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    
        var e = "Hello";
        Application["value"] = e;
    }
    

    To access this on controller write:

    string appVar = HttpContext.Application["value"] as string;
    
    0 讨论(0)
  • 2020-11-27 14:18

    Session state or the Cache are better choices. They are mockable in MVC and are designed to store session and application-scoped data.

    Static classes seems like a popular choice here. However static classes create dependencies between your types and make versioning/testing harder. Its also a bit of an odd pattern to use in a framework that is designed to break apart these kinds of dependencies. For instance, the standard ASP.NET framework is riddled with statics and sealed types. These are all replaced with mock-able instances.

    "Secure" is a bit unclear in this context. Exactly what do you mean by "secure?"

    0 讨论(0)
  • 2020-11-27 14:20

    I implemented something like below as an Extension for Global state variable. I put things like Site title,Service Endpoints, authorized roles

    public static class ApplicationStateExtension
     {
        public static T GetSetApplicationState<T>(this HttpApplicationState appState, string objectName, object objectValue = null, int syncCheckMinutes = 0)
        {
            T retVal = default(T);
            appState.Lock();
            if (appState[objectName + "LastSync"] == null || DateTime.Now.Subtract(((DateTime)appState[objectName + "LastSync"])).TotalMinutes >= syncCheckMinutes)
            {
                appState[objectName + "LastSync"] = DateTime.Now;
    
                if (objectValue != null)
                    appState[objectName] = objectValue;
            }
            if (appState[objectName] != null)
                retVal = (T)appState[objectName];
            appState.UnLock();
            return retVal;
        }
        public static object GetSetApplicationState(this HttpApplicationState appState, string objectName, object objectValue = null, int syncCheckMinutes = 0)
        {
            object retVal = null;
            appState.Lock();
            if (appState[objectName + "LastSync"] == null || DateTime.Now.Subtract(((DateTime)appState[objectName + "LastSync"])).TotalMinutes >= syncCheckMinutes)
            {
                appState[objectName + "LastSync"] = DateTime.Now;
    
                if (objectValue != null)
                    appState[objectName] = objectValue;
            }
            if (appState[objectName] != null)
                retVal = appState[objectName];
            appState.UnLock();
            return retVal;
        }
        public static void SetApplicationState(this HttpApplicationState appState, string objectName, object objectValue, int syncCheckMinutes = 0)
        {
            appState.Lock();
            if (appState[objectName + "LastSync"] == null || DateTime.Now.Subtract(((DateTime)appState[objectName + "LastSync"])).TotalMinutes >= syncCheckMinutes)
            {
                appState[objectName + "LastSync"] = DateTime.Now;
                appState[objectName] = objectValue;
            }
            appState.UnLock();
        }
        public static object GetApplicationState(this HttpApplicationState appState, string objectName)
        {
            object retVal = null;
            appState.Lock();
            if (appState[objectName] != null)
                retVal = appState[objectName];
            appState.UnLock();
            return retVal;
        }
        public static T GetApplicationState<T>(this HttpApplicationState appState, string objectName)
        {
            T retVal = default(T);
            appState.Lock();
            if (appState[objectName] != null)
                retVal = (T)appState[objectName];
            appState.UnLock();
            return retVal;
        }
    }
    

    So I can set them from Global.asax.cs something like this

    Application.SetApplicationState("UISiteTitle",paramHelper.GetUIConfigXML<XMLParams.UISiteOptions>("UISiteOptions")
                    .SiteOptionCollection.Where(v => v.name.Equals("title", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault().value););
    

    or

    var uiPermissions = Application.GetSetApplicationState<XMLParams.UIPermissions>("UIPermissions", paramHelper.GetUIConfigXML<XMLParams.UIPermissions>("UIPermissions"), 30);
    
    0 讨论(0)
提交回复
热议问题