Keep Sites Running Smoothly By Avoiding These 10 Common ASP.NET Pitfalls

May 31, 2007

when  I am browsing through the net , I came across very good article.

  1. Does your app use LoadControl to load user controls dynamically? If so, make sure this is done in such a way that LoadControl will work both with and without output caching.
  2. Are you using kernel-mode output caching and session state in the same page? If so, remove the OutputCache directive to avoid caching that page or turn kernel-mode output caching off for the entire app.
  3. Are you calling FormsAuthentication.RedirectFromLoginPage in your ASP.NET 1.x-based app? If so, either change the second parameter to false or modify the Expires property of outgoing persistent forms authentication cookies to a more appropriate timeout.
  4. Do any of your pages generate inordinate amounts of view state? If so, turn off view state for individual controls or keep it on the server.
  5. Does your app use SQL Server session state? If so, make sure you disable sessions in pages that don’t use sessions.
  6. Does your app use the ASP.NET 2.0 role manager? If so, enable role caching to increase performance.
  7. Does your app use custom data types as profile properties? If so, you’ll avoid problems by creating custom data types that are compatible with XML serialization or by attributing the types as serializable and using the binary serializer to serialize and deserialize types.
  8. Does your app do database queries that take a long time to return? If so, consider whether the pages are candidates for becoming asynchronous.
  9. Does your ASP.NET app use client impersonation? If so, simple authentication in ASP.NET might enforce ACL-based permissions with less hassle.
  10. Have you profiled your app’s database activity? Examining communication between the app and database is a good first performance-tuning step.

Note: if you are interested in detail description on each topic then please visit following link.

http://msdn.microsoft.com/msdnmag/issues/06/07/Web…


Successfully Stream a PDF to browser through HTTPS

May 28, 2007

In this article I will try to explain you the technique of displaying file in the browser using Byte Array through HTTPS. Reason behind writing this article is, I need to implement this functionality in my application I invested lots of hours behind this and finally I found the solution.

Problem Description

I have a popup window in which to display a PDF file. This PDF is coming from Web Service call, which returns us a Byte Array. All ok so far, byte array is populated.

Byte Array is passed back as a MemoryStream. That MemoryStream is then sent out through ASP.Net’s Response object after setting the ContentType=”application/pdf”.

In development, I had no problems using the following code with HTTP:

Response.Clear();

//Send the file to the output stream

Response.Buffer = true;

//Try and ensure the browser always opens the file and doesn’t just prompt to “open/save”.

Response.AddHeader(“Content-Length”, ByteArray.Length.ToString());

Response.AddHeader(“Content-Disposition”, “inline; filename=” + PDFName);

Response.AddHeader(“Expires”,”0″);

Response.AddHeader(“Pragma”,”cache”);

Response.AddHeader(“Cache-Control”,”private”);

//Set the output stream to the correct content type (PDF).

Response.ContentType = “application/pdf”;

//Output the file

Response.BinaryWrite(ByteArray);

//Flushing the Response to display the serialized data

//to the client browser.

Response.Flush();

try{Response.End();}

catch{}

Everything seems fine until I moved it to our integration server which has HTTPS enabled.

Suddenly, I’m getting a dialog box stating “This page contains both secure and nonsecure items. Do you want to display the nonsecure items?” Clicking either Yes or No continued to display the document as normal, but there seemed no solution to getting rid of the dialog box.

If you do a Google on this text, you’ll only get back about four pages and one Groups post. Do enough digging and you’ll eventually find the following two MSDN articles:
http://support.microsoft.com/default.aspx?scid=kb;en-us;300443
http://support.microsoft.com/?kbid=321532

The second post leads you to the solution. Notice that the code above includes the length of the byte array being written to the output stream? I did, so I believed that ASP.Net would correctly populate the HTTP Headers indicating the length of the output. Unfortunately, this was a bad assumption. It turns out that IE6 requires the use of the “Accept-Ranges” HTTP header when HTTPS is used. Microsoft even acknowledges that there may be a bug in IE6. Since I am navigating to the PDF display page using GET and not POST, I can assume from the KB article that a POST wouldn’t work even after adding the “Accept-Ranges” HTTP Header.

