Wednesday, December 10, 2008

Deleting subdirectory restarts AppDomain

When you deletes a subfolder either manually or programatically from the application root folder of IIS, the ASP.NET runtime automatically restarts AppDomain. To fix this, we will need to turn off the folder monitoring in the Application_Start of the Global.asax.vb, this does not apply on bin folder.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)


StopMonitorFolderChange()


End Sub


Private Sub StopMonitorFolderChange()


Dim p As System.Reflection.PropertyInfo = GetType(System.Web.HttpRuntime).GetProperty("FileChangesMonitor", _


System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Public] Or System.Reflection.BindingFlags.[Static])


Dim o As Object = p.GetValue(Nothing, Nothing)


Dim f As System.Reflection.FieldInfo = o.[GetType]().GetField("_dirMonSubdirs", _


System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.IgnoreCase)


Dim monitor As Object = f.GetValue(o)


Dim m As System.Reflection.MethodInfo = monitor.[GetType]().GetMethod("StopMonitoring", _


System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)


m.Invoke(monitor, New Object() {})


End Sub

Reference:

issue C#: Delete directory kills session

Tuesday, December 9, 2008

HttpHandler

Basics

  • An HTTP Handler is a .NET class that executes whenever you make a request for a file at a certain path.
  • The Page class is an HTTP Handler because it implements the IHttpHandler interface.
  • The IsReusable property indicates whether ASP.NET can keep the handler in memory to service multiple requests, or if it must create a new instance of the handler for every request.
  • Unless you maintain some sort of state in the handler, which is uncommon, IsReusable should always return true.
  • Two ways to create an HTTP Handler:
    • Generic Handler (.ashx): you cannot execute a Generic Handler whenever someone requests a file with the extension .gif. If you need more control over when an HTTP Handler executes, create a class implements IHttpHandler.
    • Custom class implement the IHttpHandler interface.

Class implements IHttpHandler

  • Register HTTP Handlers & Configure HTTPHandler Extension in IIS.
    • IIS 6.0
      • In the httpHandlers section of the web.config, add an entry for the file-name extension. Specify the following four attributes:
        • path: specify the path associated with the handler.
        • verb: specify the HTTP verbs, such as GET or POST, associated with the handler. You can specify multiple verbs in a comma-separated list. You can represent any verb with the * wildcard.
        • type: specify the name of the class that implements the handler.
        • validate: specify whether the handler is loaded during application startup. When true, the handler is loaded at startup. When false, the handler is not loaded until a request associated with the handler is made.
      • add the file extension to map to Aspnet_isapi.dll
      • Verify that file exists check box
        • Check. The file-name extension represents a physical file in the application. If the requested file does not exist on disk, IIS displays an error.
        • Uncheck. The file-name extension does not represent a physical file. Instead, the extension is handled dynamically by a class that is mapped to the extension in ASP.NET.
    • IIS 7.0 (Classic or Integrated mode)
      • Using IIS Manager in IIS 7.0 to add a custom handler extension is equivalent to registering the handler extension in the Web.config file of an ASP.NET application. The registration adds a handler element in the handlers section of the system.webServer group.
      • In the httpHandlers section of the web.config, add an entry for the file-name extension.

References:

ASP.NET 2.0 Unleashed

How to: Configure an HTTP Handler Extension in IIS

How to: Register HTTP Handlers

Wednesday, November 12, 2008

Loop through files in a directory

The following example shows you how to programme path in ASP.NET:


Imports System.IO


Partial Public Class WebForm1


Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


'Note: TestPath is configured the root folder in IIS


Print("-------Path------------")


Print(Server.MapPath("~")) 'Root folder: C:\ALL\Dev\TestPath\


Print(Server.MapPath("~/")) 'Root folder: C:\ALL\Dev\TestPath\


Print(Server.MapPath(".")) 'Current folder: C:\ALL\Dev\TestPath\Folder1\Folder2


