Web Application Development Lecture 01
Agenda • • • • • •
What is .Net Why .Net Development Environment Example Program Course Contents Assessment Criteria
What is .Net • .NET is a platform for developing, deploying, and maintaining desktop, enterprise, Internet and Smart devices applications.
Why .Net • A developer can enjoy or take advantage various .Net features such as: – Multiple Languages based Development – Cool Development environment, Visual Studio .NET – Support for front-end technologies: HTML, XML, Java script, Jquery, CSS, AJAX etc.
– A huge and powerful class library with over 2000 classes (data structure, data connection, threading, code security etc.)
Development Environment • Visual Studio: For Web Development Environment – Supports
MVC
REST
Single Page API
Languages Support • Visual Studio IDE is developed with support for
Example •
Example C# program
Week
Content
Introduction to MVC Framework understanding Model, View and Controller Creating new Project and adding controller class Writing first hello world function in controller class Control Structures Conditional Statements Functions with parameters Exception handling
9 10
C# classes and coding convention Collection classes Basic LINQ queries Regular Expressions Enumeration Anonymous types for loop and for each loop in View file Basic HTML (Tags, hyper reference, anchor, images) Page layout using CSS (classes, ID, CSS rules) Setting different layouts in CSS Shared View/Master Page Bootstrap CSS with Shared View/ Master Page View Bag, view data and view data dictionary The razor view engine creating classes and database using code first approach creating classes and database using data first approach Generating controller and views Model binding (default & Explicit) HTML Helper Metadata attributes on class members Advance LINQ queries Data Annotation and Validation Membership, authorization and security
11
AJAX
12
Routing
13&14. 15&16.
ASP.Net Web API Single Page Application with AngularJS
1
2
3 4
5 6 7 8
Assessment
Quiz 01
Quiz 02, Assignment 01
Assignment 02 Assignment 02 Assignment 02 Assignment 03 Assignment 03 Quiz 03 Assignment 04 Assignment 04 Quiz 04, Assignment 05 Quiz 05 Project Project Project
Assessment Exam Type
Percentage (%)
Quiz
10
Assignments
20
Project-Presentation
10
Mid Term
20
Final Term
40
Introduction to C# • C# is an stylish and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework.
• C# syntax is highly expressive, yet it is also simple and easy to learn. It is easy to understand for a developer who is already familiar with C, C++ or java.
Making First Project in C# • Choose File from menubar
Making First Project in C# • Select New-> Project
Making First Project in C# • Choose Console App (.Net Framework) • Assign name to your project and press ok
Introduction to C# Like java Packages
Preprocessing directives
All code is encapsulated in class
Variables’ Type in C# •
Variables’ Type – Value Type Type
Represents
bool
Boolean value
byte
8-bit unsigned integer
char
16-bit Unicode character
decimal
128-bit precise decimal values with 28-29 significant digits
double
64-bit double-precision floating point type
float
32-bit single-precision floating point type
int
32-bit signed integer type
long
64-bit signed integer type
sbyte
8-bit signed integer type
short
16-bit signed integer type
uint
32-bit unsigned integer type
ulong
64-bit unsigned integer type
ushort
16-bit unsigned integer type
Variables’ Type in C# – Reference Type • Do not actually store the data • They contain reference to variable • E.g. Person p1=new Person(“Lional”,39,”Itly”); Person p2=p1; p2.name=“asad”; console.write(p1.name); //it will print “asad”
p1
Person
refers refers
p2
name:Lional age:39 country:Itly
Variables’ Type in C# • Other distinct object types – Three kind of built-in types – object, dynamic, String
– object • • • •
base class for all data types in C# can be assigned values of any other types But we need type casting to get value back Example object obj; obj = 100;
• Assigning any other type to object is called boxing. • Assigning value back from object type is called unboxing. – int value=(int)obj;
Variables’ Type in C# • Other distinct object types (cont.…) – Dynamic • You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time. dynamic var = "hello"; var = 5; Console.WriteLine(var);
//prints 5
– String • allows you to assign any string values to a variable • string type is an alias for the System.String class • String str=“Welcome to .Net”;
Defining Class in C# • Naming Convention – PascalCase for class name and methods • string FirstName • double TotalAmount • double GetTotalCount()
– camelCase for arguments and local variables • double GetTotalCount(string employeeID); • void Swap(double firstVariable, double secondVariable) { int thirdVariable=firstVariable; firstVariable=secondVariable; secondVariable=thirdVariable; }
//camel case is used for class members: ‘first variable’, and ‘secondVariable’
//camel case is used for local variable ‘third variable’
Switch statement in C# namespace CSharpPractice { class Demo { static void Main(string[] args) { /* local variable definition */ char grade = 'B'; switch (grade) { case 'A': Console.WriteLine("Excellent!"); break; case 'B': case 'C': Console.WriteLine("Well done"); break; case 'D': Console.WriteLine("Promoted"); break;
case 'F': Console.WriteLine("Failed"); break; default: Console.WriteLine("Invalid grade"); break; } Console.WriteLine("Your grade is “+ grade); } //end switch } //end main } //end class
Function Call Statement • You can call a local function or class member function from main body of C# program. • For example class Demo { public static void Main(string[] args) { Demo d = new Demo(); d.mathematics() } public void mathematics(){ } }//end main } // end class
Function Call Statement • Write a C# program that takes two variables (of type double) and operator (char) from user as input. – The program calls mathematics(var1, operator, var2) and perform arithmetic operation based on provided operator. – Program shall use switch statement on operator and print accurate output on console screen.
Function Call Statement class Demo { public static void Main(string[] args) { Demo d = new Demo(); double firstVariable; firstVariable= Console.ReadLine(); char operators = '/'; double secondVariable; secondVariable
= Console.ReadLine();
d.mathematics(firstVariable, operators, secondVariable); Console.ReadKey();
} //end main }
Function Call Statement static void mathematics (double firstVariable, char operators, double secondVariable) switch (operators) { case '*': Console.WriteLine(firstVariable * secondVariable); break; case '+': Console.WriteLine(firstVariable + secondVariable); break; case '-': Console.WriteLine(firstVariable - secondVariable); break; case '/’: Console.WriteLine(firstVariable / secondVariable); break; default: break; } //end switch }// end function
{
Exception Handling class ExceptionDemo { static void Main(string[] args) { try { //your code comes here } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { Console.WriteLine(“This statement executes after try or catch block"); } Console.ReadKey(); } //end main } // end class
Exception Handling • Write a C# program that takes two variables (of type double) from user as input. – The program divides first variable by second variable and prints result – If second variable is zero (0) then it shall throw an exception printing that “divide by zero exception” + exception message.
Exception Handling class ExceptionDemo { static void Main(string[] args) { try { double firstVariable; firstVariable= Console.ReadLine(); char operators = '/'; double secondVariable; secondVariable = Console.ReadLine(); Console.WriteLine(firstVariable / secondVariable); } catch (Exception e) { Console.WriteLine(“divide by zero exception”+e.ToString()); } finally { Console.WriteLine("terminating calculator program"); } Console.ReadKey(); } //end main } // end class
Exception handling • Repeat your previous program and use throw statement when operator is ‘/’ and second variable is zero (0)
Function Call Statement class ExceptionDemo { static void Main(string[] args) { try { double firstVariable; firstVariable= Console.ReadLine(); char operators = '/'; double secondVariable; secondVariable = Console.ReadLine(); mathematics(firstVariable, operator, secondVariable); } catch (Exception e) { Console.WriteLine(“divide by zero exception”+e.ToString()); } finally { Console.WriteLine("terminating calculator program"); } Console.ReadKey(); } //end main } // end class
Function Call Statement static void mathematics (double firstVariable, char operators, double secondVariable) switch (operators) { case '*': Console.WriteLine(firstVariable * secondVariable); break; case '+': Console.WriteLine(firstVariable + secondVariable); break; case '-': Console.WriteLine(firstVariable - secondVariable); break; case '/': if (secondVariable == 0) throw new Exception(); else Console.WriteLine(firstVariable / secondVariable); break; } //end switch }// end function
{
Break and Continue… • Break
• Continue
for( int i=0;i<10;i++) { if(i>2&&i<8){ break; } else{ Console.WriteLine(i); } } //Breaks for loop when executed
for( int i=0;i<10;i++){ if(i>2&&i<8){ continue; } else{ Console.WriteLine(i); } } //Passes control back to for loop
Prints 0 1 2
Prints 0 1 2 8 9
foreach Loop static void Main() { string[] vehicle= { “Car", “Jeep", “cycle" }; foreach (string value in vehicle) { Console.WriteLine(value); } }
Defining Class in C# namespace CSharpPractice{ class Person { private string FirstName; //Pascal Case private string LastName; private string profession; double Age; ///////////SETTERS////////////// public void SetFirstName(string firstName) { FirstName = firstName; } //…………Write other setters……………. ///////////GETTERS////////////// string GetFirstName() //Pascal Case { return FirstName; } } }
namespace CSharpPractice { class Demo { static void Main(string[] args) { Person person = new Person(); //camel case for local variable ‘person’ person.SetFirstName("aziz"); Console.WriteLine(person.GetFirstName ()); Console.ReadKey(); } } }
Inheritance in C# class Employee : Person { private string EmployeeID; ////////////SETTERS////////////// public void setEmployeeID(string employeeID) { this.EmployeeID = employeeID; } ////////////GETTERS////////////// public string GetEmployeeID() { return this.EmployeeID; } } //end class
namespace CSharpPractice { class Demo { static void Main(string[] args) { Employee employee = new Employee(); employee.SetFirstName("aziz"); employee.setEmployeeID("43543"); Console.WriteLine (employee.GetFirstName()); Console.WriteLine (employee.GetEmployeeID () ); Console.ReadKey(); } } }
Using properties and Camel case in classes class Person { private string _firstName; public string _lastName; private string _profession; double _age; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } }
public string Profession { get { return _profession; } set { _profession = value; } } } //end class public static void main(String[] args) { Person person=new Person(); person.FirstName=“usama”; person.LasrName=“azhar”; : : }