The last couple of days I’ve been working on improving the in-site search on this website. The old search was quite good, but it returned a list of full posts as a search result. It was difficult to get a good and quick overview of the results that way. When you included comments in the search it didn’t display the comments so you didn’t know whether or not the result was from a post or a comment. Last but not least, it didn’t search in pages. It wasn’t that big an issue for me because I only have one page, but in the future I plan to add more.
Enter the new search
I tried to keep as close to the major search engines' way of displaying results, but added some information relevant for blogs such as tags and categories. Try a search for code or Mads.
As you’ll see from the search results it displays posts, pages and comments in the same result page. It also tells you the type of each result so you can decide whether or not it is relevant to you.
Comment search
It has been possible to include comments in search results ever since BlogEngine.NET 1.0, but with this new search it gets more transparent. You can for instance search for your own name to see all the comments you’ve written yourself. That way I can see that Phil Haack has commented 6 times on my blog and Scott Hanselman has written one comment.
This new search feature will of course be included in the upcoming version of BlogEngine.NET due in a few weeks.
I’ve been having some issues with storing Unicode characters in cookies today. Whenever a cookie is set and the value filled with Unicode characters, the same characters cannot be retrieved from the cookie again. When they are retrieved from the requesting browser, they are changed into something totally unreadable.
Background
The cookie is set when a visitor enters some text into a textbox and submits the form. When the same visitor returns to that page I wanted to pre-fill the textbox with the value submitted earlier. Very easy and simple and not before someone noticed the strange behaviour with Unicode characters I thought it worked just fine.
Because the value was displayed in a textbox I thought that maybe HTML encoding could solve the issue. Don’t ever HTML encode a cookie in ASP.NET! It results in a yellow screen of death and an exception stating that the cookie contains dangerous characters. The dangerous character it was referring to was a HTML encoded representation of a Unicode character and looked something like this "#248;". The only thing to do is to delete your cookies in order to view that page again.
The solution
It took me a while to figure it out, but all you need to do is to URL encode the cookie value. It works no matter what encoding you use for the page. The example below illustrates the very simple solution:
private void SetCookie()
{
HttpCookie cookie = new HttpCookie("cookiename");
cookie.Expires = DateTime.Now.AddMonths(24);
cookie.Values.Add("name", Server.UrlEncode(txtName.Text));
Response.Cookies.Add(cookie);
}
private void GetCookie()
{
HttpCookie cookie = Request.Cookies["cookiename"];
if (cookie != null)
{
txtName.Text = Server.UrlDecode(cookie.Values["name"]);
}
}
It is so simple but caused me a lot of time investigating and clearing cookies from the browser.