Print(Server.MapPath("..")) 'Upper-level folder: C:\ALL\Dev\TestPath\Folder1


Print(Server.MapPath("../..")) 'Upper-upper-level folder: C:\ALL\Dev\TestPath


Print("--------URL-----------")


Print(Request.RawUrl()) '/TestPath/Folder1/Folder2/WebForm1.aspx


Print(Request.PhysicalPath) 'C:\ALL\Dev\TestPath\Folder1\Folder2\WebForm1.aspx


Print(Request.PhysicalApplicationPath) 'C:\ALL\Dev\TestPath\


Print(Request.Url.AbsolutePath) '/TestPath/Folder1/Folder2/WebForm1.aspx


Print(Request.Url.LocalPath) '/TestPath/Folder1/Folder2/WebForm1.aspx


Print("--------File/Folder-----------")


Print(GetCurrentPageName) 'WebForm1.aspx


Print(GetFileExtension(Request.PhysicalPath)) '.aspx


Print(GetFileExtension1(Request.PhysicalPath)) '.aspx


Print(IsFileExist(Request.PhysicalPath)) 'True


Print(IsFolderExist(Server.MapPath(".."))) 'True


Print(IsFolderExist1(Server.MapPath(".."))) 'True


Print(FindFile(Server.MapPath("."), "form1")) 'WebForm1.aspx


End Sub


Private Sub Print(ByVal val As String)


Response.Write(val + "<br>")


End Sub


Public Function GetCurrentPageName() As String


Dim path As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath


Dim file As New System.IO.FileInfo(path)


Return file.Name


End Function


Private Function GetFileExtension(ByVal path As String) As String


Dim file As New System.IO.FileInfo(path)


Return file.Extension


End Function


Private Function GetFileExtension1(ByVal path As String) As String


Return System.IO.Path.GetExtension(path)


End Function



Public Shared Function GetFileExtension2(ByVal filename As String) As String


Dim index As Integer = filename.LastIndexOf(".")


Return filename.Substring(index + 1, filename.Length - index - 1)


End Function


Private Function IsFileExist(ByVal path As String) As Boolean


Return File.Exists(path)


End Function


Private Function IsFolderExist(ByVal folder As String) As Boolean


Return Directory.Exists(folder)


End Function


Private Function IsFolderExist1(ByVal folder As String) As Boolean


Return My.Computer.FileSystem.DirectoryExists(folder)


End Function


Private Function FindFile(ByVal path As String, ByVal contains As String) As String


Dim wildcards As String() = {"*.jpg", "*.jpeg", "*.gif", "*.aspx"}


Dim Files As System.Collections.ObjectModel.ReadOnlyCollection(Of String) _


= My.Computer.FileSystem.GetFiles(path, FileIO.SearchOption.SearchTopLevelOnly, wildcards)


For Each f In Files


If f.IndexOf(contains, StringComparison.CurrentCultureIgnoreCase) >= 0 Then


Return System.IO.Path.GetFileName(f)


End If


Next


Return "Not Found"


End Function


End Class

Sunday, November 2, 2008

Loop through properties of a .NET Class

You could use reflection to return all the public properties of a current class, such as the name and value of the property.

Sample Code:

Imports System.Web


Imports System.Reflection


Partial Class HCNLogin


Private Sub CreateHCNCookies(ByVal userID As Integer)


If Request.Browser.Cookies Then


Dim hcnUser As HCNUser = hcnUser.GetByID(userID)


If hcnUser IsNot Nothing Then


Dim t As Type = hcnUser.GetType()


For Each p As PropertyInfo In t.GetProperties()


If Not p.Name.Equals("ID") AndAlso Not p.Name.Equals("intUserID") Then


Response.Cookies(p.Name).Value = p.GetValue(hcnUser, Nothing)


Response.Cookies(p.Name).Expires = DateTime.Now.AddYears(10)


End If


Next


End If


End If


End Sub


