SharePoint world of ECM and Information Management

.NET Tips: ASP.NET

back to content

INFO:  

  • IIS6 application pools are configured to recycle after 1740 minutes. Configure the application pool to recycle at a specific (quiet) time of day. >>

  • HttpRuntime.CodegenDir retrieves the path to the sub-folder where the temp files of a given application are located. >> 

  • Application pages are compiled in batch mode into the single assembly. By default there are no more 1,000 pages and size is no more 1MB. The attributes <maxBatchSize> and <maxBatchGeneratedFileSize> let you limit the number of pages and the overall size of the assembly. >> 

  • Browsers check the domain name of the server as a string literal. It means that http://www.microsoft.com and http://66.35.250.176 are treated as different domains even if the IP address of www.microsoft.com is actually 66.35.250.176.

  • To detect the IIS version the [HKLM\Software\Microsoft\InetStp\MajorVersion] is used, and all sub-components are there[HKLM\Software\Microsoft\Microsoft\Windows\CurrentVersion\Setup\Oc Manager\Subcomponents].

  • The result memory consumption of DataSet in web server can take more that 4x time from the original DataSet size.

DataBind - 2x time, rendering to HTML - 3x time, saving to ViewState - 4x time. ("Expert ASP.NET 2.0" book, p.95)
  • The custom HttpHandler doesn't generate Init and Load events, and works like simple page request with only Rendering event. ("Programming Microsoft ASP.NET 2.0 Applications: Advanced Topics" book, p.63)

  • The global web.config for all sites locates at %Windows%\Microsoft.NET\Framework\[version]\CONFIG

  • ShadowCopy (using Temporary directory) can be turned of by <hostingEnvironment shadowCopyBinAssemblies="false">

  • ASP.NET reserves special thread pool for the local calls, for example to call Web Service. ("Programming Microsoft ASP.NET 2.0 Applications: Advanced Topics" book, p.105)

  • "<%=" and "<%#" are differ in the aspect of evaluation. '=' expression is evaluated at render time. '#' are only evaluated at DataBind() and can be used as properties in server-side controls. >>

  • When UpdatePanel control performs an asynchronous AJAX callback to the server to update its content, the request contains everything a conventional ASP.NET postback contains, including view state. >>

  • ASP.NET 1.1 persisted viewstate starts with "dDw...", when the ASP.NET 2.0 one starts with "/wE...". >>

  • Consider using microformats for they semantically meaningful human-readable content to allows the information to be easily detected, extracted, cross-referenced, indexed, and searched for. >>

  • ViewState can be moved to the end of the page, which allows to search engines see more of your content rather than the big portion of ViewState. >>

  • Impersonalization doesn't propagate to the new threads, so you need to pass the WindowsIdentity object to the new thread and from there impersonate again manually >>

  • ASP.NET has an outgoing connection limit which is set to 2 connections by default, so each next call to the Web Service or HttpRequest will be queued. To increase this number set the desired number of simultaneous into "maxconnection" attribute of connectionManagement element

  • ScriptManager is used only once on the page. If you need to add another ScriptManager, then you can use ScriptManagerProxy with the same signature. 

  • Call to Web Service can be determinated by the "/js" suffix in URL. It's exactly how ScriptHandlerFactory works, using either ASP.NET AJAX Http handler or standard one.

  • When UpdatePanel performs an asynchronous AJAX callback to the server to update its content this request contains everything a conventional ASP.NET postback contains, including view state. So, all unnecessary info is sending

  • There's a bug in ASP.NET 2.0 that you cannot change the max-age header and cache your requests. To avoid this u need to use Reflection to change _maxAge field from HttpCachePolicy class >>

  • First CLR threadpool worker thread is never initialized for COM+ so if someone instantiates a component there that calls CoInitialize, the thread can become an STA thread and cause this type of blocking >>  
  • new Calling Response.Redirect WILL NOT execute the finally block
  • There is no 100% guarantee way to handle the browser close event. Everything you can do is to use:
       1: <body onbeforeunload="BrowerClose();">
    to handle the page close; 

       1: function handleWindowClose()
       2: {
       3:     if((window.event.clientX<0) || (window.event.clientY<0))
       4:     {
       5:         event.returnValue = "Closing by X button";
       6:     }
       7: }
    to handle the X button; 

       1: if (event.altKey==true && event.keyCode==0 ) 
       2: { 
       3: alert("ALT + F4 "); 
       4: }
    to handle the ALT+F4, but nothing handling browser closing from toolbar or kill the process from task manager
  • Consider using AJAX Patterns to improve UX, like Unique URL, Timeout, Browser-Side Templating
  • With partial rendering you can only have one request pending at a time per session - like in a normal AJAX page. But you can have number of simultaneous calls if you control them directly through XMLHttpRequest >> .
  • ... 

 

