Web Technologies

BLOG for Web Technologies

Freewares, Free E-Books Download, SEO, Tips, Tricks, Tweaks, Latest News, .Net, PHP, ASP, ASP.Net, CSP, MS SQL Server, MySQL, Database
earnptr.com
Monday, January 28, 2008
ASP.NET: Controlling Caching in ASP.NET Web Forms
ASP.NET allows you to cache pages. The means that when the code behind the page runs, it produces HTML, the HTML is sent down to the client, however a copy is stored in the memory of the web server. If the same page is requested again, then the page is retrieved from the cache and the code is not rerun. You have virtually infinite flexibility on controlling when the cache gets flushed.

The most basic caching is implemented by placing this line at the top of your ASPX page:

<%@ OutputCache Duration="3600" VaryByParam="none"%>

What this tells the ASP.NET caching code is to cache the page for one hour.

The full spec for the @OutputCache lines is:

<%@ OutputCache Duration="#ofseconds" Location="Any � Client � Downstream � Server � None" VaryByControl="controlname" VaryByCustom="browser � customstring" VaryByHeader="headers" VaryByParam="parametername" %>

Duration is a count in seconds to cache.

Location allows the caching to occur on the server, on the client, on a proxy server in between. The default is Any. If you always want server caching (which seems to me to be the most useful choice), change the line to read:

<%@ OutputCache Duration="3600" Location="Server" VaryByParam="none"%>

VaryByControl
is only used by user controls, not on standard web pages. See the .NET documentation for more details.

VaryByCustom="browser" keeps a different copy of the output for each browser name and major version information. So if you have cloaking by browser version going on (which is easy to implement in .NET), then each separate page will get delivered.

VaryByCustom="customstring" Allows you to specify a string that will be passed to your code. To make this useful, you must then override the GetVaryByCustomString method in the Global.asax file. For example, place this line in your ASPX file:

<%@ OutputCache Duration="3600" Location="Server" VaryByCustom="Referer" VaryByParam="none"%>

Then in your Global.asax file add the following code:

public override String GetVaryByCustomString(System.Web.HttpContext hcContext, String strCustom)
{
switch (strCustom)
{
case "Referer":
Uri uriReferrer = hcContext.Request.UrlReferrer;
String strRet;
if (uriReferrer != null)
strRet = uriReferrer.Host;
else
strRet = null;
return strRet;
default:
return base.GetVaryByCustomString(hcContext, strCustom);
}
}

VaryByHeader allows you to cache based off of some field in the HTTP header sent by the client. The classic example is based off the Accept-Language header line.

VaryByParam allows you to cache different versions based off of querystring or post field parameters. So http://www.domain.com/foo.aspx?bar=baz would be cached separately from http://www.domain.com/foo.aspx?bar=bletch

There are also ways of controlling the caching through code.

Labels: , ,

posted by WebTeks @ 6:29 PM   0 comments
Monday, January 14, 2008
Read & Write XML through ASP.Net
Introduction

The main reason for writing this simple article is so many guys are asking doubts how to read Xml ,how to Write Xml in DotnetSpider Questions section. I Thought this article Will helpful for beginners. You can also find an article about how to write Xml document at
http://www.dotnetspider.com/kb/SubmitSample.aspx?ArticleId=2066

System.Xml namespace contains the XmlReader and XmlTextReader.
The XmlTextReader class is derived from XmlReader class. The XmlTextReader class can be used to read the XML documents. The read function of this document reads the document until end of its nodes.
Using the XmlTextReader class you get a forward only stream of XML data j It is then possible to handle each element as you read it without holding the entire DOM in memory.
XmlTextReader provides direct parsing and tokenizing of XML and implements the XML 1.0 specifications

This article explains how to read an Xml file.

Adding NameSpace as Reference

The first step in the process of reading Xml file is to add System.Xml namespace as reference to our project ,since System.Xml namespace contains the XmlReader and XmlTextReader.


using System.Xml;

let us assume there is an Xml file in c:\Dir\XmlExample.Xml

Open an Xml Document

Create an instance of an XmlTextReader object, and populate it with the XML file. Typically, the XmlTextReader class is used if you need to access the XML as raw data without the overhead of a DOM; thus, the XmlTextReader class provides a faster mechanism for reading XML.

XmlTextReader MyReader = new XmlTextReader("c:\\dir\\XmlExample.Xml");

the above code opens Xml file

Reading Data

The Read method of the XmlTextReader class read the data. See the code

while (MyReader.Read())
{
Response.Write(MyReader.Name);
}

that’s all now we are ready to read our Xml file from the above specified directory


Read the XML File into DataSet


You can use the ReadXml method to read XML schema and data into a DataSet. XML data can be read directly from a file, a Stream object, an XmlWriter object, or a TextWriter object.

The code simply looks like the following

string MyXmlFile = @"c:\\Dir\\XmlExample2.xml";
DataSet ds = new DataSet();

System.IO.FileStream MyReadXml = new System.IO.FileStream(MyXmlFile,System.IO.FileMode.Open);
ds.ReadXml(MyReadXml);

DataGrid1.DataSource = ds;
DataGrid1.DataBind();


Thus we can read Xml Data into Dataset .

Labels: ,

posted by WebTeks @ 6:18 AM   1 comments
Previous Post
Archives
Links
Template by

Free Blogger Templates

BLOGGER

Subscribe in NewsGator Online Subscribe in Rojo Add to Google Add to netvibes Subscribe in Bloglines Web Developement Blogs - BlogCatalog Blog Directory Blogarama - The Blog Directory Blog Directory & Search engine Computers Blogs - Blog Top Sites Top Computers blogs