ASP.Net 4.0 New Feature - 1 : SEO improvement through Redirect Permanently

Monday, June 6, 2011

ASP.Net 4.0 has added many features to its hat to cater various facilities to different user base. Permanent redirection feature helps in SEO. Before we understand more about the functionality let us look at the definitions of status codes sent to browser set by W3c.

10.3.2 301 Moved Permanently
The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise.
.
.
.
10.3.3 302 Found
The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.
.
.
.
Link

To simplify when browser receives code 301 it understands that resource has been permanently moved to reported website and 302 is considered as temporarily. So when we use permanent redirection through code behind server sends code 301 in head so each subsequent requests are auto redirected to new URL without involving round trip.

Response.RedirectPermanent("/about.aspx");

This function has one overload same as Response.Redirect to tell to end the response.

Design Pattern - Daily Learning

Friday, June 11, 2010

To begin with we write a game where ducks can swim and quack. We used OO inheritance:




One way was to create a virtual function in Duck class and override in Rubberduck class. But then again we realized that requirements keep changing and there is a new type of duck added called DecoyDuck which should neither flies nor quacks. And we will have many different types of duck to be added to the game in future behaving differently and we will be forced to override everytime.


So we thought of implementing an interface for fly and quack methods. Duck that fly and quack will implement that interface. So we have separated the behaviors that vary.

Later we wanted to ducks to fly in the game. If I add fly method to Duck class then all the ducks will start flying including Rubberduck which I don't want to fly.


Identify the aspects of your application that vary and separate them from what stays the same.


Based on the above principle, fly and quack are varying. So we'll pull out both the methods and create a new set of classes to represent each behavior.


To keep things more flexible we would want to assign behaviors to the instances of Duck class during run time and should be able to change it.


Program to an interface not an implementation


So based on the second principal just mentioned, we will create interfaces for the Fly and Quack.

So Duck class will look something like this


public class Duck

{

IFlyBehavior flyBehavior;

public void PerformFly()

{

flyBehavior.Fly();

}

}


public class MallardDuck : Duck

{

public MallardDuck()

{

flyBehavior = new FlyWithWings();

}

}


So now things are loosely coupled and cal be reused. What if I have to change a fly behavior at run time. It simple now, we just have a setter for flying behavior of duck in Duck class.


SetFlyBehavior(IFlyBehavior ifb)

{

flyBehavior = ifb;

}


Let us See how we will use setter


Duck d = new MallardDuck ();

d.PerformFly();

d.SetFlyBehavior( new FlyNoWay());

d.PerformFly();






We noticed that each Duck has a FlyBehavior to which it delegates its flying behavior.

So we are not inheriting their behavior but letting them change during runtime. Putting classes them together in such a way is called Composition.


Favor Composition over Inheritance.


So with these three principals, we have learnt first design pattern called Strategy


The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.

Inheritance - Daily Learning

Tuesday, June 8, 2010

I always wonder that most of the people don't realize the importance of virtual and override words in the function. Everytime I ask, many (8 out of 10) of them tend to answer that virtual function can be overridden by child class. When you don't define base class function as virtual and child class function as override then also it overrides. So here I am going to show the difference with the following example in Console App.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inheritance
{
class Program
{
static void Main(string[] args)
{
CA ca = new CA();
CB cb = new CB();
CA cab = new CB();

ca.fun1();
ca.fun2();
ca.fun3();
ca.fun4();

cb.fun1();
cb.fun2();
cb.fun3();
cb.fun4();

cab.fun1();
cab.fun2();
cab.fun3();
cab.fun4();
Console.ReadLine();
}
}

public class CA
{
public void fun1()
{
Type type = GetType();
Console.WriteLine("CA: fun1");
}
public virtual void fun2()
{
Type type = GetType();
Console.WriteLine("CA: fun2");
}
public virtual void fun3()
{
Type type = GetType();
Console.WriteLine("CA: fun3");
}
public virtual void fun4()
{
Type type = GetType();
Console.WriteLine("CA: fun4");
}
}
public class CB:CA
{
public void fun1()
{
Type type = GetType();
Console.WriteLine("CB: fun1");
}
public void fun2()
{
Type type = GetType();
Console.WriteLine("CB: fun2");
}
public override void fun3()
{
Type type = GetType();
Console.WriteLine("CB: fun3");
}
public new void fun4()
{
Type type = GetType();
Console.WriteLine("CB: fun4");
}
}
}

It's out put is as follow



Labels: , ,

C# -Daily Learning

Friday, June 4, 2010

Difference between ref and out parameter

