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();
}
c开发者_如何转开发atch (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;
}
You're approaching this in the typical manner. Scott Gu's URL Routing with ASP.NET 4 shows the exact same pattern you're using.
- setup the route
- ensure a token is on the URL somewhere
- on page load of your target .aspx page, grab the token, query the db.
- display your markup as needed for that token
If you were looking for suggestions:
- Each property should have a 'slug' or token pre-extracted in your database. Just another property to query on, not the numeric identifier. The DataTable isn't helping too much.
- Keep your token as the street address (perhaps include city for SEO juice if your client doesn't mind)
- Use that token to go look up the house/property record in the database. Forget numeric IDs at this point. SELECT the property where token matches, and write it to the page. Perf difference is zero.
string propertyToken = Page.RouteData.Values["address"].ToString();
Property p = PropertyDatabase.GetSingleByToken(propertyToken);
if(p!=null)
{
WritePropertyToPage(p);
}
else
{
//write to page that token wasn't found/misspelled/expired/etc.
}
Have you looked at ASP.NET MVC Routing Overview (C#)? That's my suggestion for where to look though ASP.NET MVC Framework (Part 2): URL Routing also has some tips that may be worth examining to some extent as you almost have something RESTful in a sense. At least that is what I'd consider before trying out the web forms solutions.
You're intuition is correct, throwing when Page.RouteData.Values["address"] is undefined is poor practice--especially since it is entirely reasonable for it to not exist.
Testing for null prior to attempting a method call on an object is always a good practice. You might also consider the coalesing operator ?? (see http://msdn.microsoft.com/en-us/library/ms173224.aspx), though some think it's ugly and/or difficult to read.
精彩评论