End Class


Reference:
Loop through object properties?

Running custom HttpHandler in IIS 7

  • When I am using a custom HttpModule/HttpHandler in IIS 7 under Vista, the handler does not fire. The fix is to change the application in IIS to the "Classic .NET AppPool". You can also use the DefaulAppPool by changing the web.config, refer to the references.
  • Also, IIS7 on Vista Client doesn't currently supports ASP.NET substitution caching while running in integrated mode. If you change it to classic mode it does.


References:

Using HttpModules and HttpHandlers under IIS7

HttpModule and HttpHandler sections in IIS 7 web.config files

IIS 7 Apppool "integrated" fails with ASP.NET Substitution

LINQ Connection String - app.config vs web.config

  • When you are using Linq to SQL, if you have your dbml in a class library project and you drag and drop a database table from the Server Explorer onto the dbml designer, VS2008 will automatically create the app.config for you which contains the connectionstring.
  • If you have a web application project which references the Linq class library and you want to put the connectionstring in the web.config, here is how:

1. Remove the default constructor in the dbml designer class. Create a partial class for the datacontext class which contains the constructor to re-set the connectionstring.

2. You need to set the dbml's connection to none whenever new stuff has been dragged onto the design surface.

3. Put the connectionstring in the web.config

Sample Code:

Imports System.Configuration


'We don't want to use app.config to store connectionstring, we need to use web.config


'So we need to set the dbml's connection to none whenever new stuff has been dragged onto the design surface


Partial Public Class MyDataContext


Public Sub New()


MyBase.New(ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString, mappingSource)


End Sub


End Class

References:

How to set the connection string in your LINQ dbml file dynamically (based on web.config)

LINQ and Web Application Connection Strings

Importing Excel - Truncates at 255 characters

When you importing an excel file into database, if you are using OLEDB, the text being read from each cell will be truncated at 255 characters.

Solutions:
  • HKEY_LOCAL_MACHINE\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows was set to 1. You can change it to a larger value and it now work.
  • You might consider using ODBC instead of OLEDB.

Sample code:

Imports System.Data.OleDb


Imports System.Data.Odbc


Module ImportExcel


Sub Main()


DoJob1()


End Sub


'Private Function GetData() As OleDbDataReader


' Dim cnAdo As New OleDbConnection()


' Dim cmCommand As New OleDbCommand()


' Try


' cnAdo.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source=E:\\test.xls;"


' cnAdo.Open()


' cmCommand.Connection = cnAdo


' cmCommand.CommandText = "SELECT * FROM [Sheet1$]"


' cmCommand.CommandType = CommandType.Text


' Return cmCommand.ExecuteReader(CommandBehavior.CloseConnection)


' Catch ex As Exception


' If cnAdo.State = ConnectionState.Open Then


' cnAdo.Close()


' End If


' Throw ex


' End Try


'End Function


'Private Sub DoJob()


' Dim drExcel As OleDbDataReader = GetData()


' Try


' Dim dc As New Enware_eStoreDataContext()


' Dim count As Integer = 0


' While drExcel.Read()


' If drExcel("ProductID") IsNot DBNull.Value Then


' Dim p As eStore_Product = dc.eStore_Products.FirstOrDefault(Function(s As eStore_Product) s.ID = CInt(drExcel("ProductID")))


' If p IsNot Nothing Then


' p.Name = drExcel("Name").ToString()


' p.Description = drExcel("Description").ToString()


' p.Image = drExcel("Image").ToString()


' p.Keyword = drExcel("Keyword").ToString()


' p.GroupCode = drExcel("GroupCode").ToString()


' p.Brochure = drExcel("Brochure").ToString()


' dc.SubmitChanges()


' Else


' Exit While


' End If


' Else


' Exit While


' End If


' count = count + 1


' End While


' Console.WriteLine("finished! - " + count.ToString() + " rows processed!")


' Console.ReadLine()


' Catch ex As Exception


