Surendra Sharma

Surendra Sharma

Search This Blog

Friday, May 29, 2015

Best SEO practices for ASP.NET Sitecore projects

If you developing any web applications, your client expect to implement SEO techniques. Here I am specifying some of the useful techniques that should be part of any website.

1.    Meta description and keywords

Add Meta tags for description and keywords in Head section.

<meta name="description" content="Put your description here" />
       <meta name="keywords" content="keyword1, keyword2, keyword3" />

2.    Authority Transfer

Do a 301 redirect to all important pages from old website.

3.    Poor URL Structure:
·         The URL’s should be in lower case
·         There should be no extension in URL like .aspx
·         There should be no spaces in the URL’s
·         Dash(-) sign should replace any spaces in the URL’s

4.    Custom URL provision:

There should be a provision to edit the URL for every page in the Sitecore admin.

5.    301 redirect provision:

Provision for 301 redirect on every page to do redirection from old URL to New URL.

6.    Default Title tags:

H1 tags should be used as default title tag for the page

7.    Open graph tags for Social media:

Implement basic og tags for social media like

·         Title
·         Description
·         URL
·         Image
·         Type

Here is example for this
<head>
<title>The Rock (1996)</title>
<meta property="og:title" content="The Rock" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />
</head>

Refer http://ogp.me/ for more details

8.    Canonical tag element

There should be a provision for adding canonical URL in every page. By default the current page URL will be used.

Here is the syntax:
<link rel="canonical" href="http://www.yourwebsite.com/">

Please leave your comments or share these tips if it’s useful for you.

Tuesday, May 19, 2015

Different ways to generate unique ID in C#

In every project we need to generate some id which should be unique. I was just wondering what are the options available in .NET to achieve this?

One can go for GUID as it’s a 128 bit integer long.

       Console.WriteLine("GUID = " + System.Guid.NewGuid().ToString());
       //Output - GUID = 54f6461a-59f4-4f9f-9a67-49b18ebc6eb6

This seems to be perfect but check GUID is alphanumeric. What if we anybody want only numbers. Holy shit.

We can use random class for it as

Random random = new Random();
Console.WriteLine("Random = " + random.Next(int.MaxValue).ToString());
       //Output - Random = 1344254902

But random class has limitation of max value of integer. It can generate only 10 digit long number. Probability of generating the duplicate number is high with Random class.

Is there any other option?

Interestingly we have datetime available in C#. We can get date as well as time and can generate long number.

Console.WriteLine(System.DateTime.Now.ToString("yyyyMMddHHmmss"));
//Output – 20150519190734

Its 14 digit long number now.

You say you want longer number than this. Don’t forget milliseconds placeholder. Most interesting part of millisecond is that it’s a 7 digit long. You can use millisecond from single f to 7 times f.

Console.WriteLine(System.DateTime.Now.ToString("yyyyMMddHHmmssfffffff"));
//Output - 201505191907346290105

Its 21 digit long number now which should be unique in its own way.
Don’t be greedy. Don’t try to write “f” more than 7 times otherwise you will get “System.FormatException: Input string was not in a correct format.” Error.

Come on buddy, 21 digits long number [Correct word for this is string] is enough for us.


Let me know if you know any other way to generate it.

Please leave your comments or share this code if it’s useful for you.

Saturday, May 16, 2015

How to fix 404 error for fonts in ASP.NET

My tester informed me that we are getting 404 for woff font file. I checked the presence of woff file in fonts folder. I checked the path of font file. Both these checks are positive and everything was seems to be correct. We were not getting the 404 error for any other font file except woff file. Then I googled it and come to know that people are already facing the same error for woff font file.

This is the problem of MIME type. So there are two ways to solve it.

·         IIS
·         Entry in ASP.NET project’s web.config file

Through IIS
          You to make MIME entry in IIS as below for this font

·         Run IIS Server Manager, Open Mime Types and add following
o    File name extension: .woff
o    MIME type: application/x-font-woff

Through web.config file

You can also add the entry of MIME types in the web config as follows

  <system.webServer>
    <staticContent>
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
    </staticContent>   
  </system.webServer>

I prefer the second option of web.config file as this file is a part of our project and we can change it at any time. But it’s very difficult to get the access of IIS on production or client environment.


Please leave your comments or share this tip if it’s useful for you.

Thursday, May 7, 2015

How to replace space by dash in URL in Sitecore

For the sack of SEO, it’s always a good practice to avoid any blank spaces in URL and replace them by dash (-).

It’s very easy to achieve this task in Sitecore. Just write below highlighted line just before closing tag of </encodeNameReplacements> in web.config file.

      <replace mode="on" find=" " replaceWith="-"/>
    </encodeNameReplacements>

That’s it. No programming needed.

You can use any other character also instead of dash.


Please leave your comments or share this code if it’s useful for you.

Tuesday, May 5, 2015

Tab order is changing on partial postback

I have three dropdown list on my page for country, state, city, area and society. All these 5 dropdown list are in update panel so that I can use partial rendering features of AJAX. Its working fine. All 5 dropdown list are working great and I was feeling proud that I implemented it.

But fuck to my tester who find out issue related with these dropdown list as well. Whenever he select state, my tab order is changed and instead of state itself, my default focus set to next control after society. However in any situation it should focus on current dropdown list.

I tried to solve it though javascript and JQuery but none of them work.

To solve this just write below code in current postback event as

        protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlList = (DropDownList)sender;
            ScriptManager.GetCurrent(Page).SetFocus(ddlList);
        }



Please leave your comments or share this code if it’s useful for you.

Page title is changing on partial postback

I have three dropdown list on my page for country, state and city. All these 3 dropdown list are in update panel so that I can use partial rendering features of AJAX. Its working fine. All 3 dropdown list are working great and I was feeling proud that I implemented it.

But fuck to my tester who find out issue related with these dropdown list as well. He has eagle eyes. Whenever he selected any of these 3 dropdown list, my webpage title changed to current URL of the page.

I debugged my code from start to end but unable to find out the reason? Why?

But fortunately its work around is just to set the page title again in the end of page load event as below.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                    //Some code here;
            }
            else
            {
                    Page.Title = "This is page title";
            }
}

Let me know if you have any better solution or particular reason of this behavior.

Please leave your comments or share this code if it’s useful for you.

Thursday, April 30, 2015

How to add HTML page in Sitecore?

My client requested to add HTML page in Sitecore project and link it to one of the item in footer. I asked to myself WTF, what kind of requirement is this? How to do this?

But finally I completed this. Here are the quick steps.

1. Add HTML page in any folder of your .NET project. Let’s say it is "/HTMLPages/faq.htm"
2. Add complete path of this HTML page in “IgnoreUrlPrefixes“ in web.config file so that Sitecore should not process this page request and ignore it. Let’s ASP.NET should serve this page request without using Sitecore.

<setting name="IgnoreUrlPrefixes" value="/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing|/HTMLPages/faq.htm"/>

3.    Specify this page in General Link field of Sitecore item. But the problem is it’s neither the internal link nor the external link. So how to provide the link so that it should take protocol and domain name automatically.

Here I use the Insert JavaScript option of General Link field as

javascript:window.open(window.location.protocol + "//" + window.location.host   + '/HTMLPages/faq.htm');

Here is screenshot of the General link field setting



Now when it renders, the item open this page in new window and take this page as a part of current website.

Sometimes client requirement plays a major role to improve our skills.

Please leave your comments or share this trick if it’s useful for you.