Saturday, January 2, 2010

AJAX 4.0, ASP.Net 4.0, Visual Studio 2010 Beta

Hello friends, Microsoft has released Visual Studio 2010 beta 2 with ASP.Net 4.0, Ajax 4.0, MVC, Routing and more features.

I had looked at the overview of it and hence sharing it with you.

Output caching in ASP.Net 4

In ASP.NET 4 you can configure one or more custom output-cache providers. Output-cache providers can use any storage mechanism to persist HTML content. Because of this you can create custom output-cache providers for diverse persistence mechanisms, which can include local or remote disks, cloud storage, and distributed cache engines.

Auto Start Application Pool

Basically in iis 7 when any website have its first request it will take time to initialize the site and its data. Now in IIS7.5 with ASP.Net 4 you can set start the application automatically. So when your web site is started, it will initialized automatically and your application pool will be in Always Running mode.

Permanently Redirecting a Page

Previously we were using code to move a page permanently to another url with 301 redirection. In ASP.Net 4 Microsoft has provided RedirectPemanent.

Shrinking Session State

If you are not using in process session state you have a new feature in ASP.Net 4, you can compress the session state with compressionEnabled="true".

Range of Allowable URLs in HttpRuntime

Now you can specify allowed url length and query string length with maxRequestPathLength="260" and maxQueryStringLength="2048"

Valid Characters in URL in HttpRuntime

You can now specify invalid characters in your website with requestPathInvalidChars="<,>,*,%,&,:,\"

Request Validation in HttpRuntime

Now in ASP.Net 4 you can create your own request validator with requestValidationType="MyCustomValidator"

Header, URL and Html encoding in HttpRuntime

You can create your own encoder class for encoding for HTTP Header, URL and Html with encoderType="MyCustomEncoder"

Performance Monitoring

ASP.Net 4 also allow to enable performance monitoring out of the application domain.

 

There are  more features which I need to explore. Will try to post it asap.

Thursday, December 31, 2009

Microsoft Ajax Minifier 1.1

There is a new release of microsoft ajax minifier on codeplex.com

What Ajax minifier does?

Ajax Minifier reduce the size of your JavaScript files to improve the performance of you Ajax enabled site.

What does it support?

The Microsoft Ajax Minifier supports two levels of minification: normal crunching and hypercrunching. Normal crunching refers to the process of removing unnecessary whitespace, comments, semicolons, and curly braces. Hypercrunching refers to the process of shortening the names of local variables and removing unreachable code.

 

You can review the project at

http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=35893

Wednesday, December 30, 2009

Dynamic Populate Extender example Ajax Control Toolkit, manual populate control

Ajax control toolkit have control "Dynamic Populate Extender".

Dynamic populate extender is a control which will replace html/content of any control based on the web service result.
To populate you need to create a web service method which have single parameter with name "contextKey". If you do not specify contextKey variable or you make its name different then your extender will fire error "Web Service call failed: 500".

There are 2 ways to populate content.

Dynamic Populate option 1) You can specify attribute PopulateTriggerControlID="YourControlId" and then you click on the control and your data will be populated

Dynamic Populate option 2) You can write a javascript code which will call the populate event of Dynamic Populate Extender.
Script is

   1:  <script type="text/javascript">
   2:  function UpdateControl(value) {
   3:  var behavior = $find('dp1');
   4:  if (behavior)   {
   5:       behavior.populate(value);   
   6:      }
   7:  }
   8:  </script>

Based on above script “value” will be passed as “contextKey” in the web service.

Html code to for this example is

   1:  Enter value
   2:  <input type="text" id="txtConetnt" onblur="UpdateControl(this.value);" />
   3:  <asp:Panel ID="Panel1" runat="server"></asp:Panel>
   4:  <ajaxToolkit:DynamicPopulateExtender ID="DynamicPopulateExtender1" runat="server" 
   5:      TargetControlID="Panel1" BehaviorID="dp1" ServiceMethod="GetValue">
   6:  </ajaxToolkit:DynamicPopulateExtender>

Web service code for this example is

   1:  'VB
   2:  <System.Web.Services.WebMethod()> _
   3:  <System.Web.Script.Services.ScriptMethod()> _
   4:  Public Shared Function GetValue(ByVal contextKey As String) As String
   5:    Return String.Format("<span style='font-family:courier new;font-weight:bold;'>{0}</span>", "You entered value " + contextKey)
   6:  End Function
   1:  //C#
   2:  [System.Web.Services.WebMethod]
   3:  [System.Web.Script.Services.ScriptMethod]
   4:  Public string GetValue(string contextKey)
   5:  {
   6:       return String.Format("<span style='font-family:courier new;font-weight:bold;'>{0}</span>", "You entered value " + contextKey);
   7:  }

 