' Console.WriteLine(ex)


' Console.ReadLine()


' Finally


' If drExcel IsNot Nothing Then


' drExcel.Close()


' End If


' End Try


'End Sub


Private Function GetOdbcDataReader() As OdbcDataReader


Dim con As New OdbcConnection()


Dim cmd As New OdbcCommand()


Try


con.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DBQ=E:\\test.xls;"


con.Open()


cmd.Connection = con


cmd.CommandText = "SELECT * FROM [Sheet1$]"


cmd.CommandType = CommandType.Text


Return cmd.ExecuteReader(CommandBehavior.CloseConnection)


Catch ex As Exception


If con.State = ConnectionState.Open Then


con.Close()


End If


Throw ex


End Try


End Function


Private Sub DoJob1()


Dim drExcel As OdbcDataReader = GetOdbcDataReader()


Try


Dim dc As New Enware_eStoreDataContext()


Dim count As Integer = 0


While drExcel.Read()


If drExcel("ProductID") IsNot DBNull.Value Then


Dim p As eStore_Product = dc.eStore_Products.FirstOrDefault(Function(s As eStore_Product) s.ID = CInt(drExcel("ProductID")))


If p IsNot Nothing Then


p.Name = drExcel("Name").ToString()


p.Description = drExcel("Description").ToString()


p.Image = drExcel("Image").ToString()


p.Keyword = drExcel("Keyword").ToString()


p.GroupCode = drExcel("GroupCode").ToString()


p.Brochure = drExcel("Brochure").ToString()


dc.SubmitChanges()


Else


Exit While


End If


Else


Exit While


End If


count = count + 1


End While


Console.WriteLine("finished! - " + count.ToString() + " rows processed!")


Console.ReadLine()


Catch ex As Exception


Console.WriteLine(ex)


Console.ReadLine()


Finally


If drExcel IsNot Nothing Then


drExcel.Close()


End If


End Try


End Sub


End Module

Reference:

Reading and Writing Excel using OLEDB

MOSS Video Format

  • SharePoint will play all files that windows media player can play. If you have all codecs correctly installed for Media player, then you can host any file in SharePoint and then add a code that will make windows media player play the file.
  • But since the user may not have the correct codecs installed for their media player, the best format is wmv since windows media player is made to play .wmv files.
  • By Default Moss will allow us to upload File size up to 50 MB. Upload File size can be adjusted to up to 2 GB.
  • The best file size will be less than 20MB.

Reference:

Does Sharepoint play any video file?

Sunday, October 26, 2008

Detect DropDownList SelectedIndexChanged on Page Load

In ASP.NET, when you define an asp:DropDownList with AutoPostBack="true", say if you are having two ListItems of the same value, then you select the first one, and now when you select the second one, the SelectedIndexChanged event of the DropDownList will not fire since the
SelectedValue of the DropDownList has not changed. You could use Request.Form("__EVENTTARGET") in the Page_Load to solve this problem.

Here is the example:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestDropDown._Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">


<title></title>


</head>


<body>


<form id="form1" runat="server">


<div>



<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">


<asp:ListItem Text="---" Value="---"></asp:ListItem>


<asp:ListItem Text="aa" Value="aa"></asp:ListItem>


<asp:ListItem Text="bb" Value="aa"></asp:ListItem>


<asp:ListItem Text="cc" Value="cc"></asp:ListItem>


</asp:DropDownList>


</div>


</form>


</body>


</html>


Partial Public Class _Default


Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


If Me.Request.Form("__EVENTTARGET") = DropDownList1.UniqueID Then


Response.Write(DropDownList1.SelectedValue)


End If


End Sub


Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged


Response.Write(DateTime.Now.ToString())


End Sub


End Class

IE8 running in IE7 mode

You could add the following as the first meta tag on the page-level to force the page to render in IE7 mode when you are using IE8:

<meta http-equiv="X-UA-Compatible" content="IE=7" />