TIPS:

  • To provide CSS among all pages use Master Page with within it. >>

  • Prefer not to rely on HttpContext because it complicates migrating to the WCF, where the app can be hosted in many types of app and not only in ASP.NET. >> (p.314)

  • Make the callback functions static (for Cache for example) to avoid keeping the page instance into the memory (up to the Gen2) >>.

  • To bring the ASP.NET web site down just put the file names App_Offline.htm in a web site's root folder. It switches the site to maintenance mode. The app-domain is shut down and all incoming requests return an error code with the contents of the App_Offline.htm

    >>
  • If you need to post data from ASP.NET 2.0 to 1.1 you can use either Response.Redirect, passing the params to the Query String, or overriding the LoadPageStateFromPersistenceMedium() method (returning empty object), so the posted viewstate will be ignored and data is sent via POST. >>

  • Prefer using MethodAsync/RegisterAsyncTask rather than AddOnPreRenderCompleteAsync to implement asynchronous pages. Because MethodAsync flows impersonation, culture, and HttpContext.Current to the MethodCompleted event handler. Moreover, the work with the multiple async calls is easier. >>

  • Avoid using ThreadPool.QueueUserWorkItem for async cals in ASP.net because it relies on the same pool, not the competion IO ports. >>

  • To use ASP.NET controls in XSLT you need to run some code inside the HTTP Pipeline before the PageHandlerFactory runs, implementing HttpModule. >>

  • If you plan to use 3rd party cookies which come from other domain you need to use Response.AddHeader("P3P", "CP=\"CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE\""); >>

  • To get the registered ASP.NET version check the [HKLM\Software\Microsoft\ASP.NET]. The existed key shows the registered ASP.net version with IIS.

  • To use the strongly-typed access to MasterPage use MasterType directive. >> 

  • Using WebConfigurationManager significanlty simplify works with web.config.


  • Use the AllowCustomerErrors or OnAsyncPostBackError to handle errors in ASP.NET AJAX UpdatePanel.

When you use UpdatePanel and errors arise in the code beneath it the standard ASP.NET page error handle mechanism can't handle these errors and the message box will be shown on the client. To avoid this your need to provide custom error handling using the UpdatePanel method to process async errors - AllowCustomerErrors or OnAsyncPostBackError. >> >> 

  • Using <img src='<%# expression_to_get_image_ulr %>' /> provides the most effective way to show the images on the page.

This approach doesn't waste server resources. ASP.NET transforms #-expression into DataBoundLiteralControl where DataBind use the #-expression to show image.
  • To avoid rendering the <div> around hidden fields use RenderDivAroundHiddenInputs method of XhtmlTextWriter class.

Having set the RenderDivAroundHiddenInputs property to true, or turning LegacyRendering mode on in web.config ASP.NET won't generate the <div> around the hidden fields. The only problem is that current property of HtmlTextWriter (which is used by default in page rendering) is always set to false. The solution is to override either the Render method of the Page class or the HtmlTextWriter.CreateHtmlTextWriter method to return the XhtmlTextWriter instance. >>
Alternative solution is to use CSS >>.

  • Use the HtmlWriter.WriteBreak()  to write the BR with closed tag.
  • You can remove the ASP.NET version from the header (X-AspNet-Version => 2.0.50727) using <httpRuntime enableVersionHeader="false" /> >>
  • Use trace.axd if you what to get the trace info when AJAX is applied, because the standard trace is not updated in this case.  

  • Instead of using the WebService to make the calls from client to server you can use "Page Methods"

This approach allows you to have the service methods implemented within your codebehind files using the [ScriptService] and [WebMethod] attributes. Cliens have access to these page methods through the PageMethod proxy, which is enabled by setting a ScriptManager control’s EnablePageMethods property to true >>

  • To control start or cancel the UpdatePanel updates use the set_cancel / abortPostBack methods of the initializeRequestEventArgs object, which is the second params of the initializeRequest event of the PageRequestManager class. >>   

  • To avoid flickering in CollapsiblePanel set the right styles: height = "0", CSS overflow = "hidden";

  • To use the Server.MapPath() in static environment, where no context exists (like library) use the HostingEnvironment.MapPath() >>

  • Consider using window.addEventListener in JS to avoid conflicts with multipe windows.onload events >>
  • use "application/xhtml+xml" MIME type to have the strict XML parsing support in Firefox (closing tags in rights places) >>
  • The only way to apply themes to all pages on your site dynamically is to create HttpModule, and add theme to Page.Theme property.
  • To disable the postbacks for <asp:XXX> server controls and call only client code remove OnClick attribute, add UseSubmitBehavior="true" and add "return false;" to the very end of OnClientClick methods call;
  • new using jQuery to call the web service directly, we’ve eliminated over 100 KB of JavaScript and three extra HTTP requests.


 back to content

 

Leave a Comment

(required) 

(required) 

(optional)

(required)