What is ASP.NET Development Server?

In VS 2005, file system websites are hosted on the new local web server called” ASP.NET Development Server” by default instead of IIS. This server dynamically chooses the port and hosts the website for local requests only. As a part of Framework 2.0 WebDev.WebServer.exe program runs as development server and host the website whenever website is run in the VS.
Now IIS does not installed along with WinXP or Win2003 like previous versions of Windows, you needs to install it separately therefore if you are developing websites using VS 2005, you don’t have to worry about the web server to host your website as asp.net development server will take care of it.
This program is found at (%WINDIR%\Microsoft.NET\Framework\v2.0.XXXXX) location.
You can also host website manually using this program.

Syntax: WebDev.WebServer.exe [/port:] /path: [/vpath:]

Like: webdev.webserver.exe /port:4955 /path:"c:\RnD\WebSite1" /vpath:”/WebSite1”

Here you can assign any port no you want.


Difference between ASP.NET Development Server and IIS Server
Below are some notable differences:

•Development server only accepts authenticated requests on the local computer as it runs under the security context of currently authenticated user account. It means it doesn’t run under less privileged ASPNET account. Where as IIS by default runs under ASPNET account.
•Unlike IIS, Development server can only serve local requests. It will not serve pages to another computer.
•Unlike IIS, Development server can only serve resources like files that are within the application scope.
•IIS provides other services like SMTP, FTP etc. that development server does not provides so if your application has e-mail sending functionality or file download functionality, you would have to use IIS server.

Reference: http://msdn.microsoft.com/en-us/library/58wxa9w5(VS.80).aspx

Website Logging and AWStats Tool

Analysis of a website is very important if you want to optimize the performance of the website. The informations like from where most of the web traffic is coming or who is eating up your sites bandwidth are very handy for improving your websites performance. These informations are logged by the web servers that can be processed by some tools like AWStats, Analog, Webalizer etc... into some graphical charts for easy analysis.
Web logging is by default enabled in IIS5.1 web server and the data is stored in the
C:\WINDOWS\system32\LogFiles\W3SVC1 location in the form of text files with the name format like exyymmdd.log, here yy- year, mm-month and dd-day.
By default some selected properties are logged so you can customize the logging information to your requirement.For that -
Open the website properties window and click on the properties button in the enable logging section. Extented logging properties window will open. Here check & uncheck the properties you want to log like you you want to measure bandwidth comsumption of your site, check Bytes Sent option or if you want to know who is sending most of the traffic , check Referer option.

AWStats Tool

This is the tool to analyse this web logging information and show it with graphics chart.This tool is free for use.
Check out below link for more information on this tool.

http://awstats.sourceforge.net/

Uploading Multiple Files in ASP.NET

In this post I have given code for uploading multiple files using client callback.
Here I will not explain client callback as I have published a dedicated post on it.

Now, this multiple files upload functionality has 4 parts:

1. Selecting multiple files functionality and Upload button functionality that will request the client callback in FileUpload.aspx page .

2. Implementing ICallbackHandler interface by defining GetCallbackResult and RaiseCallbackEvent methods in the FileUpload.aspx.cs file. RaiseCallbackEvent methods will call UploadFile method of WebClient class, which posts the file to the given url(in our case it is fileserver.aspx).

3. Saving functionality for uploaded file on the server in Fileserver.aspx file.

Here is the sample code.

I have not explained it in details as it is self explainatory yet I have commented code as and when required.

----------------------------------------------------------------------
File - FileUpload.aspx

< script language="javascript" type="text/javascript">
var selectedFiles = '';

function CallbackResultHandler(result, context) //this function will receive the callback result
{
alert(result);
}
// below function will receive callback resulted appended with error msg when server throws an error
function CallbackErrorHandler(result, context)
{
alert("Error: " + result);
}
function uploadFile() // this function will trigger the callback
{
var fileList = document.getElementById("divUploadFile").getElementsByTagName("INPUT");
for(i=0; i < fileList.length;i++)
{
selectedFiles += fileList[i].value + "|";
}
< %=_callbackRef % > // here callback method - WebForm_doCallback will be rendered.
}

function attachFile()
{
var varTag1 = document.createElement("INPUT");
varTag1.type = "file";
var varTag2 = document.createElement("< BR >");
document.getElementById("divUploadFile").appendChild(varTag1);
document.getElementById("divUploadFile").appendChild(varTag2);
}

< /script >

< html xmlns="http://www.w3.org/1999/xhtml">
< head runat="server">
< title >File Upload Page< /title >
< /head >
< body >
< form id="form1" runat="server" >
< div >
< table >
< tr >
< td colspan="2">
< ol >
< li style="list-style: none">
< div id="divUploadFile" / >
< /li >
< /ol >
< /td >
< /tr >
< tr >
< td >
< a href="#" onclick="attachFile()">Attach a file< /a >
< /td >
< td >
< input type="button" value="Upload" onclick="uploadFile()" / >
< /td >
< /tr >
< /table >
< /div >
< /form >
< /body >
< /html >

------------------------------------------------------------------
File - FileUpload.aspx.cs

