How to hide database table primary ID in ASP.NET Routes?
I am using asp.net routes for the first time. Here is something I am trying to achieve.
Registered routes in global.asax file
routes.MapPageRoute("HelpEditRoute", "Help/{action}/{id}", "~/ad/Help.aspx")
This code inside Help.aspx
Response.Redirect("~/Help/Edit/12")
In code behind page of say help.aspx I am doing a redirect to following url to edit article number 12. Here ID is the database table primary key ID.
Problem: I don't want to show database primary key(ID) into browser address bar. Is ther开发者_JAVA百科e any way to do it?
Thanks.
Oftentimes this is referred to as a url slug. And, in manageable scenarios (eg NOT stack overflow which generates URL slugs AND exposes the primary key), it is easy to add a user-generated, uniquely indexed field to your table to handle this. Code-wise, if you stick with the default routing conventions, you just lookup based on the surrogate key (slug) you just created rather than the primary key.
A common way to get around showing the ID in the url is to encrypt the ID (one example: How to Encrypt Query String Parameters in ASP.NET | Keyvan Nayyeri), there are a multitude of ways to encrypt an ID then decrypt on the server side.
Another method is to use GUIDs as a key to your DB record and use the GUID in your URL
精彩评论