Here’s the corrected code:

Response.Clear();

//Send the file to the output stream

Response.Buffer = true;

Response.AddHeader(“Accept-Header”,resultSet.Document.Length.ToString());

//Try and ensure the browser always opens the file and doesn’t just prompt to “open/save”.

Response.AddHeader(“Content-Length”, ByteArray.Length.ToString());

Response.AddHeader(“Content-Disposition”, “inline; filename=” + PDFName);

Response.AddHeader(“Expires”,”0″);

Response.AddHeader(“Pragma”,”cache”);

Response.AddHeader(“Cache-Control”,”private”);

//Set the output stream to the correct content type (PDF).

Response.ContentType = “application/pdf”;

Response.AddHeader(“Accept-Ranges”, “bytes”);

//Output the file

Response.BinaryWrite(ByteArray);

//Flushing the Response to display the serialized data

//to the client browser.

Response.Flush();

try{Response.End();}

catch{}

References:

http://blogs.ittoolbox.com/visualbasic/dotnet/archives/successfully-stream-a-pdf-through-https-1554

If you are looking for detail information on Content-Disposition Header, you can visit following link.

http://www.imc.org/ietf-822/old-archive1/msg03620.html


Dot Net 2.0 New Features collection

May 25, 2007

Every time I search for good technical article on .net, I end up with multiple results which are not to the point with some good example.

So I have compiled list of “Good” articles for core .Net 2.0 and asp.net 2.0 concepts.

Send me any good article link on Dot Net (2.0, 3.0), estimation, and architecture, design. I will compile this list again and update it.

Themes and Skins:
Reference: http://www.ondotnet.com/pub/a/dotnet/2004/08/30/themesandskins.html?page=1

Localization:
Reference:http://www.codeproject.com/useritems/localization.asp

Generics:
Reference:http://www.15seconds.com/issue/031024.htm

Partial Classes:
Reference:http://www.devx.com/dotnet/Article/22603

Nullable Types:
Reference:c-sharpcorner

Static Classes:
Reference:c-sharpcorner

Master Pages:
Reference:http://www.codeproject.com/aspnet/masterpages.asp

Selective Validation:
Reference:c-sharpcorner

Data Points:
Reference:msdn

Transaction Scope:
Reference:c-sharpcorner

Caching:
Reference:http://www.developer.com/net/asp/article.php/10917_3427131_1

Membership, Roles, and Profile:
Reference:http://aspnet.4guysfromrolla.com/articles/120705-1.aspx

Use Case Points:
Reference:http://www.codeproject.com/gen/design/usecasepoints.asp

Function Point:
Reference:http://www.codeproject.com/gen/design/Softwarecosting.asp

URL Mapping:
Reference:http://aspnet.4guysfromrolla.com/articles/011007-1.aspx

CSS Friendly Control Adapters:
Reference:http://www.asp.net/CSSAdapters/WhitePaper.aspx

Networking:
Reference:http://www.15seconds.com/Issue/051215.htm

ASP.NET Web Site Layout :
Reference:http://msdn2.microsoft.com/en-us/library/ex526337.aspx

Site Navigation:
Reference:http://aspnet.4guysfromrolla.com/articles/111605-1.aspx

ASP.NET AJAX:
Reference:http://ajax.asp.net/Default.aspx

Personalization:
Reference:http://msdn2.microsoft.com/en-us/library/ms379593(VS.80).aspx

for this article thanks to mukesh patel, who helped me a lot to create this list.


Making web pages printable using CSS

May 22, 2007

One of the classic problems with how web pages are designed, is that they are horrible to print. Horrible in the sense that page elements (text, images etc) don’t line up and unwanted images and navigation links get printed.

