Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Part 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

ASP.

NET

ASP.NET

• What is ASP.NET and how is different from ASP

– ASP: server side technology for creating dynamic web pages


using scripting languages eg vb script.
– ASP.NET: server side technology for creating dynamic web pages
using Fully Fledged programming languages supported by .NET
What is .NET?
• A Microsoft strategy and new technology for delivering software
services to the desktop and to the web
• Components include:
– MS Intermediate Language; all code is complied into a more abstract,
trimmed version before execution. All .NET languages are compiled to
MSIL – the common language of .NET
– The CLR- common language runtime; responsible for executing MSIL
code; interfaces to Windows and IIS
– A rich set of libraries (Framework Class Libraries) available to all .NET
languages
– The .NET languages such as C#, VB.NET etc that conform to CLR
– ASP.NET is how the Framework is exposed to the web, using IIS to
manage simple pages of code so that they can be complied into full .NET
programs. These generate HTML for the browser.
• Built on open protocols (XML, SOAP)
• Future for development of MS & non-MS based systems.
• Also heading towards the “Internet Operating System”

Framework Overview

VB C++ C# JScript …

Common Language Specification


Visual Studio.NET

Web Forms
Win Forms
(ASP.NET)

Data and XML

Base Class Library

Common Language Runtime


Namespace
• The base class libraries are organized into logical
groupings of code called namespaces
• A namespace is a hierarchical way to identify
resources in .NET
• The System object is at the top of the namespace
hierarchy, and all objects inherit from it
– ASP.NET: System.Web namespace
– WebForms: System.Web.UI namespace
– HTML Server Controls:
System.Web.UI.Control.HTMLControl
– ASP.NET Server Controls:
System.Web.UI.Control.WebControl

Some ASP.NET namespaces


System Defines fundamental data types eg
system.string
System.Collections Definitions and classes for creating
various collections
System.IO File reading & writing operations

System.Web Support browser/server


communication
System.Web.UI Creates the Page object whenever
an .aspx page is requested
System.Web.UI.web Classes and definitions to create
controls server controls
ASP.NET – class browser
• ASP.NET provides a means of exposing the .NET
Framework and its functionality to the WWW
• Contains a number of pre-built types that take
input from .NET types and represents them in a
form for the web (such as HTML)
• Class browser (over 9000 classes; lists the
namespaces):

ASP.NET
• Visual Studio .NET is a developer application used to
create ASP.NET Web applications
• There are two main types of Web resources created
with ASP.NET applications
– WebForms are ASP.NET pages within an ASP.NET
application.
– Web Services are ASP.NET Web pages that contain
publicly exposed code so that other applications can
interact with them.
– Web Services are identified with the file extension .asmx
WebForms
• The ASP.NET WebForm is separated into two
logical areas:
– The HTML template
– A collection of code behind the WebForm
• The HTML template
– Contains the design layout, content, and the controls
– Creates the user interface, or presentation layer
– Instructs the browser how to format the Web page
– Is created using a combination of HTML controls,
HTML Server controls, Mobile Controls, and ASP.NET
controls

Server Controls
• HTML Server controls are similar to the HTML
controls, except they are processed by the server
• Add runat = "server" to the HTML control to
transform it into an HTML Server control
• HTML control: <input type="text">
• HTML Server control:
<input type="text" runat="server"/>
<input type=”radio” runat=”server” value=”Yes”/> Yes
• Server-side programs can interact with the control
before it is rendered as a plain HTML control and
sent to the browser
ASP.NET Controls
• ASP.NET form controls will create the HTML code
• ASP.NET Server controls are organized as:
– ASP.NET Form Controls
– Data Validation Controls
– User Controls
– Mobile Controls
• ASP.NET controls are usually identified with the
prefix asp: followed by the name of the control
• ASP.NET button:
<asp:Button id="ShowBtn" runat="server"
Text="Show the message." />

HTML Server Vs
ASP.NET Server, Controls
• ASP.NET form controls can interact with client-
side events such as when the user clicks on a
button
– When the event occurs, ASP.NET can trigger a script to
run on the server
• ASP.NET form controls also have different
properties than their HTML server control
counterparts
– HTML Server label control
• Message1.InnerHTML = "Product 1"
– ASP server label control
• Message2.Text = "Product 2"
User Controls
• User controls are external files that can be
included within another WebForm
• User controls allow you to reuse code across
multiple files
• For example, you can create a user control that
displays the a navigation bar
• You can use this control on the home page; they
are often used for creating self-contained code,
headers, menus, and footers
• User controls replace the functionality of ASP
server-side include pages