Hope this example help you to use Dynamic Populate Extender

Tuesday, December 29, 2009

Various URL rewriting methods in ASP.Net C# Overview


When we have multiple parameters in our URL, at the time it is possible that hackers can identify our logic based on parameter and try to get into the system with those parameters. To resolve this issue we can use URL rewriting. URL rewriting is the process of hiding our parameterized URL and make it simple, browser and crawler friendly URL.
Example:
URL rewriting is also useful for Search Engine Optimization (SEO). When you use parameter on your single page and load different data. Search engine (i.e. Google) will consider it as single page. But when you use URL rewriting it will consider that all pages are different.

Ways of Programming

In .Net there are 3 ways to achieve URL Rewriting.
1.       With Http handler factory
2.       With Http Module
3.       Global.asax

URL Rewriting with Http Handler Factory

Benefits

When you use handler based URL rewriting you can specify that for particular extension this handler will be called. So for each request your code will not execute. What handler factory does is select appropriate handler based on your code and returns it to compiler to handle the request.

Disadvantages

When you want to handle directory without any page you need to specify wild card for the handler factory. In this case each request will be go through your code. And for image or static files it will fire error (if not handled) that cannot parse file. To avoid this issue you can specify static file handler in web.config file above the wild card (such as .jpg, .gif), so your file will be treated as static.

URL Rewriting with Http Module

Benefits

When you are using this method you do not require to specify any extension or wild card in web.config. All file will be checked for rewrite and if any code is not available then it will load file as normal http request.

Disadvantages

In this case request for each either a page or static file like images will pass through the module. So it will cause more processing. If the code is not proper then it will execute each of your rule/code for all requests.

Global.asax

Benefits

This will work same as http module.

Disadvantages

This method does have one more disadvantage over http module. In IIS 7 some of methods are restricted so you either need to recode or need to go over http module.

Monday, October 26, 2009

How to use ListView Control of ASP.Net 3.5?

Hi Friends,

ASP.net 3.5 have one new control called ListView as compare to .Net framework 2.0.

Here i will mention a few benefits/use of ListView.

Now when we want to display items [our data] in repeated column but we need to break it to another row. for certain scenario we were previously using DataList of v2.0

What is difference between DataList and ListView
DataList renders table structure with  and  structure, while ListView have free form like repeater.

Now what is difference between Repeater and ListView
Repeater can repeat same ItemTemplate for each of your data item, while in ListView you will be able to repeate Same ItemTemplate and break it through GroupTemplate in LayoutTemplate.

LayoutTemplate is basically a template which will cover all of your items. In the LayoutTemplate you need to specify the GroupTemplate.

<LayoutTemplate >
  <div>
     <asp:placeholder ID="groupPlaceholder" runat="server">
  </asp:placeholder></div>
</LayoutTemplate >

LayoutTemplate must contain either GroupTemplate or ItemTemplate to bind and load the data.

Now we will have a question that what is GroupTemplate?
In GroupTemplate you will be able to specify your template of group of item.

<GroupTemplate >
  <ul>
     <asp:placeholder id="itemPlaceholder" runat="server">
  </asp:placeholder></ul>
</GroupTemplate >

In group template you must specify the itemPlaceholder in order to achive the items to be bound.

Now your will specify your item view in ItemTemplate
<ItemTemplate>
<!-- Your Item Code -->
  <li>
     <%# Eval("Name") %>
  </li>
</ItemTemplate>

Now you will say where would i define that how much item should be covered in one group. Here is the answer, you need to assign it to GroupItemCount property of ListView.

<asp:ListView ID="ListView1" runat="server" GroupItemCount="3">
///Your templates will go here LayoutTemplate, GroupTemplate, ItemTemplate
</asp:ListView>

Above code will repeat 3 item per group.

So output of this sample code will be like
<div>
  <ul>
     <li>Item 1</li>
     <li>Item 2</li>
     <li>Item 3</li>
  </ul>
  <ul>
     <li>Item 4</li>
     <li>Item 5</li>
     <li>Item 6</li>
  </ul>
</div>

Hope it helps.
I will post further details in another article.

Have a nice programming day.....