简体   繁体   English

如何在ASP.NET MVC中初始化所需的全局变量?

[英]How do I initialize needed global variables in ASP.NET MVC?

In this article there is an explanation of the way of creating SharePoint 2013 add-in in web forms, I need MVC though. 这个文章有一种创造的SharePoint 2013的附加在网页表单的方式解释,我需要MVC虽然。

They initialize accessToken, currentUser, and other variables in the Default.aspx.cs file: 它们初始化Default.aspx.cs文件中的accessToken,currentUser和其他变量:

In the Default.aspx.cs file of your web application, add these variables inside the Page class. 在Web应用程序的Default.aspx.cs文件中,将这些变量添加到Page类中。

SharePointContextToken contextToken;
string accessToken;
Uri sharepointUrl;
string siteName;
string currentUser;
List<string> listOfUsers = new List<string>();
List<string> listOfLists = new List<string>();

Does it really matter where I initialize it in MVC and if it does, where do I have to initialize these? 我在MVC中将其初始化在哪里真的有关系吗,如果有的话,我必须在哪里进行初始化? Is HomeController okay? HomeController可以吗?

Sorry for the question's being probably dumb as I am new to both SharePoint and ASP.NET MVC. 抱歉,由于我对SharePoint和ASP.NET MVC都是陌生的,这个问题可能很愚蠢。

PS: The question is indirectly connected with SharePoint so I asked it there and not at SharePoint StackExchange PS:该问题与SharePoint间接相关,所以我在那里而不是在SharePoint StackExchange上询问

You can declare global variables in app_code folder of your solution,and use it where ever you need. 您可以在解决方案的app_code文件夹中声明全局变量,并在需要时使用它。

In App-code you need to add a class file like Global.cs(you can give any name) and there you can declare global variables. 在应用程序代码中,您需要添加一个诸如Global.cs的类文件(您可以输入任何名称),然后可以在其中声明全局变量。

Create one BaseController and define required variable as public constant/read only , then inherit this BaseController to your Controller , you can access that variable. 创建一个BaseController并将所需变量定义为public constant / read ,然后将此BaseController 继承 到Controller ,您可以访问该变量。

Please look at here for more details: 请在此处查看更多详细信息:

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

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