Wednesday, September 15, 2010

Url Rewriting Made Easy - Article 3

After implementing URL Rewriting there are certain issues which we need to tackle, so that URL rewriting logic will not affect web application. This is I am talking with reference to URL Rewriting for Search Engine Optimization, where we are preferring hackable URLs.
This article is continuation of my previous articles on URL Rewriting.
  1. Url Rewriting Made Easy- Article 1 (Demo Code)
  2. Url Rewriting Made Easy- Article 2 (Demo Code)
Basic things which I considered while developing logic was, Url Rewriting should be very simple to understand and implement. And it should be considered while developing the web application and not after completing the development.
What happens when we do URL Rewriting? On the client side i.e. on Browser we display URL which is hackable (i.e. Easy to understand and can be remembered easily, which also directly or indirectly displays directory structure.). On server side page get redirected to some other page using query string and rest of the operations can be performed in normal way. But There is one serious thing which we need to consider, normally when we Use Images and JavaScript files in HTML logic we tend to give relative path; and this path is picked by browsers to display images and include JavaScript files in HTML code.
In case of URL Rewriting we have discussed in Article 1 and Article 2, we need to keep work around for images and JavaScripts. As per the logic Raw Urls (i.e. Hackable URL) is being displayed and used by the browser, but for displaying Images which belongs to different folder we need to use fully qualified urls.
USE FULLY QUALIFIED URLS FOR IMAGES AS WELL AS TO INCLUDE JAVA SCRIPT FILES, SO THAT URL REWRITING CAN WORK. You can always go for Image Handlers or manipulate Image Urls by making Images server side controls. for including JavaScripts you can either Manipulate the Head Section Dynamically or use your own logic. Submit this story to DotNetKicks

Read more...

Wednesday, September 8, 2010

Handling Head Section Dynamically

When it comes to Search Engine Optimization there are few modifications we need to do in Header Section. If a web application has simple structure then handling Head Section is an easy task. But consider an application like E-Commerce store where there is huge collection of different kind of products which belongs to various categories and sub categories. In that case we have to manipulate many things for every request depending on the product, its details, depending on category and subcategory. ( URL Rewriting is one the important thing for Search engine optimization, which I have already discussed in some articles, Article 1, Article 2).
Head section also plays an important role for Search Engine Optimization, in this article we will discuss all elements which matters in HEAD section and can be handled dynamically in asp.net.
  • Handling Page Title: Page title can contain many things explaining details or abstract about the Product or about the current page which is being displayed for some purpose. User might click on some link and page is being redirected to display some product or category. In that case Page Title should also be change accordingly. This can directly be handled as follow


    protected void Page_Load(object sender, EventArgs e)
        {
            this.Header.Title = "This is Demo Title";
            //we can also use following
            //Page.Header.Title or Page.Title or this.Title
            //Can also apply some switch case, depending on Query String.
            //depending on product or category details
        }
  • Adding JavaScript in Head Section: We can either load direct JavaScript in HEAD section or we can add JS file in HEAD Section. check out following codes


    protected void Page_Load(object sender, EventArgs e)
        {
            HtmlGenericControl HTMLControl = new HtmlGenericControl();
            HTMLControl.TagName = "script";
            HTMLControl.Attributes.Add("type", @"text\javascript");
            HTMLControl.InnerText = @"alert('Insert Your JavaScript as String HERE instead of this Alert')";
            this.Page.Header.Controls.Add(HTMLControl);
        }

    Also we can add JavaScript file reference as below.



    protected void Page_Load(object sender, EventArgs e)
        {
            HtmlGenericControl HTMLControl = new HtmlGenericControl();
            HTMLControl.TagName = "script";
            HTMLControl.Attributes.Add("type", @"text\javascript");
            HTMLControl.Attributes.Add("src", "yourJSFile.js");
            this.Page.Header.Controls.Add(HTMLControl);
       }
  • To add content Meta Type in HEAD Section we can use following code.


    protected virtual void AddMetaContentType()
    {
    HtmlMeta meta = new HtmlMeta();
    meta.HttpEquiv = "content-type";
    
    meta.Content = Response.ContentType + "; charset=" + Response.ContentEncoding.HeaderName;
    
    Page.Header.Controls.Add(meta);
    
    }
    
  • To Add Meta Section, e.g. For Keyword and Description we can use following function

    protected void AddMetaTag(string name, string value)
      {
       if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(value))
        return;
    
       HtmlMeta meta = new HtmlMeta();
       meta.Name = name;
       meta.Content = value;
       Page.Header.Controls.Add(meta);
      }
    
Submit this story to DotNetKicks

Read more...