Wednesday, June 10, 2009

Running Cassini (local) Web Server without having the Application Name in the url

One of the small issues of the Cassini server thats built in with Visual Studio IDE is that when you run it, it also has the Folder name of your application/project in the url. While this is not a major issue, it surely does some damage to the relative urls of images and/or other files which are relative. This would mean changing the relative paths for the development and production copies. I have done that in the past and know it is annoying if one forgets to change them, even though they can be stored in Application web.config file

One of the fixes that I found out was to configure the site using the external tools section of the IDE:


Clicking on the external tools takes you to the following window:



And thats what it is.

Follow this to have it done.

Click on "Add" to add a new Item to the project.
Title : This could be your project name
Command : This is the path for the Cassini Web server. Locate its exe and provide over here. This is what I have
(C:\Program Files\Common Files\microsoft shared\DevServer\9.0\WebDev.WebServer.EXE)

Arguments : Here you would assign a port number and your application folder as your arguments. The port number can be any number. The Application folder is where your project resides

/port:8085 /path:"C:\Users\Username\Documents\Visual Studio 2008\WebSites\SD30"


Once you are done. Click on Ok.

Now click on "Tools" from the IDE and you will see your Application Name in the Menu. Click on your Application Name to instantiate the Cassini Web Server for your Application. You will see the Web server activated in the task bar. Click on it to open your default page.

Thats all! Nice and Easy!

Monday, June 8, 2009

Using Google SiteMaps

Today I wanted to create a dynamic google sitemap file for my site. So I started reading the documentation for creation of a sitemap. Google has a very good documented format, making all the points clear. you can vew it here. The Google Webmaster tools are also one of the powerful features where you could view the site stats, and thats where you need to submit your google sitemap.

The sitemaps protocol is defined at http://www.sitemaps.org/protocol.php

The sitemap is placed in the root directory which contains all the site urls for google to index pages. You can have a maximum of 50,000 urls per Sitemap. For additional urls, another sitemap file can be created and then linked with each other using SiteMap indexing file. Google does great at explaining all this stuff, so make sure you go through all the stuff in the link that I have put up here above to get the best of both worlds.

Generating the dynamic SiteMap file:

Generation is just like any other xml file, except that you have to adhere to the standards mentioned in the sitemap protocol.

Here's the code. You have to modify the query to suit your needs.


public partial class NewsSiteMap : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
DataClassesDataContext db = new DataClassesDataContext();

var latestNewsArticles = (from articles in db.article_to_magazine_mappings
where
articles.magazine_id == 1 && articles.article.article_type_id != 20
select new
{
ID = articles.article.id,
Title = articles.article.web_title,
TitleLink = articles.TitleLink,
Author = articles.AuthorName,
TitleKeywords = articles.article.meta_tags,
Date = articles.publish_date,
LegacyID = articles.article.legacy_id
})
.Take(49999);


Response.Clear();

Response.ContentType = "text/xml";

using (XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
{
writer.WriteStartDocument();

writer.WriteStartElement("urlset");

writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

foreach (var newsarticle in latestNewsArticles)
{
writer.WriteStartElement("url");

writer.WriteElementString("loc", ConcatenateUrls(newsarticle.TitleLink,newsarticle.Author,newsarticle.TitleKeywords,newsarticle.ID.ToString()));

writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", newsarticle.Date));

writer.WriteElementString("changefreq", "daily");

writer.WriteElementString("priority", "1.0");

writer.WriteEndElement();
}

writer.WriteEndDocument();

writer.Flush();

}

db.Dispose();
db = null;

Response.End();
}

protected string ConcatenateUrls(string title, string author, string keywords, string articleid)
{
return GetHost(title + "/By_" + author + keywords + "/" + articleid);
}
}


In the html, you just need to add the following line
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="newsSitemap.aspx.cs" Inherits="NewsSiteMap" %>


Make sure you have encoded your content so that it doesnt break.
After you are done, submit your sitemap file to Google, and you are good!

Tuesday, June 2, 2009

Firefox and Mouseout Issue

Today I had an a very minor issue, but it was worth writing here. Firefox had this nasty delay of triggering from the mouseover even to mouseout event on my menu which had a javascript to be executed in order to change the text inside a span tag. Having done this tons of times before, I cannot recall this issue. However, this might be due to the fact that the anchor tag encompassing the span tag of the menu had a background image applied and this might be the cause of conflict or delay on the triggering of the mouseout event of the span tag. This issue was not displayed in IE, Opera or Safari, though.

I tried using innerHTML to change the text, but of no avail. After a couple of quick searches, I found out using the
obj.firstChild.data
element, where obj is your span tag. It worked perfect miraculously! This brings to the conclusion that the old school DOM model is still worthwhile!
There was an error in this gadget