Does asp.net MVC have Application variables?

本小妞迷上赌 提交于 2019-11-26 19:46:33

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();

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?"

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);

Make a static class?

Rohul Amin

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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!