Other ASP.NET Server Controls


• Data validation controls
– A series of controls that validate form data without extensive
JavaScript programming

• Mobile controls
– A series of controls that provide form functionality within
wireless and mobile devices

• Literal controls
– Page content that is not assigned to a specific HTML control
such as a combination of HTML tags and text to the browser
Server Controls within
Visual Studio .NET
• In Visual Studio
.NET most of the
ASP.NET Server
controls are located
on the Web Forms
tab in the toolbox

Server controls with Visual Studio.NET

The Code Behind


• Server programs are written in a separate file
known as the code behind the page
• By separating the programming logic and
presentation layer, the application becomes
easier to maintain
• Only Server controls can interact with the code
behind the page
– Written in any ASP.NET compatible language such
as Visual Basic .NET, C#, Perl, or Java
– Filename is the same as the WebForm filename
– Add a file extension that identifies the language
• Visual Basic .NET use .vb (mypage.aspx.vb)
• C# use .cs (mypage.aspx.cs)
Code Behind file
• The location of the code behind the page is determined
via a property that is set on the first line in the page
using the @Page directive
<%@ Page Language=“cs" Codebehind="WebForm1.cs"
Inherits=“MyFirstApp.WebForm1"%>
• The @Page directive allows you to set the default
properties for the entire page such as the default
language
• The CodeBehind property identifies the path and
filename of the code behind file
• The Inherits property indicates that the code behind the
page inherits the page class
• This page class contains the compiled code for this
page

Compiling the Page Class


• The compiled code behind the page is the class
definition for the page
– A class is a named logical grouping of code
– The class definition contains the functions, methods, and
properties that belong to that class
• In Visual Studio .NET the process of compiling a
class is called building
– When you build the application, you compile the code
into an executable file
– Visual Studio .NET compiles the code behind the page
into an executable file and places the file in the bin
directory
Page Class Events
• The Page Class consists of a variety of methods,
functions, and properties that can be accessed
within the code behind the page
• The first time a page is requested by a client, a
series of page events occurs
• The first page event is the Page_Init event
which initializes the page control hierarchy
• The Page_Load event loads any server controls
into memory and occurs every time the page is
executed

Page class events


• Page_init
• Page_load
• Server_Controls
• Page_prerender
• Page_Unload
ASP.NET Vs PHP
Feature PHP ASP.NET
HTML Yes Yes
CSS Yes Yes
‘php Templates’ Yes UserControls
ServerControls No Yes
(buttons,grids etc)

Javascript Yes Yes + Validation controls


Database Conn Yes Yes
Cookies & Sessions Yes Yes
VIEWSTATE No Yes
POSTBACK No Yes

Web Services
• Web Services also provide a means to
expose .NET functionality on the web but
Web Services expose functionality via XML
and SOAP (cf: function calls over the web)
INTRODUCTION TO WEB
SERVICES

Lecture 6
What is a Web Service ?

Web service is a means by which computers talk to each other over


the web using HTTP and other universally supported protocols.

A Web service is an application that:

• Runs on a Web server


• Exposes Web methods to interested callers
• Listens for HTTP requests representing commands to invoke Web
methods
• Executes Web methods and returns the results

Web Services is based on:

• HTTP (Hypertext Transport Protocol)


• SOAP (Simple Object Access Protocol)
• UDDI (Universal Description, Discovery and Integration)
• WS-POLICY (Web Services Policy)

Most Web services expect their Web methods to be invoked


using HTTP requests containing SOAP messages. SOAP is an
XML-based vocabulary for performing remote procedure
calls using HTTP and other protocols.
Sample web service
Calc.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>


using System;
using System.Web.Services;

[WebService (Name="Calculator Web Service",


Description = "Perform simple math over the Web")]
class CalcService
{
[WebMethod (Description = "Computes the sum of two integers")]
public int Add (int a, int b) { return a+b;}
[WebMethod (Description = "Computes the difference between two integers")]
public int Subtract (int a, int b) { return a-b;}
}

The example demonstrates several important principles of Web service


programming using the .NET Framework:

• Web services are implemented in ASMX files. ASMX is a special file name
extension registered to ASP.NET (specifically, to an ASP.NET HTTP handler) in
Machine.config.

• ASMX files begin with @ WebService directives. At a minimum, the directive


must contain a Class attribute identifying the class that makes up the Web service.

