How to access HttpServerUtility.MapPath in a Thread

Usually, when you want the root directory of your website, it is easy to call:

HttpContext.Current.Server.MapPath

or just

Server.MapPath

from a page.

But if you are running your code in a thread or timer, then HttpContext.Current is null (giving you a NullReferenceException), and you are out of luck. :(

However, as long as you are not using virtual directories, there is an easy fix:

AppDomain.CurrentDomain.BaseDirectory

will give you the same result, and doesn’t depend on a current HTTP context.

Quick Link Building tip

If you haven’t been following the link building tips over a Collin Lahay’s blog, then you should.

He often gives out locations of sites that allow dofollow links (for example work.com). This is great - but the site is huge - this tip allows you to find the best place to add your comments quickly.

First off, install the seoquake toolbar. This toolbar mixes in seo data with your google search results.

Then go to google and enter:

site:work.com +your +keywords

You will get a list of pages on work.com that relate to your niche - showing the PR, inbound links, age etc. So you know which is the best page to comment on.

Note: Please only add useful comments, nobody likes spam