a ref parameter should be assigned before they are called.
out are assigned by the called function itself.
out parameter is used to return more than one values from a function other than return object.
ref = Reference, when parsed to a method, the method references that original variable and doesnt create another instance. So if you make a change to a var parsed into that method when ref is used, it will affect the calling method

Labels: ,

Learning C# - Everyday learning

Thursday, June 3, 2010

Namespaces can be nested. So I can have one namespace within another.
System is most frequently used namespace
Inside System namespace is class called Console, which contains static methods.

The datatypes are defined as classes, they are equipped with methods. ToString() is a common method for all the datatypes.

We can use pointers in C# but we have to mark the functions that use pointers as 'unsafe'

unsafe static void Main()

To read numbers from console window use Parse method.
 Number = int.Parse(Console.ReadLine());

Formatting numbers
Character Used For
c C Currency values
d D Decimal numbers
e E Scientific numeric display such as 1.45e5
f F Fixed decimal numbers
g G General and most common type of numbers
n N Natural numbers
r R Roundtrip formatting
x X Hexadecimal formatting
p P Percentages
var Distance = 248.38782;
Distance.ToString("E") would result var into 2.483878E+002

var NewColor = 3478;
NewColor.ToString("X") would result var into D96

var HSalary = 22.74121
HSalary.ToString("c") would result var into $22.74

var HoursWorked = 35.5018473;
HoursWorked.ToString("F") would result var into 35.50

Labels: ,

HTML CSS training

Last week we had training on HTML CSS taken by Lakshminarayana. Training included on-board explaination, theory lectures, Hands-On as well as assessment (which I have not completed till i wrote this post). I am going to put some of the points that I learnt and found really useful which as a developer we tend to ignore or forget.
Specificity : It shows that how specific a selector is and based on the weight-age it assigns the value. Specificity is calculated as follow :
if Specificity is --> a b c d
then a i shows if it is inline style. if yes (a=1) and the specificity becomes highest and gets precedence over all other selectors.
b shows number of Id attributes in the selector
c is for class and pseudo classes
d is for number of elements

Box Model

You can define margin as either explicitly margin-left, margin-top
or you can give it as whole
margin: '10px 12px 6 px 4px';
where 10px is top margin, 12 is right and sequentially clockwise.

Similarly padding and border can be defined.
margin backgrounds are always transparent.

In CSS 2.1 horizantal margin never collapse but vertical may.

Vertical margins may collapse between certain boxes:

  • Two or more adjoining vertical margins of block boxes in the normal flow collapse. The resulting margin width is the maximum of the adjoining margin widths. In the case of negative margins, the maximum of the absolute values of the negative adjoining margins is deducted from the maximum of the positive adjoining margins. If there are no positive margins, the absolute maximum of the negative adjoining margins is deducted from zero. Note. Adjoining boxes may be generated by elements that are not related as siblings or ancestors.
  • Vertical margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
  • Vertical margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
  • Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).
  • Margins of inline-block elements do not collapse (not even with their in-flow children).
  • If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.
    • If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.
    • Otherwise, either the element's parent is not taking part in the margin collapsing, or only the parent's bottom margin is involved. The position of the element's top border edge is the same as it would have been if the element had a non-zero bottom border.

    An element that has had clearance applied to it never collapses its top margin with its parent block's bottom margin.

    Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements.

  • Margins of the root element's box do not collapse.
Margin width property can be difine in 2 ways, one through definite margin width and another is percentage. percentage value is defined based on width of the content (even margin-top also refers to width of content in case of percentage. so percentage of content's width is taken not the height)

salt marched the GIDS.Net

Tuesday, April 20, 2010

The fresh morning, we headed towards beautiful place amidst city traffic. IISc, a dreamland for all GATE aspirants has been a perfect place to have such a summit that provides all the means to organize evnt and at the same time surrounded by in campus greenry. We entered the reception area, got our id, meal coupon and other goodies.

The entire day was heavily loaded with sessions from different technologies. I could see some microsoft techology evangelist, key note speakers walking around. some of the faces from speakers and developers I could easily recollect from last year conference at same place. Personally, I liked Stephen Forte's presentation last year and was looking forward to attend at least one of his sessions. Finally Idecided to attend his session on SQL 2008 R2 just after the lunch and it was very interesting. Another session that I liked most was "Hands on MVVM for WPF and silverlight" by Anoop from Sumeru. We had nice lunch with my faorite dessert "gulab jamoon" :). Though sessions were well distributed but still in each time slot I found more than one topic or speaker that I was interested in.
All in all it's been a lovely day so far before I finish my day at conference by attending last session. Good wrk done by saltmarch team.