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 didn’t find any file that I can add this code. Do you know if there is a specific file that has this purpose?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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:
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.
Method 2
This kind of startup code typically goes in the Application_Start() method, Global.asax.cs file
Method 3
Use the following in the Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0