Simple ASP.NET SEO Enhancement

As many people have mentioned one of the most important (and easy) SEO related enhancements you can make to your site is to use a unique title and meta description on each page.

In ASP.NET 2.0 the page title is easily set either via either the title attribute of the page directive in the markup, or by setting it directly in code e.g

this.Title = “my unique title”;

Creating a unique meta description is slightly more involved but still pretty straight forward.

HtmlMeta metaDescription = new HtmlMeta();
metaDescription.Name = “description”;
metaDescription.Content = “my unique description”;
this.Header.Controls.Add(metaDescription);

These are two of the simplest and quickest SEO related enhancements you can make to your ASP.NET site – you’ve really got no excuse not to!

Ndbunit is great – once you get past the pain

Over the last few days I’ve been setting up my latest project to use Ndbunit to initialise the database to known state prior to running each of my unit tests. It has been a bit of painful process as the documentation is fairly minmal and non-existant for some features, but I think I’ve got there now.

A couple of things I wanted to put down here in case I ever forget them and start banging my head on the desk again!

Firstly if you want to use the InsertIdentity method you need to make sure your xsd sets the AutoIncrement attribute to true for your identity column. eg.

<xs:element autoincrement=”true” type=”xs:int” name=”PERSON_ID”>

Secondly if you want to insert more than one row into a table you need to make sure your xsd sets the IsDataSet attribute to true e.g

<xs:element name=”PERSON” isdataset=”true”>

HttpHandlers and session state

While hacking on my website the other night I found a need to build an HttpHandler (actually a page would have worked fine, but a httphandler was more elegant). I got part way through debugging when I found that session state wasn’t available in an HttpHandler! This caused me some consternation as I have an HttpModule which I use to retrieve my nhibernate session during the execution of a request and it stores the nhibernate session in the HttpContext session (hopefully that’s not too many sessions and you’re still with me).

Anyway I almost re-engineered the handler to work another way when a search of google groups revealed this simple solution, your handler simply has to “implement” the empty IRequiresSessionState interface (you’ll need to be using the System.Web.WebSessionState namespace.) e.g.

public class VerificationHttpHandler : IHttpHandler, IRequiresSessionState

That’s it, you now have http session available in your handler.

nhibernate – a complex beast

I am beginning to discover that nHibernate is a far more complex beast than I realised. Over the last couple of days I’ve spent some more time reading the documentation (out of necessity rather than pure interest) and it is capable of some amazing things, however this flexiblity comes at the cost of complexity (as is often the case).

Here’s an example of a trap for young players that caught me the other night. I have two entities A & B that map two two underlying tables tableA and tableB. The primary key of tableA is a foreign key in tableB. tableA contains a property Rating1Count which is the count of a column in tableB. I had code similar to the following:

ISession session = NHibernateHttpModule.CurrentSession;
ITransaction transaction = session.BeginTransaction();
try
{
    log.Debug(“Saving new B”);
    session.SaveOrUpdate(b);

    log.Debug(“saved new B”);
    ICriteria crit = session.CreateCriteria(typeof(A));
    crit.Add( Expression.Eq(“AId”, a.AId) );
    log.Debug(“Executing query”);
    IList As = crit.List();
    if (As.Count == 1)
    {
        a = As[0] as A;
        log.Debug(string.Format(“Count of 1s in tableB {0}”, a.Rating1Count));
    }
    transaction.Commit
}

After testing this I found that even though my changes were being persisted to tableB (verified by querying mysql directly) they weren’t reflected in my tableA object.

Then it dawned on me, at what point does nhibernate write the changes to the database? I moved the transaction.Commit line up to be directly below the call to SaveOrUpdate and lowe and behold it works!

In hindsight it’s obvious, but it had me scratching my head for a while (and it was late – at least that’s my story and I’m sticking to it!).