简体   繁体   English

asp.net MVC有Application变量吗?

[英]Does asp.net MVC have Application variables?

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 efficient. 我正在忙着将Web应用程序转换为MVC,并将一些信息保存到跨多个租户/帐户使用的应用程序变量中,以使事情更有效率。

I realise the point of MVC is to keep things as stateless as possible, Sesion State obviously makes sense to have and exists in MVC but we dont want to just convert Application to Session variables as we would rather have something more global and more secure. 我意识到MVC的目的是让事情尽可能无状态,Sesion State显然在MVC中存在并存在,但我们不想将Application转换为Session变量,因为我们宁愿拥有更全局和更安全的东西。 Do MVC applications have Application Variables? MVC应用程序是否具有应用程序变量? I have seen some examples where caching is used? 我看过一些使用缓存的例子? Is this now standard and How robust/secure is this compared to Application/Session State? 这是现在的标准吗?与应用程序/会话状态相比,这是多么强大/安全?

Yes, you can access Application variables from .NET MVC. 是的,您可以从.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. 它们在MVC中是可模拟的,旨在存储会话和应用程序范围的数据。

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. 例如,标准的ASP.NET框架充满了静态和密封类型。 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 所以我可以从Global.asax.cs中设置它们

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

做一个静态课?

You can declare Application variables in Application_Start like this: 您可以在Application_Start声明Application变量,如下所示:

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. 是的,MVC是一个位于普通asp.net框架之上的框架。

I would however create a static class that uses a cache store as it's backing. 然而,我会创建一个静态类,它使用缓存存储作为它的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM