Mehfuz's WebLog

Live crazy, think different!

Sponsors

News

Passionate about cutting edge technologies and facinated by the modern web and phone revolution.Currently working at Telerik Corporation, the leading .net component vendor.
Follow me


Articles


Projects

January 2008 - Posts

Keep your CSS files clean with a tiny HttpHandler

CSS, is the heart of any web application today. Writing cross browser CSS is really a hell of an art and including CSS files selectively and writing CSS hacks that will work out in one browser but wont in other is also a tedious job.

Anyway, I have come up with a tiny CSS handler , which might be handy in terms of loading multiple CSS for different browsers, removing extraneous characters that can help reduce file size and do some caching on the browser end.

At the end of post , I have given the files to download, but before that lets see how can we put it in our project.

First of all , add it to your .aspx or .master file.

<link rel="Stylesheet" href="StyleHandler.axd" type="text/css" />

Then , add the handler to the httpHanlders section in  web.config

<add verb="*" path="StyleHandler.axd" type="CssHandler" validate="false"/>

Of course, the CssHandler.cs should be under App_code. Also, validate=false means the underlying handler file wont be verified for its existence(Ex. there is no physical existence of StyleHandler.axd). Finally, you need to include a tiny xml file to the web project, that will have the browser to CSS mappings.

Inside CssMappings.xml

<cssMappings>
  <add browser="all" file="default1.css|default2.css"/>
  <add browser="ie*" file="allie.css" />
  <add browser="firefox*" file="allfirefox.css" />
</cssMappings>

Terminology

"all" means the CSS will be included for all browser types. ie*, firefox* means the CSS file will be used for all versions of that particular browser. Similarly, when specific browser number is used (Ex.ie7), the corresponding CSS file will only be included for that specific type of browser.In some scenarios, it is necessary that we have customized CSS for different version of the same browser, in that case ie* and ie7 distinction is useful. "|" is used for multiple CSS files that will be combined for a given browser type. You can also add "safari" and other known browsers that you wish to target CSS for, but you need to be sure of the name which is passed in HttpRequest for a particular browser to put it in mapping file, as the handler compares mapping entry with Request.Browser property values.

--

Additionally, the handler does a basic compression , by removing comments, newlines and tabs that makes browser feel happy.

To see , how the code works or to give a try, download it here

Updated Jan 27, 2008 : Added Gzip compression

kick it on DotNetKicks.com

Posted: Jan 25 2008, 11:36 AM by mehfuzh | with 11 comment(s) |
Filed under: ,
Article of the day at Asp.net

My Creating custom LINQ provider using LinqExtender is article of the day at asp.net. Those of you haven't check that out yet, I would suggest to have a look, it shows the easiest way of creating LINQ providers without any Expression parsing knowledge.

image

Have Fun!!

REST with LINQ to XML

With LINQ to XML, what I have learned so far, it's awesome, it is not only the first class concept of processing XML data, but also let you process a complex XML doc in the most simplest and readable way.

I have a project called, Linq.Flickr , and I often need to process  XML data for REST query.

To show a simple example of LINQ to XML, lets take a REST response from a Flickr method called flickr.people.getUploadStatus and see how it is processed.

<user id="12037949754@N01" ispro="1">  
<username>Bees</username> 
<bandwidth maxbytes="2147483648" maxkb="2097152" 
usedbytes="383724" usedkb="374" remainingbytes="2147099924" remainingkb="2096777" /> 
<filesize maxbytes="10485760" maxkb="10240" /> 
<sets created="27" remaining="lots" /> 
</user>

Before .net 3.5. it could have been done by GetElementyByTagName("users") - > inside the while loop getting the element, reading value, storing it in private variables , going to next sibling and repeat, if nested , then take the first child and so forth. But , in .net 3.5 using LINQ , the translation is not only just like XML itself, moreover , in XElement.Load , we can directly pass the REST query string.

So, Two, steps to do a REST with LINQ

  1. Build the URL and pass it to XElement.Load
  2. Parse the response and take necessary actions.

For example, for the above XML, I wrote something like below, in PhotoRepository.cs, that does a selective parsing.

try
{  
    XElement element = GetElement(##REQUEST URL##);

    People people = (from p in element.Descendants("user")
      select new People
      {
            Id = p.Attribute("id").Value ?? string.Empty,
            IsPro =
            Convert.ToInt32(p.Attribute("ispro").Value) == 0 ? false : true,
            BandWidth =
            (from b in element.Descendants("bandwidth")
               select new BandWidth
                {
                      RemainingKB = 
                      Convert.ToInt32(b.Attribute("remainingkb").Value),
                      UsedKB = Convert.ToInt32(b.Attribute("usedkb").Value)
                 }).Single<BandWidth>()
               }).Single<People>();

    return people;
}
catch (Exception ex)
{
    throw new ApplicationException("Could not process response", ex);
}

Note :GetElement is a local method that does a XElement.Load, with some validation check.

Here my Client-Side object looks like

People
Id
IsPro
BandWidth
   RemainingKB
   UsedKB

The most interesting thing is, everything is done on the fly , no loop, no intermediate result tracking, no headache of sibling and first child.Overall, the code has a high point in terms of readability.

Take that !!

kick it on DotNetKicks.com
[New Article] Creating custom LINQ provider using LinqExtender

First of all happy new year, secondly my new article is just published at Dotnetslackers. This explains the detail of creating custom LINQ providers using Linqextender.

You can check that out at http://dotnetslackers.com/articles/csharp/CreatingCustomLINQProviderUsingLinqExtender.aspx

Moreover, I would like to add some tiny enhancements to the extender.

1. All the extender descendant can now do something like.

var query = (from q in _queryContext
             orderby q.LastUpdated descending
             select new { q.Id, q.Title, q.LastUpdated }).Take(1);
 
int count = query.Count(); 
2. OpenLinqToSql, which is a part of Linqextender, now, supports SQLCE database.
3. Easy OnError event for exception trapping.

In the end, there are plenty of things that needs to done and to be told, but the article explains exactly how to get started and create a provider in no time. Please take a look , and if possible give some votes.

kick it on DotNetKicks.com 
More Posts