São Paulo, 23/04/2014   

Creating code for coding - Generating a functional website in asp.net with one click

Creating code for coding - Generating a functional website in asp.net with one click


Download C# source code



Introduction


Have you ever felt like doing same tasks or writing similar code repetitive times?

For example creating a new web application, creating a database, running aspnet_regsql.exe, creating roles, writing web.config, creating a new form for login, coding the login...

What about having it at once with one click at all?


Background


Some years ago I was faced to an "easy request"; convert an excel file into an application.
The deadline was one week, easy huh?
Opening the excel file it was nothing less than hundreds of sheets, this day I have decided to create a virtual programmer i.e. code to create code.
In one week the application was done. 


How it works?


Basically the idea of the virtual programmer has the following features:

  • Read a template file, substitute tags, write an output file;  

  • Perform configuration operations. 

The current article shows up how to get in one click the following tasks/code:

  • Create a database; 

  • Run aspnet regsql to create membership tables; 

  • Create web.config; 

  • Create some roles and users; 

  • Create the Login form and code behind;

  • Have a startup project with SQL Authentication. 

Note: Yes! There are many tools to perform the same task! But the idea here is the approach of creating code for coding helping to get rid of doing repetitive work and saving time. It means more time for coffee :-) 


Creating a database 



Our first task is to create a database, we can do this by the following code: 


... SqlCommand command = connection.CreateCommand(); command.CommandText = "CREATE DATABASE " + dbname; connection.Open(); command.ExecuteNonQuery(); connection.Close(); ...  // ! removed validations in the example above 


Executing aspnet.regsql  



Now we should run aspnet_regsql manually once again. Instead we will call SqlServices which will perform the same operation to create the aspnet tables in the DB.


... SqlServices.Install(dbname, SqlFeatures.All, connectionstring); ... 


Creating the web.config 



The next task should be edit manually the web.config... But why? 

We will read a template file, i.e. web_config.txt, perform a tag replacement and then save the output web.config file. 


sContents = TextFileHelper.ReadTextFile("web_config.txt");  sContents = sContents.Replace("@CONNECTIONSTRING@", connectionstring); sContents = sContents.Replace("@APPLICATIONNAME@", applicationname);  ... TextFileHelper.WriteTextFile("web.config", sContents);   ... 


Adding roles and users 



To add roles and users to the aplication we do have again template files. They are configure_aspx.txt and configure_aspx_cs.txt.

The concept is the same: read the template file, substitute the tags and create the output files configure.aspx and configure.aspx.cs.  


sContents = TextFileHelper.ReadTextFile("configure_aspx_cs.txt");  sContents = sContents.Replace("@ROLE1@", role1); sContents = sContents.Replace("@ROLE2@", role2);  ... sContents = sContents.Replace("@USER1@", user1); sContents = sContents.Replace("@USER2@", user2);  ...  sContents = sContents.Replace("@USER1ROLE@", user1role); sContents = sContents.Replace("@USER2ROLE@", user2role);  ...  TextFileHelper.WriteTextFile("configure.aspx.cs", sContents);   ...



The code on configure.aspx.cs



UserHelper.AddRole("@ROLE1@"); UserHelper.AddRole("@ROLE2@"); ... UserHelper.CreateUser("@USER1@", "@PWD1@", "@ROLE1@"); UserHelper.CreateUser("@USER2@", "@PWD2@", "@ROLE2@");  ... 

After running the code above it is just call the configure page configure.aspx through the browser and voila you do have the skeleton of an asp.net application with SQL Authentication. 



Running the application


 


Fill up the application name and your connection string.


Click Proceed and voila, you do have a database and a functional website. 


 


The website files are generated one folder under your application exe / Output / chosen application name.


Now in VS go to File / Open Website and open your new application. 




Set your virtual path to / 




And run the configure.aspx. It will create the roles and an admin user. 



Log using the credentials admin adminpwd 


And here you are at default.aspx 




Points of Interest 


This is only a sample where you can code to create your code. 

Think on the tasks you repetitively perform and create your own virtual programmer.


Cheers!