• Web service classes can be attributed with optional WebService attributes. The
one in the previous example assigns the Web service a name and a description
that show up in the HTML page generated when a user calls up Calc.asmx in his
or her browser.

• Web methods are declared by tagging public methods in the Web service class
with WebMethod attributes. You can build helper methods into a Web service—
methods that are used internally by Web methods but that are not exposed as Web
methods themselves—by omitting the Webmethod attribute.
Testing a Web Service :
How do you test an ASMX Web service?
Simple: just call it up in your browser.

ASP.NET responds to the HTTP request for Calc.asmx by generating an HTML page
that describes the Web service.
• The name and description in the ASMX file’s WebService attribute appear at the top of the
page.
• Underneath is a list of Web methods that the service exposes, complete with the
descriptions spelled out in the WebMethod attributes.

Click “Add” near the top of the page, and ASP.NET displays a page that you can use to
test the Add method .
• ASP.NET knows the method name and signature because it reads them from the metadata
in the DLL it compiled from Calc.asmx. It even generates an HTML form that you can use
to call the Add method with your choice of inputs.
• The XML returned by the Web method appears in a separate browser window

The forms that ASP.NET generates on the fly from ASMX files enable you to
test the Web services that you write without writing special clients to test them
with.
Suppose you write a Web service that publishes Web methods named Add and Subtract that
callers can use to add and subtract simple integers. If the service’s URL is
www.wintellect.com/calc.asmx, here’s how a client would invoke the Add method by transmitting
a SOAP envelope in an HTTP request. This example adds 2 and 2:

POST /calc.asmx HTTP/1.1


Host: www.wintellect.com
Content-Type: text/xml; charset=utf-8
Content-Length: 338
SOAPAction: http://tempuri.org/Add

<?xml version="1.0" encoding="utf-8"?>


<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema instance"
xmlns: xsd=http://www.w3.org/2001/XMLSchema
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Add xmlns="http://tempuri.org/">
<a>2</a>
<b>2</b>
</Add>
</soap:Body>
</soap:Envelope>

And here’s how the Web service would respond:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 353

<?xml version="1.0" encoding="utf-8"?>


<soap:Envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd=http://www.w3.org/2001/XMLSchema
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddResponse xmlns="http://tempuri.org/">
<AddResult>4</AddResult>
</AddResponse>
</soap:Body>
</soap:Envelope>
The Web service’s job is to parse the SOAP envelope containing the inputs, add 2
and 2, formulate a SOAP envelope containing the sum of 2 and 2, and return it to the client
in the body of the HTTP response. This, at the most elemental level, is what Web services
are all about.
Web services written with the .NET Framework also allow their Web methods to be
invoked using ordinary HTTP GET and POST commands. The following GET command
adds 2 and 2 by invoking the Web service’s Add method:

GET /calc.asmx/Add?a=2&b=2 HTTP/1.1


Host: www.wintellect.com

The Web service responds as follows:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 80

<?xml version="1.0" encoding="utf-8"?>


<int xmlns="http://tempuri.org/">4</int>

Here’s a POST command that adds 2 and 2:

POST /calc.asmx/Add HTTP/1.1


Host: www.wintellect.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 7

a=2&b=2

And here’s the Web service’s response:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 80

<?xml version="1.0" encoding="utf-8"?>


<int xmlns="http://tempuri.org/">4</int>

The hard part of writing a Web service is parsing HTTP requests and generating HTTP
responses. The .NET Framework insulates developers from the low-level details of HTTP, SOAP,
and XML and provides a high-level framework for writing Web services and Web service clients
alike.
Web Services Description Language - WSDL

If other developers are to consume (that is, write clients for) a Web service that
you author, they need to know :

• What Web methods your service publishes


• What protocols it supports
• The signatures of its methods
• The Web service’s location (URL)

All this information and more can be expressed in a language called the Web
Services Description Language, or WSDL for short.
WSDL is an XML vocabulary http://www.w3.org/TR/wsdl.

Web Service Discovery—DISCO and UDDI

Once a client has a WSDL contract describing a Web service, it has all the
information it needs to make calls to that Web service.
But when you publish a Web service by making it available on a Web server,
how do clients find out where to get a WSDL contract? For that matter, how do
clients know that your Web service exists in the first place?

The answer comes in two parts:


DISCO and Universal Description, Discovery, and Integration (UDDI)

• DISCO is a file-based mechanism for local Web service discovery—that is, for
getting a list of available Web services from DISCO files deployed on Web
servers.
• UDDI is a global Web service directory that is itself implemented as a Web
service.

You might also like