Saturday, October 18, 2008

ReadOnly TextBox in ASP.NET

If you set the value of a readonly textbox via javascript, you cannot get the text property of the textbox from code behind. The solution is to use the Request object.

Here is how:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestTime._Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">


<title>Untitled Page</title>


<script language="javascript">


Function SetValue()


{


document.getElementById("txtTest").value = "Test";


}


</script>


</head>


<body>


<form id="form1" runat="server">


<div>


<asp:TextBox ID="txtTest" runat="server" ReadOnly="true"></asp:TextBox>


<asp:Button ID="Button1" runat="server" Text="PostBack" OnClientClick="SetValue();"/>


<asp:Label ID="lblTest" runat="server"></asp:Label>


</div>


</form>


</body>


</html>


Partial Public Class _Default


Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


End Sub


Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


lblTest.Text = txtTest.Text 'txtTest.Text = ""


lblTest.Text = Request(txtTest.ClientID) 'Request(txtTest.ClientID)= "Test"


End Sub


End Class


Optionally, you can use the following line to set the readonly attribute:

txtMyTextBox.Attributes.Add("readonly", "readonly")

Reference:

ReadOnly Textbox ViewState in .NET 2.0

Friday, October 17, 2008

SQL 2008 Select All Rows

If you want to select all rows from a table using SQL Server 2008 Management Studio, here is how:
1. Go to Tools > Options > SQL Server Object Explorer
2. Change both the 'Value for Edit Top n Rows command' and 'Value for Select Top n Rows command' to zero.

3. You can now view or edit all rows of a table.

Reference:
SQL 2008 - Change ‘Edit Top 200 Rows"

Saturday, September 27, 2008

Cookie in ASP.NET

The HTTP protocol, the fundamental protocol of the World Wide Web, is a stateless protocol. What this means is that from a web server’s perspective, every request is from a new user.

Cookie

  • You can create two types of cookies:
    • Session Cookie:
      • Exists only in memory.
      • Disappears when browser is closed.
      • If you do not specify an expiration date for the newly created cookie, then it will become a session cookie.
    • Persistent Cookie:
      • IE stores cookies in: \Documents and Settings\[user]\Cookies
      • FireFox stores cookies in: \Documents and Settings\[user]\Application Data\Mozilla\Firefox\Profiles\[random folder name]\Cookies.txt
  • Browser Relative
    • Cookies created by IE will not be recognised by FireFox.
  • Stored in Clear Text
    • You should never store sensitive information in a cookie.
  • Domain Relative
    • When a browser creates a cookie, the browser records the domain associated with the cookie and doesn’t send the cookie to another domain.
  • Cookie names are case sensitive.
  • Size
    • A single domain cannot store more than 4096 bytes which includes both the cookie names and the cookie values.
    • Most browsers restrict the number of cookies that can be set by a single domain to
      no more than 20 cookies (but not IE). If you attempt to set more than 20 cookies, the oldest cookies are automatically deleted. You can work around this limitation by creating multi-valued cookies.
  • Usage
    • Many parts of the ASP.NET Framework rely on cookies.
      • Web Parts
      • Forms Authentication
      • Session State
      • Anonymous Profiles
    • Many websites rely on cookies.
      • Yahoo
      • MSDN

Cookie Properties

  • Domain:
    • This property is useful when your organization includes subdomains.
    • Use this property to associate a cookie with a subdomain, but not an entirely different domain.
  • HttpOnly
    • Specify whether a cookie can be accessed from JavaScript code.
    • This property works only with Internet Explorer 6 (Service Pack 1) and above.
    • The property was introduced to help prevent cross-site scripting attacks.

Multi-Valued Cookies

  • A multi-valued cookie is a single cookie that contains subkeys. You can create as many subkeys as you need.
  • You can use the HttpCookie.HasKeys property to detect whether a cookie is a normal cookie or a multi-valued cookie.

Code Sample:

