Random Notes and simple code on Developing Web Sites (Applications) in ASP.net using C# #19

Random Notes and simple code on Developing Web Sites (Applications) in ASP.net using C#

  • The information below primarily apply to the web page model of web-development in C#
  • _PageStart.cshtml runs at each page start/open
  • _AppStart.cshtml runs at the beginning of the application
  • Syntax to use the Layout file: @{Layout = “/Shared/Layout.cshtml”;}
  • if you use this – @{Layout = /Shared/Layout.cshtml”;} in the _PageStart.cshtml file, you will not need to use it at the top of each file
  • You can use Microsoft Webmatrix, Visual Web Developer, or Visual Studio for web development
  • Webmatrix, includes IIS Express, and also a database workspace where you can create and use compact SQL Server Databases
  • DateTime.Now : will display current date and time
  • You can connect to a database using var db = Database.Open(“db_name”);
  • db = Database.Open(“db_name”); will look for the database in the same workspace; if not found will use the connection parameter as defined in the web.config file to find and connect to database
  • Executing a query: var queryString = “SELECT * FROM Product ORDER BY Name”; db.Query(queryString)
  • Accessing each row from a rowset of data as returned from database:
                   foreach(var entry in db.Query((queryString))){                @entry.Id, @entry.Name            }               
  • if (IsPost) : will check if a form is submitted by POST or not
  • username = Request.Form[“username”]; Will retrieve username from the Request object [submitted form with control named username]
  • A basic login authentication code can be as follows
               var username = "";      var password = "";      var ErrorMessage = "";              // If this is a POST request, validate and process data     if (IsPost){                        username = Request.Form["username"];            password = Request.Form["password"];                        if (username.IsEmpty() || password.IsEmpty()){              ErrorMessage = "You must specify a username and password.";         }else{              // Login, Navigate back to the homepage and exit                if (WebSecurity.Login(username, password, false)){                  Response.Redirect("~/");                }               else{                   ErrorMessage = "Login failed";              }           }       }                                       @if (ErrorMessage!="") {            @ErrorMessage, Please correct the errors and try again.     }   
  • Execute an Insert Statement: db.Execute(“INSERT INTO UserProfile (Email) VALUES (@0)”, email);
  • WebSecurity object can be used for the security of your web-site (application) such as user authentication, block a page until a user is authenticated
  • Block user if he is not logged in [can be added at the top of the page to be blocked]
               @{              if (!WebSecurity.IsAuthenticated) Response.Redirect("~/Login"); //redirect to login page            }       
  • WebSecurity.InitializeDatabaseConnection(“db_name”, “profile_of_the_users_table”, “UserId”, “Email”, true);
  • WebSecurity.CreateAccount(email, password, false);

From: http://sitestree.com/?p=5298
Categories:19
Tags:
Post Data:2012-01-30 09:33:18

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>