I am as guilty as anyone of creating pages like that, but I am aiming to fix it for your websites and mine. While most other web designers will create separate ‘printable’ pages (big waist of time), we will only be making some simple additions to our pages to make them ‘print friendly’.

Basic ideas:

  • People will print your pages typically to be able to read the content, not to see pictures. You need to hide all images from the printer and this is done in the print style sheet. I will explain what the print style sheet is in a moment.
  • Navigational elements are not needed in the printed document as well, so all navigational element should be removed from the printed page.
  • Let’s say you designed your pages with a black background with white text. You don’t want peoples printers printing all that black, they won’t be too happy with the price of ink these days! To solve this problem, in our print style sheet we will set the page color to ‘white’ and the text color to ‘black’.

The ‘print style sheet’:

I’ve mentioned the ‘print style sheet’ a few times with no explanation; let’s take care of that now. CSS today allows you to link to more than one style sheet in your web page.

The simple reason you would want to do this is so that you could have the HTML page ‘change’ it’s appearance automatically when someone visits your page with a particular type of device. These types of devices can include a typical desktop computer, a PDA (Windows CE device, Palm Pilot etc) and a printer among other devices!

It works like this; when you link a style sheet to your HTML page there is an attribute that you can specify in the CSS link tag that tells the device reading your page if it should use the style sheet specified in the link.

So the wise men and women that came up with the CSS specification, came up with a few ideas for device types that could be specified in the CSS link. For the sake of this short article we are concerned with only two of them:

1: ‘print’
2. ‘screen’

Here’s a sample pair of CSS links that point to different style sheets, one for the printer to use and the other for a normal PC monitor to use:

<link href=”CSS/webdesignersHanbook_Print.css” mce_href=”CSS/webdesignersHanbook_Print.css” rel=”stylesheet” type=”text/css” media=”print”>

<link href=”CSS/webdesignersHanbook_1.css” mce_href=”CSS/webdesignersHanbook_1.css” rel=”stylesheet” type=”text/css” media=”screen”>

You can see that these CSS links are actually taken from the Handbook on Killersites.com. What you’re looking for in the link is this text:

media=”print”
and
media=”screen

The first (‘media=”print”‘) points to the style sheet that has been set up for printing while the other (media=”screen”) is set up for normal PC monitors. Nowadays most browsers know that if someone tries to print the page the style marked with: ‘media=”print”‘ should be used.

So now we know how to tell the browser which style sheet to use when printing the HTML page, so what is different in the print style sheet that makes this work?

There are all kinds on things you can do, but the easiest is just to wrap HTML and images you don’t want printed with <div> tags and give all those <div> tags an ID of a style that is set to:

‘display: none’

What?!

Ok, the best way to understand is to see it work. Use the template for the CSS article on Killersites.com and create a new style sheet called something like ‘print.css’. In that style sheet you ad this:.noPrint {
display: none;
}
Now in your HTML pages just wrap DIV’s around elements you don’t want printed:

<div class=’noPrint’>
<p>Text that I don’t want printed.</p>
</div>

(Don’t forget to include your CSS links in the <head> of your HTML page!)

Those elements inside the DIV’s with the class ‘noPrint’ won’t print.

Another thing you should do is make all you text black and your pages white:

body {
font-family:
Georgia, “Times New Roman”, Times, serif;
font-size: 14px;
color: #000000;
background-color: #FFFFFF;

}

If you used font tags or set your font color on other tags (with CSS) in your HTML page you will have to change those as well, for example:

H1 {
color: #000000;
}

The above CSS sets all the H1 tags text to black.

If you used font tags you’re going to have to manually remove them and use CSS to style your fonts! That can be time consuming, but once you fix a site with crappy font tags, you will never use them again!

One last tip – create a print button on your page with this code:

<a href=”#” mce_href=”#” onClick=” window.print(); return false”>Print this page</a>

It’s just a link tag with a little bit of Javascript:

onClick=” window.print(); return false”


Follow

Get every new post delivered to your Inbox.