In the sample code, I will show you how to create, display and delete cookie.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestCookie._Default" Trace="true"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">


<title>Untitled Page</title>


</head>


<body>


<form id="form1" runat="server">


<div>


<asp:Button ID="btnCreate" runat="server" Text="Create Cookie" />


<asp:Button ID="btnPostBack" runat="server" Text="Post Back" />


<asp:Button ID="btnDisplay" runat="server" Text="Display Cookie" />


<asp:Button ID="btnDelete" runat="server" Text="Delete Cookie" />


<asp:Button ID="btnCreateMultiValued" runat="server" Text="Create multi-valued Cookie" />


<asp:Button ID="btnDisplayMultiValued" runat="server" Text="Display multi-valued Cookie" />


</div>


</form>


</body>


</html>

Partial Public Class _Default


Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


End Sub


Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click


Response.Cookies("TestUserFirstName").Value = "Sam"


Response.Cookies("TestUserFirstName").Expires = DateTime.MaxValue


Response.Cookies("TestUserLastName").Value = "Fu"


Response.Cookies("TestUserLastName").Expires = DateTime.MaxValue


End Sub


Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click


For i As Integer = 0 To Request.Cookies.Count - 1


Response.Write(String.Format("{0}: {1}<br />", Request.Cookies(i).Name, Request.Cookies(i).Value))


Next


If Request.Cookies("TestUserFirstName") IsNot Nothing Then


Response.Write("TestUserFirstName=" + Request.Cookies("TestUserFirstName").Value)


End If


If Request.Cookies("TestUserLastName") IsNot Nothing Then


Response.Write("TestUserLastName=" + Request.Cookies("TestUserLastName").Value)


End If


End Sub


Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click


Dim cookies As String() = Request.Cookies.AllKeys


For Each cookie As String In cookies


Response.Cookies(cookie).Expires = DateTime.Now.AddDays(-1)


Next


End Sub


Private Sub btnCreateMultiValued_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateMultiValued.Click


Response.Cookies("TestUser")("FirstName") = "Sam"


Response.Cookies("TestUser")("LastName") = "Fu"


Response.Cookies("TestUser").Expires = DateTime.MaxValue


End Sub


Private Sub btnDisplayMultiValued_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplayMultiValued.Click


If Request.Cookies("TestUser") IsNot Nothing Then


Response.Write(Request.Cookies("TestUser")("FirstName"))


Response.Write(Request.Cookies("TestUser")("LastName"))


End If


End Sub


End Class

Note:

  • Request.Browser.Cookies will only check whether the browser support cookie, not whether or not they're enabled.

How it works

Step 1. Before any request.


Step 2. Click the create cookie button. The server send the response back with an additional HTTP header to the browser. The HTTP response header looks like this: Set-Cookie: TestUserFirstName=Sam. This Set-Cookie header causes the browser to create a cookie named TestUserFirstName that has the value Sam. The 'Headers Collection' of the tracing ouput is always the request header, not the response header, so you cannot see 'Set-Cookie' in this section.



Step 3. Click the postback button. After a cookie has been created on a browser, whenever the browser requests a page from the same application in the future, the browser sends a request header that looks like this:Cookie: TestUserFirstName=Sam. The Cookie header contains all the cookies that have been set by the web server.


Note:

  • There are two HTTP headers, Set-Cookie and Cookie, that are related to cookies.
    • Set-Cookie response header is sent by the server in response to an HTTP request, which is used to create a cookie on the user's system. (Refer to Step 2 above)
    • Cookie request header is included by the client application with an HTTP request sent to a server if there is a cookie that has a matching domain and path. (Refer to Step 3 above)

Reference:

ASP.NET 2.0 Unleashed

Working with Cookies

Monday, September 15, 2008

WCF vs. ASMX

Protocols Support

  • WCF
    • HTTP
    • TCP
    • Named pipes
    • MSMQ
    • Custom
    • UDP
  • ASMX
    • HTTP only

