Asp.Net brief introduction




asp.net

Image credit: c2.staticflicker.com

Asp.Net brief introduction

The sole purpose of this post about Asp.Net brief introduction is to make you pleased by mentioning all those techniques, that are applied by every software engineer to pursue his career in the challenging field of Asp.Net development. The challenges which a new comer or even a professional programmer faces, have never been addressed in such an honest way as I am explaining to you.

Development Tools

Many ASP.NET developers use Microsoft’s Visual Studio to develop ASP.NET sites. But there are less expensive and free alternatives as well. Microsoft’s Visual Web Developer Express Edition is free. Web Matrix is useful in that it also provides a simplified version of the Internet Information Services (IIS), Microsoft’s web server. The version of IIS included with Web Matrix allows you to test locally hosted applications. Since January 2006 Microsoft has made available free Express Editions of Visual Studio, including Visual Web Developer, which uses the later ASP.NET 2.0, and also has the same local IIS (called Cassini). All require in addition the appropriate .NET framework also available from Microsoft, and for development the appropriate .NET framework SDK as well.

It is also possible to develop ASP.NET web applications using open source tools, or even simple text editors like Notepad.

Hosting

While most ASP.NET web applications are hosted using Microsoft’s IIS web server, the Mono Project provides a way to host ASP.NET projects on open source web servers such as Apache.

Languages

An unusual feature of ASP.NET is that it supports more than one programming language. There is, in fact, no language called “ASP” but rather a programming platform . The two most popular languages for ASP.NET development are Visual Basic .NET (VB.NET) and C# (pronounced “C sharp”).

Approaches to Coding

ASP.NET pages can include their VB.NET or C# code inline with the HTML/CSS. We will explore this method of creating ASP.NET pages first. But ASP.NET pages also offer the ability to greatly separate the logic of the web application from the presentation layer of the page. In this latter style, the C# or VB.NET code is kept in a separate file (called a “code-behind” file). Thus, ASP.NET pages using the code-behind method will actually contain two files: one a fairly traditional HTML file and a second file containing the bulk of the page logic.

A significant advantage of using code behind is that the code behind file must be pre-compiled. Pre-compiling the file allows it to execute faster and forces the person designing it to fix errors at compile-time, rather than run-time.

Your First ASP.NET Page

<%@ Page Language=”VB” %>

 

C#

<%@ Page Language=”C#” debug=”true” trace=”false”%>

 

As you can see, an ASP.NET page has a lot in common with a regular HTML-based page. Now in order to make our Hello World example we just need to add one line of code to the above examples.

VB.Net

Sub Page_Load()

‘On Page Load Run this code

Response.Write (“Hello World”)

End Sub

C#

void Page_Load() {

//On Page Load Run this Code

Response.Write(“Hello World”);

}

Copy the original code into a new file with the extension aspx. Then make the above modification. When the file is run, assuming it is being hosted on an ASP.NET compatible web server, you will see the words Hello World on your web browser when you access the page.

ASP.Net Membership Provider

As of ASP.Net Version 2.0 Microsoft has included a membership system, or Membership Provider, that can be easily integrated into any ASP.Net 2.0 web site that has a MSSql Database back-end. The new system is not limited to just logins but also integrates role based authentication, and profiles and takes away much of the hassle of having to create your own system to do the same thing.

First Steps: Setting Up The Database

Since the new Membership system relies on a MSSql Database back-end the first step is to make the necessary additions/changes to your web.config file.

If you do not currently have a connection string entry to your MSSql database inside your web.config file please reference the ASP.Net Database section first before continuing on to this next step.

Assuming that you have your database connecting string properly entered into your web.config file we will now configure your database and add the appropriate tables, stored procedures, etc… that are required. Fortunately Microsoft makes this fairly painless by providing a tool that will do the job for us! The tool is called aspnet_regsql.exe and is found at the following path normally: C:windowsmicrosoft.netFrameworkv2.0.xxxxxaspnet_regsql.exe. After you start the ASP.Net SQL Server Setup Wizard click Next. Select the option Configure SQL Server for application services from the next page, then click Next. At this point the Wizard is asking you for the path/location of your MSSQL Database. In the Server box type in the full path to the MSSQL server that you are using. If the SQL server is located on the current machine then type localhost into the box. If your server is located on a remote machine then you will most likely have to enter in a SQL authentication, unless you are using Windows Authentication.

Once you have all of the authentication information entered and you have selected the database that you wish to use from the drop down list click Next. The next screen will simply give you a summary of the information you have selected showing you the name of the server and database that the Wizard will install the Membership schema information onto. Click Next to install the Schema into your database. If all goes well you should get a success screen and you may click Finish.

Enabling Membership in Web.config

After the database schema has been updated the next step is to modify our Web.config file and enable the Membership Provider.

First open up your Web.config file. At the top of the page, or near the top, should be your Database Connection string that we entered in the Database section. Note the name that this connection has, such as LocalSqlServer, or whatever you have named it.

Inside the section of the Web.config file we copy and paste the following piece of code:


<configuration>
<system.web>
<membership defaultProvider=”SqlProvider” userIsOnlineTimeWindow=”15″>
<providers>
<clear />
<add
name=”SqlProvider”
type=”System.Web.Security.SqlMembershipProvider”
connectionStringName=”MySqlConnection”
applicationName=”MyApplication”
enablePasswordRetrieval=”false”
enablePasswordReset=”true”
requiresQuestionAndAnswer=”true”
requiresUniqueEmail=”true”
passwordFormat=”Hashed” />
</providers>
</membership>
<system.web>
<configuration>

Also inside the section of your Web.config file you will need to change your Authentication entry to look like the following:

<system.web>
<authentication mode=”Forms” >
<forms loginUrl=”login.aspx”
name=”.ASPXFORMSAUTH” />
</authentication>

Assuming that your connection string is correct, that you installed the Membership Schema correct, and you copied, pasted, and modified the above code appropriately there shouldn’t be much left before it will work. So let’s save our Web.config file and test it out.

Creating A New User

The best way to test our system is to try creating a user. Included in ASP.Net 2.0 are several Web Controls that should be in your Tool Box that are specifically designed for working with the Membership Provider. So let’s make a new Web Form in the root directory of our website. Drag and drop and CreateUserWizard from the Tool Box onto your new page. Save the page and then click the preview page button/load the page by hand in your web browser, then try filling in all the fields and making a new user. If all goes as it should you should be displayed a User Created Successfully box in your browser.

Logging in as Your New User

Now that we can make users let’s try logging in as our user. In the Tool Box there is also a Login Control that we can drag and drop onto the same page we made earlier. We will also want to add a LoginName Control so that after we are logged in we can verify who we are logged in as. After you have added these controls to the page save the page and load it in your web browser once again. Using the login box try to login using the account you made earlier. If all goes well you should login and the LoginName Control will be displaying your user name.

Overview Of Included Login/Authentication Related Controls

Login – Standard User name & Password login box. The Login Control is very customizable. When the login button is clicked the default action is to Post-Back the page and verify the users credentials.

LoginView – Allows you to show different content in a part of the page based upon whether the user is logged in or not logged in. The LoginView can also be used to display different content to Users based upon the Roles they are in (whether the user has logged in or not).

PasswordRecovery – Provides users access to recover their own passwords. Requires an answer to a password question.

LoginStatus – Shows the currently status of the user. If the user is logged in then this control will display a Logout link. If the user is not logged in the this user will display a Login link.

LoginName – Will display the user name of the currently logged in User. If no user is logged in then it will display nothing.

CreateUserWizard – A wizard that helps you create a new user account.

ChangePassword – Change password Control that allows users to change their own passwords, requires that you enter the old password.

An Overview of Master Pages

Masterpages were introduced for the first time in .NET framework 2.0 in order to create an application in which multiple files have similar content. The content for which masterpages have been created include headers, trailers, etc. For sites which have a common header for all the pages, it is difficult and hectic to ensure that the same header has been inserted into the code of each page and to ensure uniformity in text alignment, etc. To overcome this drawback, we use master pages. In .NET 1.1, cascading style-sheets were used to perform this function. However, in .NET 3.0, these stylesheets were replaced by masterpages.

Introduction

The syntax used to denote a masterpage is

masterpage

< %@ Master Language=”C#” CodeFile=”MasterPage.master.cs” Inherits=”MasterPage” %>

This specifies that the page is a masterpage and is given at the start of the page.

The ASP.NET page which inherits content from a masterpage has the following tag in it.

<%@ Page Language=”VB” MasterPageFile=”~/MasterPages/Master1.master” Title=”Content Page” %>

Creating a Master Page

1 Create a New Project

2 Add a master page to the project. This could be done by clicking ‘Add New Item’ and selecting ‘Masterpage’ from the list of items displayed.

3 Open the masterpage and enter the contents to be displayed just above the ContentPlace Holder object.

4 Right-click on the project and click ‘Add New Item’. Select ‘Web Page’ and check ‘Select Master Page’ option and click ‘Ok’. The project folder dialog box will be displayed. Select the masterpage file (.master) from the list of files displayed and click ‘Ok’. The file gets created and you will observe that a ‘content’ object gets created by default. This tag is created when a masterage is selected. If no masterpages are required in the page then this tag can be removed.

5. Run the file. You will observe that the content specified on top of the ContentPlaceHolder in the Masterpage file appears on the Web page.

Nested Masterpages

It is also possible to nest one masterpage inside another. The designer doesn’t support nesting of master pages; however, two or more masterpages could be nested at the code level.

Site Master Page

The SiteMaster page is made up exclusively of the Content control.

Section Master

Scoping Masterpages

Masterpages could be specified either at the page or folder or application level.

ASP.NET/Roles

A role is a group of users who are uniqely identified by the set of privileges they are given. A typical website has three roles:

Administrator

Power User

User

An administrator has all privileges over the particular site while power user and ordinary users don’t have.

Creation of Roles

Roles can be created in various ways. The typical process of role creation involves using the Role Manager tag in Web.config

Example

Roles can also be assigned or modified programmatically using the Role class.

Using the Website Administration Tool

Roles can be created in .NET 2.0 using the ASP.NET Website Administration tool. This tool is available in the Solution Explorer toolbar.

The tool allows the user to change

Security settings

Application Configuration

Provider Configuration

A few snapshots of the tool are availabe below. These snapshots are self-explanatory:

mmc1

mmc2

mmc3



Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.