C# asp.net MVC 有应用程序变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2266533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:54:17  来源:igfitidea点击:

Does asp.net MVC have Application variables?

c#asp.net-mvcapplication-state

提问by Mark Redman

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. Do MVC applications have Application Variables? I have seen some examples where caching is used? Is this now standard and How robust/secure is this compared to Application/Session State?

我意识到 MVC 的重点是让事物尽可能无状态,会话状态显然在 MVC 中存在并存在是有意义的,但我们不想只是将应用程序转换为会话变量,因为我们宁愿拥有更全局和更安全的东西。MVC 应用程序有应用程序变量吗?我看过一些使用缓存的例子吗?这是现在的标准吗?与应用程序/会话状态相比,它的健壮性/安全性如何?

采纳答案by WWC

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

是的,您可以从 .NET MVC 访问应用程序变量。就是这样:

System.Web.HttpContext.Current.Application.Lock();
System.Web.HttpContext.Current.Application["Name"] = "Value";
System.Web.HttpContext.Current.Application.UnLock();

回答by svinto

Make a static class?

做一个静态类?

回答by used2could

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.

然而,我会创建一个使用缓存存储作为支持的静态类。

回答by used2could

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. These are all replaced with mock-able instances.

静态类在这里似乎是一个流行的选择。然而,静态类会在您的类型之间创建依赖关系,并使版本控制/测试变得更加困难。在旨在打破这些依赖关系的框架中使用它也是一种奇怪的模式。例如,标准的 ASP.NET 框架充斥着静态和密封类型。这些都替换为可模拟的实例。

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

在这种情况下,“安全”有点不清楚。您所说的“安全”究竟是什么意思?

回答by dynamiclynk

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

回答by Rohul Amin

You can declare Application variables in Application_Startlike this:

您可以Application_Start像这样声明应用程序变量:

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;