Hosting

  • ASMX
    • Can be hosted only with HttpRuntime on IIS.
  • WCF
    • A WCF component can be hosted in any kind of environment in .NET 3.0, such as a console application, Windows application, or IIS.
    • WCF services are known as 'services' as opposed to web services because you can host services without a web server.
    • Self-hosting the services gives you the flexibility to use transports other than HTTP.

WCF Backwards Compatibility

  • The purpose of WCF is to provide a unified programming model for distributed applications.
  • Backwards compatibility
    • WCF takes all the capabilities of the existing technology stacks while not relying upon any of them.
    • Applications built with these earlier technologies will continue to work unchanged on systems with WCF installed.
    • Existing applications are able to upgrade with WCF
    • New WCF transacted application will work with existing transaction application built on System.Transactions

WCF & ASMX Integration

  • WCF can use WS-* or HTTP bindings to communicate with ASMX pages

Limitations of ASMX:

  • An ASMX page doesn’t tell you how to deliver it over the transports and to use a specific type of security. This is something that WCF enhances quite significantly.
  • ASMX has a tight coupling with the HTTP runtime and the dependence on IIS to host it. WCF can be hosted by any Windows process that is able to host the .NET Framework 3.0.
  • ASMX service is instantiated on a per-call basis, while WCF gives you flexibility by providing various instancing options such as Singleton, private session, per call.
  • ASMX provides the way for interoperability but it does not provide or guarantee end-to-end security or reliable communication.

References:

Pro WCF: Practical Microsoft SOA Implementation

Migrating ASP.NET Web Services to WCF

Monday, September 8, 2008

Sharepoint Lookup Column - Get multiple values

In this post, I will show you how to display lookup column data in a multiple selection format.

Steps:

1. I have a list called 'ATSCompany' which has a lookup column called 'Video' which references a document library called 'ATSCompany Videos'. Notice that I have selected the 'Allow multiple values' checkbox.

2. Edit a list item of the ATSCompany list, here is what it looks like.


3. After you have selected all 3 videos, the value of the video column is a string containing all the three video file name separated by semicolon.

4. In Sharepoint designer, I have created a custom list form for viewing the ATSCompany List. Right click the List and go to List Properties, you can see that I have specified CompanyProfile.aspx as the view item page for the ATSCompany Page. More
details.



5. In the CompanyProfile.aspx, it should displays three links which link to the videos in the 'ATSCompany Videos' document library.


6. In the Sharepoint designer, under the WebPartPages:dataformwebpart of the ATSCompany List. You will have the following to show the video links. Note that template ParseVideoString makes a recursive call to itself.

1 <xsl:if test=" string-length(@Video) &gt;0" ddwrt:cf_ignore="1">


2 <div>


3 <br />


4 <h3>Videos</h3>


5 <xsl:call-template name="ParseVideoString">


6 <xsl:with-param name="parse-string" select="@Video" />


7 </xsl:call-template>


8 </div>


9 </xsl:if>



1 <xsl:template name="ParseVideoString">


2 <xsl:param name="parse-string"></xsl:param>


3 <xsl:if test="not($parse-string='')">


4 <xsl:choose>


5 <xsl:when test="contains($parse-string, ';')">


6 <a href="../ATSCompany Videos/{substring-before($parse-string, ';')}" ><xsl:value-of select="substring-before($parse-string, ';')"/></a><br /></xsl:when>


7 <xsl:otherwise>


8 <a href="../ATSCompany Videos/{$parse-string}" ><xsl:value-of select="$parse-string"/></a></xsl:otherwise>


9 </xsl:choose>


10 <xsl:call-template name="ParseVideoString">


11 <xsl:with-param name="parse-string">


12 <xsl:value-of select="substring-after($parse-string, ';')"/>


13 </xsl:with-param>


14 </xsl:call-template>


15 </xsl:if>


16 </xsl:template>