Showing posts with label SEO. Show all posts
Showing posts with label SEO. Show all posts

Combine ScriptResource ajaxcontroltoolkit script

Task: Combine the AjaxControlToolkit Script.

Description: Combine the AjaxControlToolkit Script. As we can see when we use AjaxControl on our webpage it load with several javascript resources (ScriptResource.axd). For SEO purpose we need to minimize the several request. So that we can combine these script in a single request.

First we need to know which script's are required for our webpage. for that we need to use ScriptReferenceProfiler (it is available in codeplex). So download it from codeplex and stored the dll in our 'Bin' Folder. And Use below code to find the several request.



<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="c1" %>
<%@ Register Assembly="ScriptReferenceProfiler" Namespace="ScriptReferenceProfiler"    TagPrefix="srp" %>

<html>
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm1" runat="server">
</asp:ScriptManager>
<srp:ScriptReferenceProfiler ID="ScriptReferenceProfiler1" runat="server" />
<%-- Below will be all controls with ajax controls whatever.. --%>
</form>
</body>
</html>

Result (On Browser):


11 references found on this page:
        <asp:ScriptReference name="MicrosoftAjax.js"/>
        <asp:ScriptReference name="MicrosoftAjaxWebForms.js"/>
        <asp:ScriptReference name="AjaxControlToolkit.Compat.Timer.Timer.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
        <asp:ScriptReference name="AjaxControlToolkit.Common.Common.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
        <asp:ScriptReference name="AjaxControlToolkit.Compat.DragDrop.DragDropScripts.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
        <asp:ScriptReference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
        <asp:ScriptReference name="AjaxControlToolkit.DragPanel.FloatingBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
        <asp:ScriptReference name="AjaxControlToolkit.DynamicPopulate.DynamicPopulateBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
        <asp:ScriptReference name="AjaxControlToolkit.RoundedCorners.RoundedCornersBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
        <asp:ScriptReference name="AjaxControlToolkit.DropShadow.DropShadowBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>

        <asp:ScriptReference name="AjaxControlToolkit.ModalPopup.ModalPopupBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>



Now we can combine the several Script request to one. For that we have to just copy the all found reference & use as below code .




<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="c1" %>

<html>
<head runat="server"></head>
<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="sm1" runat="server">
<CompositeScript>
<Scripts>
      <asp:ScriptReference name="MicrosoftAjax.js"/>
      <asp:ScriptReference name="MicrosoftAjaxWebForms.js"/>
      <asp:ScriptReference name="AjaxControlToolkit.Compat.Timer.Timer.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.Common.Common.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.Compat.DragDrop.DragDropScripts.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.DragPanel.FloatingBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.DynamicPopulate.DynamicPopulateBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.RoundedCorners.RoundedCornersBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.DropShadow.DropShadowBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
      <asp:ScriptReference name="AjaxControlToolkit.ModalPopup.ModalPopupBehavior.js" assembly="AjaxControlToolkit, Version=3.0.30930.28736, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
</Scripts>
</CompositeScript>
</asp:ScriptManager>
<%-- Below will be all controls with ajax controls whatever.. --%>
</form>
</body>
</html>


Now we can see in our browser the several ScriptResource combined in one request.

Create automated sitemap using asp.net

Task: Create automated sitemap dynamically using asp.net generic web handler.

Description: In E-Commerce website or blog we need to update sitemap regularly, instead of generating sitemap using third-party tool, we can generate sitemap dynamically using asp.net. Using "ashx" generic handler we can generate xml sitemap & using url rewriting we can rewrite "sitemap.ashx" for "sitemap.xml" request.


<%@ WebHandler Language="C#" Class="sitemap" %>

using System;
using System.Web;
using System.Text;
using System.Data;

public class sitemap : IHttpHandler {

    string currentTime = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
    public void ProcessRequest (HttpContext context) {

        context.Response.ContentType = "text/xml";
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Cache.SetAllowResponseInBrowserHistory(true);
        context.Response.Charset = "UTF-8";
        context.Response.Write("<?xml version='1.0' encoding='UTF-8'?><urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>");
        context.Response.Write(sitemap());
        context.Response.Write("</urlset>");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
    public string sitemap()
    {
        string weburl = "http://hemantrautela.blogspot.com/";
        System.Text.StringBuilder sitemap = new System.Text.StringBuilder();
        sitemap.Append(@"<url>
        <loc>" + weburl + @"</loc>
        <lastmod>"+currentTime+@"</lastmod>
        <priority>1.00</priority>
        </url>");
        // Append more url in your sitemap here using database
        return sitemap.ToString();
    }

Remove new line from title tag created dynamically

Task:  Remove extra new line from dynamically created title tag 

Description: Remove extra new line from dynamically created title tag.  As previously we created Title tag (Title of page) using code (Link) for SEO purpose. There is a little issue with Title tag, It takes extra new line but in SEO we want to remove this new line. we can use this in Master Page so that we needn't to rewrite it for every page.



using System.Reflection;
public static void InitializeTitleTag()
{
    Type type = typeof(HtmlTextWriter);
    FieldInfo field = type.GetField("_tagNameLookupArray", BindingFlags.Static | BindingFlags.NonPublic);
   
    Array lookup = (Array)field.GetValue(null);
    int index = (int)HtmlTextWriterTag.Title;
    object value = lookup.GetValue(index);
    type = value.GetType();
    field = type.GetField("tagType");
    field.SetValue(value, 0);
    lookup.SetValue(value, index);

}

Dynamic SEO for c# website,How To Set Html Title, Meta Keywords & Meta Description From Asp.Net(C#.Net)

Task: Put SEO stuff (Title of page, Meta keyword, Meta description) in website using c# code in asp.net website/ web application. Onsite SEO –in Dynamic Website or E-Commerce Website.

Description: Now these day SEO is a very important thing in a Website (Dynamic Website or E-Commerce Website). When we work on SEO for website then we require the setup page title, Meta keyword, Meta description. So we require option for this in admin panel for Content Management System Website.

Here I use a small function for this, you can connect it to database and call on pageload.

C# Code for Dynamic SEO

  public void settitle(string pagetitle, string metakeyword, string metadescription)
    {
        HtmlTitle obj = new HtmlTitle();
        obj.Text = pagetitle;
        Header.Controls.Add(obj);

        //  or title can be set as
        Page.Title = pagetitle;

        HtmlMeta mt = new HtmlMeta();
        mt.Name = "keywords";
        mt.Content = metakeyword;
        Header.Controls.Add(mt);

        //  or can be set as, for position of meta tag
        Header.Controls.AddAt(2,mt);

        HtmlMeta mtd = new HtmlMeta();
        mtd.Name = "description";
        mtd.Content = metadescription;
        Header.Controls.Add(mtd);

       //  or can be set as, for position of meta tag
        Header.Controls.AddAt(2,mtd);
    }