I want when my application starts, to execute some code
我想在我的应用程序启动时执行一些代码
if (!WebMatrix.WebData.WebSecurity.Initialized){
WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
There is a folder App_start at the project, but I did not find any file that I can add this code. Do you know if there is a specific file that has this purpose?
项目中有一个文件夹App_start,但我找不到任何可以添加此代码的文件。你知道是否有一个具有这个目的的特定文件?
Thanks a lot
非常感谢
3 个解决方案
#1
20
This kind of startup code typically goes in the Application_Start()
method, Global.asax.cs file
这种启动代码通常位于Application_Start()方法Global.asax.cs文件中
#2
33
Put your code in static method inside a class.
将代码放在类中的静态方法中。
public static class SomeStartupClass
{
public static void Init()
{
// whatever code you need
}
}
Save that in App_Start
. Now add it to Global.asax
, along with the other code MVC initialises here:
将其保存在App_Start中。现在将它添加到Global.asax,以及其他代码MVC初始化:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
SomeStartupClass.Init();
}
Now your startup code is separated nicely.
现在你的启动代码很好地分开了。
#3
2
Use the following in the Global.asax:
在Global.asax中使用以下内容:
protected void Application_Start(object sender, EventArgs e)
{
#1
20
This kind of startup code typically goes in the Application_Start()
method, Global.asax.cs file
这种启动代码通常位于Application_Start()方法Global.asax.cs文件中
#2
33
Put your code in static method inside a class.
将代码放在类中的静态方法中。
public static class SomeStartupClass
{
public static void Init()
{
// whatever code you need
}
}
Save that in App_Start
. Now add it to Global.asax
, along with the other code MVC initialises here:
将其保存在App_Start中。现在将它添加到Global.asax,以及其他代码MVC初始化:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
SomeStartupClass.Init();
}
Now your startup code is separated nicely.
现在你的启动代码很好地分开了。
#3
2
Use the following in the Global.asax:
在Global.asax中使用以下内容:
protected void Application_Start(object sender, EventArgs e)
{