I'm working on a real estate website and using URL Routing so my client can have their featured properties show their address in the URL: www.realestatewebsite.com/featured/123-Fake-St
However, I'm new to URL Routing and I think I took a poor shortcut. I would have originally liked to use the primary key of my database to access the properties (propID) but didn't want to have to pass it into the URL, so instead used the address to get the propID and use it from there. However, if I attempt to assign Page.RouteData.Values["address"] to a string and there is no address in the URL it throws an exception. Therefore I have a try/catch statement to handle it. This seems like poor practice to me though. If anyone can tell me whether or not this is acceptable to do, or if there's a much better solution, please let me know.
Here's the Global.asax:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "Featured/{address}", "~/Featured/Default.aspx");
}
And here's a method from www.website/com/Featured/Default.aspx:
protected int getPropID()
{
string address;
int propID = -1; //If the method returns -1 the page just lists all featured properties
try
{
address = Page.RouteData.Values["address"].ToString();
}
catch (Exception ex)
{
return propID;
}
if (address != null)
{
string strSQL = "SELECT propID FROM tblFeatured WHERE address = '" + address + "'";
DataTable dt = da.FillDataTable(strSQL);
if (dt.Rows.Count > 0)
propID = Convert.ToInt32(dt.Rows[0]["propID"]);
return propID;
}
else
return propID;
}