G
Guest
Hi guys
I want to create a situation whereby users can use a band name to access a
data page. So..
site.com/bandname actually goes to site.com/music/artist.aspx?id=100
I currently do this by having loads of directories, but that means loads of
work if I add a new artist, so I wanted to recode it using
Context.RewritePath.
Here's the code I've got in global.asax..
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
string URL = incoming.Request.Path.ToLower();
string reWri = new URLRewriter().RewriteURL(URL).ToString();
if(reWri.CompareTo("") != 0)
incoming.RewritePath(reWri);
}
And the URLRewriter class looks like this...
public class URLRewriter
{
public URLRewriter()
{
}
public string RewriteURL(string URL)
{
// see if we can find an artist, and send back a URL or a blank string
string bandname, retURL="";
OleDbConnection dbConn = new
OleDbConnection(ConfigurationManager.ConnectionStrings["mswDB"].ToString());
dbConn.Open();
// get the artist
OleDbCommand artCmd = new OleDbCommand("SELECT ArtistID, ArtistName
FROM Artists ORDER BY ArtistName", dbConn);
OleDbDataReader artDr = artCmd.ExecuteReader();
while (artDr.Read())
{
bandname = artDr["ArtistName"].ToString().ToLower();
bandname = bandname.Replace(" ","");
bandname = "/" + bandname;
if (bandname.CompareTo(URL) == 0)
retURL = "/_music/artist.aspx?id=" +
artDr["ArtistID"].ToString();
}
artDr.Close();
artDr = null;
dbConn.Close();
dbConn = null;
return retURL;
}
}
I think the code is right - I've had some test stuff in there and I know
it's returning the "_music/artist.." page URL. The problem is that it doesn't
redirect to it. I just get a 404...
Am I missing some code, or do I need to do something in IIS to make this
work?! Any help appreciated!
Cheers
Dan (running Win2k3, IIS6, vs2005, .net2)
I want to create a situation whereby users can use a band name to access a
data page. So..
site.com/bandname actually goes to site.com/music/artist.aspx?id=100
I currently do this by having loads of directories, but that means loads of
work if I add a new artist, so I wanted to recode it using
Context.RewritePath.
Here's the code I've got in global.asax..
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
string URL = incoming.Request.Path.ToLower();
string reWri = new URLRewriter().RewriteURL(URL).ToString();
if(reWri.CompareTo("") != 0)
incoming.RewritePath(reWri);
}
And the URLRewriter class looks like this...
public class URLRewriter
{
public URLRewriter()
{
}
public string RewriteURL(string URL)
{
// see if we can find an artist, and send back a URL or a blank string
string bandname, retURL="";
OleDbConnection dbConn = new
OleDbConnection(ConfigurationManager.ConnectionStrings["mswDB"].ToString());
dbConn.Open();
// get the artist
OleDbCommand artCmd = new OleDbCommand("SELECT ArtistID, ArtistName
FROM Artists ORDER BY ArtistName", dbConn);
OleDbDataReader artDr = artCmd.ExecuteReader();
while (artDr.Read())
{
bandname = artDr["ArtistName"].ToString().ToLower();
bandname = bandname.Replace(" ","");
bandname = "/" + bandname;
if (bandname.CompareTo(URL) == 0)
retURL = "/_music/artist.aspx?id=" +
artDr["ArtistID"].ToString();
}
artDr.Close();
artDr = null;
dbConn.Close();
dbConn = null;
return retURL;
}
}
I think the code is right - I've had some test stuff in there and I know
it's returning the "_music/artist.." page URL. The problem is that it doesn't
redirect to it. I just get a 404...
Am I missing some code, or do I need to do something in IIS to make this
work?! Any help appreciated!
Cheers
Dan (running Win2k3, IIS6, vs2005, .net2)