public partial class FileUpload : System.Web.UI.Page, ICallbackEventHandler
{
string _result = string.Empty;
protected string _callbackRef = string.Empty;

protected void Page_Load(object sender, EventArgs e)
{
//following code returns the javascript call to WebForm_doCallback function
_callbackRef = ClientScript.GetCallbackEventReference(this, "selectedFiles", "CallbackResultHandler","null", "CallbackErrorHandler", true);
}

#region ICallbackEventHandler Members

public string GetCallbackResult() //returnd the callback result
{
return _result;
}

public void RaiseCallbackEvent(string eventArgument) // invokes callback event
{
string[] Files = (eventArgument.TrimEnd('|')).Split('|');
System.Net.WebClient Client = new System.Net.WebClient();
try
{
string Url = "http://localhost:" + Request.Url.Port.ToString() +
"/FileUploadWebsite/FileServer.aspx";
foreach (string file in Files)
{
Client.UploadFile(Url, "POST", file);
}
_result = "All files are uploaded successfully.";
}
catch (System.Net.WebException ex)
{
_result = "One or more file(s) could not be uploaded.";
throw ex;
}
}

#endregion
}

-------------------------------------------------------------
File - Fileserver.aspx

< script runat="server" >
void Page_Load(object sender, EventArgs e)
{
string Uploadpath = @"C:\TestWeb\UploadedFiles\"; // ASPNET account should have read/write permission on the folder

string[] Keys = Request.Files.AllKeys;
foreach (String key in Keys)
{
HttpPostedFile File = Request.Files[key];
File.SaveAs(Uploadpath + File.FileName);
}
}
< /script >

< html xmlns="http://www.w3.org/1999/xhtml" >
< head runat="server" >
< title >File Server Page< /title >
< /head >
< body >
< form id="form1" runat="server">
< div >

< /div >
< /form >
< /body >
< /html >

Script Callback in ASP.NET

In very simple term script callback is a machanims of calling server side code from client side(using javascript) without rendering the web page. In other term posting web page asynchronously. In the heart of it, is XmlHttpRequest DOM object. During a normal page postback(synchronous), the Web page and controls are recreated and a new version of the entire Web page is rendered on the client. Where as in the client callback browser create new connection using XmlHttpRequest to send only the callback data to the remote server not the content of entire page making the page lifecycle short(page rendering event does not occur).

The ASP.NET 2.0 implementation of client callbacks uses the Client Callback Manager. After initial page load, subsequent requests to server-side code are made by a client-side component, without refreshing the entire Web page. The page runs a modified version of its normal life cycle where the page is initiated and loaded, but the page contents are not rendered. Instead, a special method in the server-side code is invoked, which processes the callback request content, then returns a value to the browser that can be read by JavaScript function. The JavaScript function uses technology inherent to the browser (e.g. DOM, DHTML) to update Web page content dynamically. The Web page continues to stay live while the request is being processed.

The Page.IsPostback property will return true for both request types(normal postback & client callback). The Page. But IsCallback property will return true only when the request is a client callback.

Page LifeCycle in case of Client Callback:

Init-> LoadState-> ProcessPostbackData-> Load-> ChangeEvent-> RaiseCallbackEvent-> GetCallbackResult -> Unload

Note-: PostBack , PreRender, Render and SaveState events are skiped.

Implementing Client Callback:

Following are the steps:-

1. Write a javascript function that will request the callback by calling WebForm_DoCallback function.

2. Write another javascript function(Callbackhandler) that will receive the callback and do the needful client side processing.

3. Call the GetCallbackEventReference method in the Page_Load event.

This method creates a JavaScript string containing code for calling WebForm_DoCallback function in the client Web page to start the callback.

like :WebForm_DoCallback('__Page', message, processMyResult, context, postMyError, true)

Syntax: GetCallbackEventReference(control, argument, clientCallback, context, clientErrorCallback, useAsync);

control - The control or the page which implements ICallbackEventHandler.
argument - data that needs to be processed by the asynchronous call.
clientCallback - javascript function(Callbackhandler) that will receive the result.
context - Name of a client-side JavaScript variable. Usually used to determine the content of the message (argument) in the callback request. The value of the variable will be stored on the client as the context parameter associated with a callback.
clientErrorCallback - javascript function that will receive the result when server throws an error.
useAsync - boolean value to determine if the call is async or not.

4. Implement the System.Web.UI.ICallbackEventHandler interface by defining its 2 methods- RaiseCallbackEvent and GetCallbackResult.

Here is the code sample:

File - syncCallback.aspx

< script language="javascript">
function CallbackHandler(result, context) //Step 2
{
alert(result);
}
< /script >

< html xmlns="http://www.w3.org/1999/xhtml" >
< head runat="server" >
< title >Client Callback Demo< /title >
< /head >
< body >
< form id="form1" runat="server" >
< div >
Name :< input id="txtName" type="text" />
< input id="btnClient" type="button" value="Asyn Callback" runat="server" / >
< /div >
< /form >
< /body >
< /html >


File - syncCallback.aspx.cs

public partial class syncCallback : System.Web.UI.Page, ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{
string CallbackRef = ClientScript.GetCallbackEventReference(this,"document.all['txtName'].value","CallbackHandler", "null",true);
//Step 3 will generate following string

// WebForm_DoCallback('__Page',document.all['txtName'].value,CallbackHandler,null,null,true)


btnClient.Attributes["onclick"] = String.Format("javascript:{0}",CallbackRef); //Step 1
}

#region ICallbackEventHandler Members
string _str;

public string GetCallbackResult() //Step 4
{
return _str;
}

public void RaiseCallbackEvent(string eventArgument) //Step 4
{
_str = "Hello " + eventArgument;
}

#endregion
}